简体   繁体   中英

Cant backup sql database with c#

I tried this code samples

SqlCommand cmd = new SqlCommand();
SqlConnection con2 = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\schoolmate.mdf;Integrated Security=True;User Instance=True");
string sql = "BACKUP Database '" +Application.StartupPath+@"\schoolmate.mdf'" +" TO DISK ='D:\\schoolmate-" + DateTime.Now.Ticks.ToString() + ".bak'";
con2.Open();
cmd = new SqlCommand(sql, con2);
cmd.ExecuteNonQuery();

it gives a Incorrect syntax error.

And also tried

string sql = "BACKUP Database 'schoolmate.mdf' TO DISK ='D:\\schoolmate-" + DateTime.Now.Ticks.ToString() + ".bak'";

and

string sql = "BACKUP Database 'schoolmate' TO DISK ='D:\\schoolmate-" + DateTime.Now.Ticks.ToString() + ".bak'";

Which gives database not found error on cmd.ExecuteNonQuery();

尝试这个:

string sql = "BACKUP Database [schoolmate] TO DISK ='D:\\schoolmate-" + DateTime.Now.Ticks.ToString() + ".bak'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM