[英]The identifier that starts with … is too long. Maximum length is 128
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. 当我在VS中启动程序时,一切都很好,因为到数据库的路径少于128个字符。 However, when I publish and install the app, path is more than 128 characters in length and I get that error.
但是,当我发布并安装该应用程序时,路径的长度超过128个字符,并且出现该错误。
On the Internet I found two solutions: 在Internet上,我找到了两种解决方案:
SET QUOTED_IDENTIFIER OFF
& SET ANSI_NULLS ON
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();
}
When you select select physical_name from sys.database_files
, you get a bunch of file names. 当您
select physical_name from sys.database_files
选择select physical_name from sys.database_files
,会得到一堆文件名。 The syntax for BACKUP DATABASE
requires, after DATABASE
, not a file name, but a database name. 对于语法
BACKUP DATABASE
要求,经过DATABASE
,而不是一个文件名,但一个数据库名称。
Your file names include paths, and that causes the total path to be so long that it cannot be interpreted as a database name. 您的文件名包含路径,这会导致总路径太长而无法将其解释为数据库名称。 If the path were less long, you'd instead get the error that no database with that name could be found.
如果路径的长度较短,则会收到错误消息,即找不到该名称的数据库。 This would tell you more directly what you're doing wrong.
这将更直接地告诉您您在做什么错。
You should have the database name already from your connection string. 您应该已经从连接字符串中获得了数据库名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.