generating sql script of query performed using c# code

510 Views Asked by At

Hi I am working on a project,where I am generating sql query for CRUD operations performed on a table having xml data in one of its column.I have successfully generated sql queries and for now I am displaying those query in a textarea and saving those query in a .txt file but now I want that those query could be saved to a .sql file. For now I am storing my query in a text file with the below code.

Query for update

sqlQuery = "update tblCCBT_Step_Page_Text_Xml set Xml_XmlData.modify('replace value of (/page/*[position()="+xmlNodeIndex+"]/text())[1] with " + newValue + " ') where Xml_Id = " + xmlId;
            string a = Server.MapPath("~/Content/dbScripts");
            string dt = System.DateTime.Now.ToShortTimeString();
            dt = dt.Replace(":", "-");
            FileStream fs1 = new FileStream(a + "\\editNode_"+dt+".txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter writer = new StreamWriter(fs1);
            writer.Write(sqlQuery);
            writer.Close();

I want my query to be saved in .sql file.Please suggest how to proceed further.Thanks

2

There are 2 best solutions below

0
TTeeple On

A .sql file is the same as a .txt file, just with a different extension. This is testable by putting a New Document on your desktop, name it query.txt, put SQL (something simple like SELECT 'A') in it, save, and rename it to query.sql.

You should be able to just replace the ".txt" with ".sql".

0
rupinder18 On

I was able to save my query to a .sql file by just replacing .txt to .sql.

Updated code

sqlQuery = "update tblCCBT_Step_Page_Text_Xml set Xml_XmlData.modify('replace value of (/page/*[position()="+xmlNodeIndex+"]/text())[1] with " + newValue + " ') where Xml_Id = " + xmlId;
            string a = Server.MapPath("~/Content/dbScripts");
            string dt = System.DateTime.Now.ToShortTimeString();
            dt = dt.Replace(":", "-");
            FileStream fs1 = new FileStream(a + "\\editNode_"+dt+".sql", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter writer = new StreamWriter(fs1);
            writer.Write(sqlQuery);
            writer.Close();