3
Answers

How to read SQL queries from .txt file for C#?

Mani Kandan

Mani Kandan

8y
603
1
Simply runs a query to that database (query available in a text file) file name to be send as a parameter to the exe.

How Read Text File contain query in (cmd.CommandText=)?

protected void ExportTextFile(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
Stream str = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(str);
Byte[] size = br.ReadBytes((int)str.Length);
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "How Read Text File contain query here???";
cmd.Parameters.AddWithValue("@Name", filename);
cmd.Parameters.AddWithValue("@Type", "application/text");
cmd.Parameters.AddWithValue("@Data", size);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}

Answers (3)