Dear sir,
As per your direction i wrote the code.
But still it is error
Incorrect syntax near 'VMS'.
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
private void button2_Click(object sender, EventArgs e)
{
//con1 = new SqlConnection("Data Source=" + comboBox1.Text + ";Database=Master;data source=.; Initial Catalog=" + comboBox2.Text + "; uid=sa; pwd=321;");
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["VMS"].ToString());
string database = con.Database.ToString();
if(con.State!=ConnectionState.Open)
{
con.Open();
}
try
{
string sqlStmt2 = string.Format("ALTER DATABASE[" + database + "]SET SINGLE_USER WITH ROLLBACK AFTER 5");
SqlCommand cmd = new SqlCommand(sqlStmt2, con);
cmd.ExecuteNonQuery();
string sqlstmt3 = "USE MASTER RESTORE DATABASE[" + database + "] FROM DISK='" + textBox1.Text + "';WITH MOVE'" + cmblogicalnamemdf.Text + "'TO'" + textBox2.Text + "',MOVE'" + cmdlogicalnameldf.Text + "'TO'" + textBox3.Text + "' WITH REPLACE MOVE;";
SqlCommand cmd1 = new SqlCommand(sqlstmt3, con);
cmd1.ExecuteNonQuery();
string sqlstmt4 = string.Format("ALTER DATABASE[" + database + "]SET MULTI_USER");
SqlCommand cmd2 = new SqlCommand(sqlstmt4, con);
cmd2.ExecuteNonQuery();
MessageBox.Show("database restoration done successfully");
con.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}