I have a problem. I try to create a database backup of a local database.
When I start the program in VS everything is fine because path to database is less than 128 characters. However, when I publish and install the app, path is more than 128 characters in length and I get that error.
On the Internet I found two solutions:
SET QUOTED_IDENTIFIER OFF
& SET ANSI_NULLS ON
but whichever combination I try, I cannot get it right.
Can anyone tell me how to get it right?
My code:
internal void CreateDbBackup(string DbBackupPath)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand GetDataFile = new SqlCommand();
GetDataFile.Connection = con;
GetDataFile.CommandText = "select physical_name from sys.database_files where type = 0";
con.Open();
string YourDataFile = (string)GetDataFile.ExecuteScalar();
con.Close();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = string.Format(@"BACKUP DATABASE [" + YourDataFile + "] TO DISK = N'{0}' WITH INIT , NOUNLOAD , NOSKIP , STATS = 10, NOFORMAT", DbBackupPath);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
答案 0 :(得分:0)
当您选择Optional.ofNullable(toCheck)
.filter(e ->
{
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if ((Character.isWhitespace(str.charAt(i)) == false)) {
return false;
}
}
return true;
})
.isPresent()
时,会得到一堆文件名。在select physical_name from sys.database_files
之后,BACKUP DATABASE
的语法不是文件名,而是数据库名。
您的文件名包含路径,这会导致总路径太长而无法将其解释为数据库名称。如果路径较长,则会出现错误,即找不到具有该名称的数据库。这会更直接地告诉你你做错了什么。
您应该已经从连接字符串中获取了数据库名称。