To run sql script in C# and the script is executed at the back end.
Use a File upload control in the apsx page with button.
protected void UploadButton_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
string filename = Path.GetFileName(FileUpload1.FileName); FileUpload1.SaveAs(Server.MapPath("~/SQL/") + filename);
string script = File.ReadAllText(Server.MapPath("~/SQL/") + filename);
using (SqlConnection conn = new SqlConnection(CS))
{
SqlCommand sqlCmd = new SqlCommand(script, conn);
conn.Open();
Server server = new Server(new ServerConnection(conn)); server.ConnectionContext.ExecuteNonQuery(script);
StatusLabel.Text = "Upload status: "+filename+" is uploaded successfully!";
}
}
catch (Exception ex)
{
StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
}
protected void UploadButton_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
string filename = Path.GetFileName(FileUpload1.FileName); FileUpload1.SaveAs(Server.MapPath("~/SQL/") + filename);
string script = File.ReadAllText(Server.MapPath("~/SQL/") + filename);
using (SqlConnection conn = new SqlConnection(CS))
{
SqlCommand sqlCmd = new SqlCommand(script, conn);
conn.Open();
Server server = new Server(new ServerConnection(conn)); server.ConnectionContext.ExecuteNonQuery(script);
StatusLabel.Text = "Upload status: "+filename+" is uploaded successfully!";
}
}
catch (Exception ex)
{
StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
}