locked
Output an SQL query to a script file using C# RRS feed

  • Question

  • Can anyone explain how I can turn the code below into an SQL query by outputting the result to the script file?

     private void button3_Click(object sender, EventArgs e)
        {
          string text = "Whatever, blah blah..";
          File.WriteAllText(mainPath + txtRelease.Text + @"\data_" + txtRelease.Text + ".txt", text);
        }
    
    Friday, January 21, 2011 10:06 AM

Answers

  • It sounds like maybe you want to execute the SQL Query at the database and output the result to a text file? If so, try this:

    using (SqlConnection oConn = new SqlConnection(this.ConnectionString))
    {
      SqlCommand sc = new SqlCommand("SELECT * FROM MyTable", oConn);
      SqlDataAdapter da = new SqlDataAdapter(sc);
      DataSet ds = new DataSet();
      da.Fill(ds);
      
      ds.WriteXml("MyFile.xml");
    }
    
    

    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Proposed as answer by Neddy Ren Wednesday, January 26, 2011 4:01 AM
    • Marked as answer by crazyco Wednesday, January 26, 2011 1:37 PM
    • Unmarked as answer by crazyco Wednesday, January 26, 2011 1:38 PM
    • Marked as answer by crazyco Wednesday, January 26, 2011 1:38 PM
    Monday, January 24, 2011 1:16 AM

All replies

  • Sorry, but I can't understand your Question.

    Can you give us more information?


    Florian Kowalsky -> If it was helpful please mark ist as helpful!!
    Friday, January 21, 2011 10:11 AM
  • Hi, I am trying to figure out how I can use an SQL query here and output the result to a text file.  At the moment it works just by manually entering the text "Whatever, blah blah.." as the output but would like to use an SQL query here, just a very simple SELECT statement so I can see how it works.

    Thanks

    Friday, January 21, 2011 10:18 AM
  • Where is the SQL code? I don't see it.

    Your code writes to a file, not to data base!

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    Sunday, January 23, 2011 9:22 AM
  • Hi,

    I don't really understand your question.

    Do you want to put an SQL query in your text file and execute it ?

    Could you please explain ?

    Regards,

    Sunday, January 23, 2011 6:04 PM
  • It sounds like maybe you want to execute the SQL Query at the database and output the result to a text file? If so, try this:

    using (SqlConnection oConn = new SqlConnection(this.ConnectionString))
    {
      SqlCommand sc = new SqlCommand("SELECT * FROM MyTable", oConn);
      SqlDataAdapter da = new SqlDataAdapter(sc);
      DataSet ds = new DataSet();
      da.Fill(ds);
      
      ds.WriteXml("MyFile.xml");
    }
    
    

    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Proposed as answer by Neddy Ren Wednesday, January 26, 2011 4:01 AM
    • Marked as answer by crazyco Wednesday, January 26, 2011 1:37 PM
    • Unmarked as answer by crazyco Wednesday, January 26, 2011 1:38 PM
    • Marked as answer by crazyco Wednesday, January 26, 2011 1:38 PM
    Monday, January 24, 2011 1:16 AM
  • BonnieB, thank you, this got me started.  Your answer wrote it in xml format so I adapted it to write to a .txt file.
    Wednesday, January 26, 2011 1:39 PM
  • You're welcome, glad I could help! =0)

    Do you mean you simply changed ds.WriteXml("MyFile.xml") to ds.WriteXml("MyFile.txt")?


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Proposed as answer by Neddy Ren Thursday, January 27, 2011 1:33 AM
    • Unproposed as answer by Neddy Ren Friday, January 28, 2011 1:16 AM
    Wednesday, January 26, 2011 4:42 PM