How to take Back up of LocalDB C# my database file

2019-09-13 19:41发布

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();

1条回答
来,给爷笑一个
2楼-- · 2019-09-13 20:23

Error was that "The identifier start with---- is too long. Maximum length is 128" so I Make the "MyDatabase.mdf" with small Name "MyDb.mdf".So the identifier becomes less than 128.

My code is


 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();

}

successfully take the Backup

查看更多
登录 后发表回答