我有一个C#winforms应用程序(.net 2框架)。 我需要从我的应用程序备份数据库。 我想通过异步执行一个SqlCommand来做到这一点。 的代码并且无例外执行,但我没有得到我的目的地.bak文件...SqlCommand备份数据库
这是代码:
#region backup DB using T-SQL command
string connString = "Data Source=" + ConfigurationManager.AppSettings.Get("localhost_SQLEXPRESS") + ";Initial Catalog=" + db + ";UserID=" + ConfigurationManager.AppSettings.Get("user") + ";Password=" + ConfigurationManager.AppSettings.Get("password");
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connString);
builder.AsynchronousProcessing = true;
using (SqlConnection sqlConnection1 = new SqlConnection(builder.ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("BACKUP DATABASE " + db + " TO DISK=" + location + "\\" + ConfigurationManager.AppSettings.Get("DataBaseBackupsFolderName") + "\\" + db + ".bak'", sqlConnection1))
{
sqlConnection1.Open();
IAsyncResult result = cmd.BeginExecuteNonQuery();
while (!result.IsCompleted)
{
Thread.Sleep(100);
}
}
}
#endregion
你能穿上sqlConnection1.Open(断点),并检查cmd.CommandText的值(在特别是文件名路径) – Steve
只是为了确保...备份存储在服务器上,而不是在发出该命令的计算机上(除非两者是同一台计算机)。 –
备份在计算机上... –