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();
答案 0 :(得分:0)
错误是"标识符以----开头太长。最大长度为128" 所以我制作了#34; MyDatabase.mdf"使用小名称" MyDb.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();
}
成功进行备份