【发布时间】:2012-07-04 10:05:56
【问题描述】:
我有一个 c# winforms 应用程序(.net 2 框架)。 我需要从我的应用程序中备份数据库。 我试图通过异步执行 SqlCommand 来做到这一点。 代码无一例外地执行,但我没有在目的地获得 .bak 文件...
这是代码:
#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 这是命令 - BACKUP DATABASE DB_Tags_TestingTagBurner TO DISK=D:\New folder\DataBaseBackups\DB_Tags_TestingTagBurner.bak'
标签: c# sql database sql-server-2008 tsql