Restore database from SQL script file using C# and VB.Net in ASP.Net

Last Reply 16 days ago By dharmendr

Posted 16 days ago

hi

now I want to import script into data base how I can do it ?

refer below link Generator Sql Script from database Table using C# and VB.Net in ASP.Net

Best regards

Neda

Posted 16 days ago

Hi nedash,

Refer below code.

C#

protected void Restore(object sender, EventArgs e)
{
    string data = System.IO.File.ReadAllText(Server.MapPath("~/Test.sql"));
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
    SqlCommand cmd = new SqlCommand(data, con);
    cmd.CommandType = CommandType.Text;
    cmd.Connection = con;
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
}

VB.Net

Protected Sub Restore(ByVal sender As Object, ByVal e As EventArgs)
    Dim data As String = System.IO.File.ReadAllText(Server.MapPath("~/Test.sql"))
    Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
    Dim cmd As SqlCommand = New SqlCommand(data, con)
    cmd.CommandType = CommandType.Text
    cmd.Connection = con
    con.Open()
    cmd.ExecuteNonQuery()
    con.Close()
End Sub