string dbpath= System.Windows.Forms.Application.StartupPath;
string dbp = dbpath + "\\MyDatabase.Mdf";
SqlCommand cmd = new SqlCommand("backup database ['"+dbp+"'] to disk ='d:\\svBackUp1.bak' with init,stats=10",con);
cmd.ExecuteNonQuery();
错误是“以----开头的标识符太长。最大长度为128” 所以我用小名称“MyDb.mdf”制作“MyDatabase.mdf”。因此标识符变得小于128。
我的代码是
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NewConnectionString"].ConnectionString);
{
con.Open();
string DatabaseName = Application.StartupPath + @"\MyDb.mdf";
SqlCommand cmd = new SqlCommand("BACKUP DATABASE ["+DatabaseName+"] to DISK='D:\\MyBackup.bak' ", con);
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Success");
}
catch (Exception Ex){
MessageBox.Show("'"+Ex.ToString()+"'");
}
con.Close();
}
备份成功
非常有帮助,备份部分已完成,用于恢复该 .bak 文件的代码是什么