How to take Back up of LocalDB C# my database file name is MyDatabase.mdf

蹲街弑〆低调 提交于 2019-12-25 05:21:58

问题


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:


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



来源:https://stackoverflow.com/questions/36512709/how-to-take-back-up-of-localdb-c-sharp-my-database-file-name-is-mydatabase-mdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!