可能重复:
SQL Server - Database ‘Database’ does not exist. Make sure that the name is entered correctly
我写了这段代码,但是数据库无法识别备份?请指导
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Resources\dbcn.mdf;Integrated Security=True;User Instance=True";
SqlCommand cmd = new SqlCommand();
string MasterPath = "c://";
try
{
con.Open();
//BACKUP DATABASE نام بانک اطلاعاتی TO DISK = 'مسیر پشتیبان گیری بانک اطلاعاتی'
string query = "BACKUP DATABASE dbcn TO DISK = '" + MasterPath + @"\BackupLibrary" + "\\dbcn.mdf" + "'";
cmd.CommandText = query;
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("error");
}
catch(System.Exception EX)
{
System.Windows.Forms.MessageBox.Show(EX.Message);
}
错误;
Database 'dbcn' does not exist. Make sure that the name is entered correctly.
BACKUP DATABASE正在异常终止。
答案 0 :(得分:0)
这可能超出了您对此项目的需求,但将来您可能需要调查SQL管理对象(SMO)。它使您能够在托管代码中执行SQL Server维护任务(例如c#)。
我相信它是SQL Server SDK的一部分。您需要将以下引用添加到项目中:
Microsoft.SqlServer.Smo
Microsoft.SqlServer.SmoExtended
Microsoft.SqlServer.ConnectionInfo
Microsoft.SqlServer.Management.Sdk.Sfc
Microsoft.SqlServer.SqlEnum
我能够在以下位置找到这些程序集:C:\ Program Files \ Microsoft SQL Server \ 110 \ SDK \ Assemblies \
string ServerName = "server";
string DatabaseName = database;
string UserId = userId;
string Password = password;
ServerConnection serverConnection;
private Server _server;
public Server Server
{
get
{
if (_server == null)
{
Connect();
}
return _server;
}
}
string connectionString
{
get
{
return string.Format("Data Source={0};Initial Catalog={1};UID={2};Password={3}", ServerName, DatabaseName, UserId, Password);
}
}
public void Connect()
{
_serverConnection = new ServerConnection();
_serverConnection.LoginSecure = false;
_serverConnection.Login = UserId;
_serverConnection.Password = Password;
_server = new Server(_serverConnection);
}
public void BackupDatabase()
{
using (SqlConnection cn = new SqlConnection(connectionString))
{
ServerConnection svCon = new ServerConnection(cn);
Server svr = new Server(svCon);
cn.Open();
cn.ChangeDatabase("master");
string testFolder = @"C:\temp";
string databaseName = _databaseName;
Backup backup = new Backup();
backup.Action = BackupActionType.Database;
backup.Database = databaseName;
backup.Incremental = false;
backup.Initialize = true;
backup.LogTruncation = BackupTruncateLogType.Truncate;
string fileName = string.Format("{0}\\{1}.bak", testFolder, databaseName);
BackupDeviceItem backupItemDevice = new BackupDeviceItem(fileName, DeviceType.File);
backup.Devices.Add(backupItemDevice);
backup.PercentCompleteNotification = 10;
//backup.PercentComplete += backup_PercentComplete;
//backup.Complete += backup_Complete;
backup.SqlBackup(svr);
if (!VerifyBackup(svr))
{
//throw new Exception("Backup could not be verified.");
}
svr = null;
cn.Close();
}
}
public bool VerifyBackup(Server svr)
{
string testFolder = @"C:\temp";
string databaseName = _databaseName;
Restore restore = new Restore();
restore.Action = RestoreActionType.Database;
string fileName = string.Format("{0}\\{1}.bak", testFolder, databaseName);
BackupDeviceItem backupItemDevice = new BackupDeviceItem(fileName, DeviceType.File);
restore.Devices.AddDevice(fileName, DeviceType.File);
restore.Database = databaseName;
restore.PercentCompleteNotification = 10;
//restore.PercentComplete += new PercentCompleteEventHandler(ProgressEventHandler);
bool verified = restore.SqlVerify(svr);
return verified;
}
public void RestoreDatabase()
{
using (SqlConnection cn = new SqlConnection(connectionString))
{
ServerConnection svCon = new ServerConnection(cn);
Server svr = new Server(svCon);
cn.Open();
cn.ChangeDatabase("master");
string testFolder = @"C:\temp";
string databaseName = _databaseName;
Restore restore = new Restore();
restore.Action = RestoreActionType.Database;
string fileName = string.Format("{0}\\{1}.bak", testFolder, databaseName);
BackupDeviceItem backupItemDevice = new BackupDeviceItem(fileName, DeviceType.File);
restore.Devices.AddDevice(fileName, DeviceType.File);
restore.Database = databaseName;
restore.ReplaceDatabase = true;
restore.PercentCompleteNotification = 10;
//restore.PercentComplete += new PercentCompleteEventHandler(ProgressEventHandler);
svr.KillAllProcesses(databaseName);
restore.SqlRestore(svr);
svr = null;
cn.Close();
}
}