gpt4 book ai didi

c# - SqlCommand 备份数据库

转载 作者:搜寻专家 更新时间:2023-10-30 20:05:22 25 4
gpt4 key购买 nike

我有一个 c# winforms 应用程序(.net 2 框架)。我需要从我的应用程序备份数据库。我试图通过异步执行 SqlCommand 来做到这一点。代码执行无一异常(exception),但我没有在我的目的地获得 .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

最佳答案

在您的 SQL 备份行中,您似乎在备份文件路径的开头缺少单引号。

  using (SqlCommand cmd = new SqlCommand("BACKUP DATABASE " + db + " TO DISK='" + location + "\\" + ConfigurationManager.AppSettings.Get("DataBaseBackupsFolderName") + "\\" +db + ".bak'", sqlConnection1)) 

关于c# - SqlCommand 备份数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11326874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com