sqlexpress 不支持 BackUp 和restore sql 语句。进行备份和还原.
在网上找了一些例子。有一些问题。自己进行了简单修改 。现在能用了。现在分享出来。
public class DataOperationBackUp
{
public static string ConStr = ConfigurationManager.AppSettings["ConStr"].ToString();
public static void BackupDatabase(string sConnect,string filename)
{
string dbName;
var filenamestr = @"D:\SampleBackup.bak";
using (SqlConnection cnn = new SqlConnection(sConnect))
{
cnn.Open();
dbName = cnn.Database.ToString();
ServerConnection sc = new ServerConnection(cnn);
Server sv = new Server(sc);
// Check that I'm connected to the user instance
Console.WriteLine(sv.InstanceName.ToString());
// Create backup device item for the backup
BackupDeviceItem bdi = new BackupDeviceItem(filenamestr, DeviceType.File);
// Create the backup informaton
Backup bk = new Backup();
bk.Devices.Add(bdi);
bk.Action = BackupActionType.Database;
bk.BackupSetDescription = "SQL Express is a great product!";
bk.BackupSetName = "SampleBackupSet";
bk.Database = dbName;
DateTime now = new DateTime();
// bk.ExpirationDate = now.AddMonths(1);
bk.LogTruncation = BackupTruncateLogType.Truncate;
bk.Initialize = true;
bk.Checksum = true;
// Run the backup
bk.SqlBackup(sv);
Console.WriteLine("Your backup is complete.");
}
}
public static void RestoreBackup(string sConnect,string fileName)
{
string dbName;
var filenamestr = @"D:\SampleBackup.bak";
using (SqlConnection cnn = new SqlConnection(sConnect))
{
cnn.Open();
dbName = cnn.Database.ToString();
// 此处先要执行下这条sql 要不会 产生因为数据库正在使用,所以无法获得对数据库的独占访问权
string cmdText = "use master alter database "+dbName+" set SINGLE_USER with ROLLBACK IMMEDIATE ";
SqlCommand sqlcommand = new SqlCommand(cmdText, cnn);
sqlcommand.ExecuteScalar();
cnn.ChangeDatabase("master");
ServerConnection sc = new ServerConnection(cnn);
Server sv = new Server(sc);
// Check that I'm connected to the user instance
Console.WriteLine(sv.InstanceName.ToString());
// Create backup device item for the backup
BackupDeviceItem bdi = new BackupDeviceItem(filenamestr, DeviceType.File);
// Create the restore object
Restore resDB = new Restore();
resDB.Devices.Add(bdi);
resDB.NoRecovery = false;
resDB.ReplaceDatabase = true;
resDB.Database = dbName;
//cnn.Close();
// Restore the database
resDB.SqlRestore(sv);
Console.WriteLine("Your database has been restored.");
}
}
}
当中遇到了很多问题。刚开始的没注意。爆出来的错误都是 备份对于服务器“\\.\pipe\23B01ACD-73C3-49\tsql\query”失败。
这时候你需要打开异常查看 innerexception 具体原因:
错误1:混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集
添加配置文件解决
修改配置文件
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
还原的时候产生了
因为数据库正在使用,所以无法获得对数据库的独占访问权
执行这一句
string cmdText = "use master alter database "+dbName+" set SINGLE_USER with ROLLBACK IMMEDIATE ";
SqlCommand sqlcommand = new SqlCommand(cmdText, cnn);
sqlcommand.ExecuteScalar();
配置sqlexpress 数据库的时候还需要注意下连接字符串:
data source=.\SQLEXPRESS1;attachdbfilename=|DataDirectory|\ktvsystem.mdf;Initial Catalog=ktvsystem;integrated security=True;user instance=True;multipleactiveresultsets=True;
需要配置下Initial Catalog 这个属性。 要不数据库里面上面产生的dbname字段:带有绝对路径。如 c:\test\ktvsystem.mdf ; 这样执行"use master alter database "+dbName+" set SINGLE_USER with ROLLBACK IMMEDIATE "; 这条sql 会报错。。配置了Initial Catalog 就没其他什么问题了