简体   繁体   中英

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. 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:

  1. To use a single quote
  2. To set: 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. The syntax for BACKUP DATABASE requires, after DATABASE , not a file name, but a database name.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM