Here is the code:

string firstA = "CREATE DATABASE " + textBox1.Text + ";" + "\n"; string firstB = "USE " + textBox1.Text + ";" + "\n"; firstPart = firstA + firstB; secondPart = "CREATE TABLE " + textBox2.Text + ";" + "\n"; System.IO.StreamWriter textFile = new System.IO.StreamWriter(@"E:\" + path + ".txt"); textFile.WriteLine(firstPart + secondPart); textFile.Close(); 

Displays:

 CREATE DATABASE data;USE data;CREATE TABLEusers; 

A must:

 CREATE DATABASE data; USE data; CREATE TABLE users; 

What is the reason? It seems all right = /

  • Who prints? What did you look at the file E:\????.txt ? - alexlz pm
  • one
    Environment.NewLine - wind
  • @alexlz via notepad: / - navi1893
  • @ navu1893 Strange it works. I would not be surprised if the file looked like this: CREATE DATABASE data; USE data; CREATE TABLE users; And in one line - a very strange notebook program. - alexlz

3 answers 3

Do not bother with the system ends of the lines. Let the equipment work for you:

 var commands = new[] { string.Format("CREATE DATABASE {0};", textBox1.Text), string.Format("USE {0};", textBox1.Text), string.Format("CREATE TABLE {0};", textBox2.Text) }; File.WriteAllLines(@"E:\" + path + ".txt", commands); 

By the way, think about what happens if a user enters a DB into textBox2 , and into a textBox2

TEST; DROP DATABASE DB; COMMIT

(and read about SQL injection and prepared statements).

  • Can i ask you? How can I do this: I have a button, when I click in C #, create a database. How to create an interpreter? That is, instead of typing, it works in a visual environment and at the end a database is created. I hope I got my message across - navi1893
  • @navi1893: Not quite clear. Interpreter what? If you mean a program that makes up the SQL commands for user input and communicates in the database, then in general you are going the right way, but I would (1) separate the interface and the work part into separate modules, (2) did not generate text in file, and worked with the database directly through eg. Entity Framework. - VladD
  • @VladD "did not generate text to a file, but worked with the database directly through eg." here it is interesting to me. How can I do? Can you tell? - navi1893
  • @navi1893: Entity Framework is the same! Well, a lot of other ways there. - VladD

At the end of the lines you need "\r\n" instead of "\n" .

  • And what will? Translations of some lines should be. - alexlz 5:53 pm
  • It worked! Thank you - navi1893

In .Net there is a class System.Enviroment, and it has a string property NewLine. Example of use:

 string firstA = "CREATE DATABASE " + textBox1.Text + ";" + Enviroment.NewLine; 

In general, pasting a large number of lines through a "+" is a moveton. It is better to use string.Format (...) or the class StringBuilder. For example:

 string firstA = string.Format("CREATE DATABASE {0};{1}", textBox1.Text, Enviroment.NewLine); string firstA = new StringBuilder().Append("CREATE DATABASE ").Append(textBox1.Text).AppendLine(";");