TSQL - 对象或列名丢失或为空。尝试将 sql 服务器数据库备份到磁盘时

TSQL - An object or column name is missing or empty .. When trying to backup sql server database to disk

My Connection String in which error occurs

<add name="SmartSales_local" connectionString="Server=.\SQLEXPRESS;Integrated Security=true;AttachDbFileName=|DataDirectory|\SmartSales.mdf;"
          providerName="System.Data.SqlClient" />
My Database Backup Code in C#
            con.ConnectionString = MyConnection();
            String receiptdir = @"D:\SmatSalesReports\Backup";
            if (!Directory.Exists(receiptdir))
            {
                Directory.CreateDirectory(receiptdir);
            }
            string dbname = con.Database.ToString();
            
            string backupdb = "BACKUP DATABASE [" + dbname + "] TO DISK='" + receiptdir + "\" + "SmartSales" + "-" + DateTime.Now.ToString("yyyy-MM-dd--HH-mm-ss") + ".bak'";
            
            
            con.Open();
            cm = new SqlCommand(backupdb, con);
            cm.CommandTimeout = 120;
            cm.ExecuteNonQuery();
            con.Close();

我在尝试将 sql 服务器数据库备份到磁盘时遇到此错误。数据库中的其他一切工作正常“inserting/Updating/deleting”。 当我尝试备份数据库时它给了我这个错误。 Screenshot of the error described

An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.

编辑后添加的新信息

My Connection String in which everything works fine

<add name="SmartSales" connectionString="Data Source=LAPTOP-GPJ5DM2V\SQLEXPRESS;Initial Catalog=SmartSales;Integrated Security=True"
      providerName="System.Data.SqlClient" />

当我使用这个连接字符串时一切正常。但是当安装了 SSMS 并在 SSMS 中打开数据库时它可以工作。

加上@squillman 在评论中建议的内容,您将在 backup database [] to disk='yourpath.bak'; 中得到确切的错误。在连接打开之前,连接数据库 属性 不会自动设置,因此您需要在打开后移动这些行:

con.Open();
string dbname = con.Database.ToString();
string backupdb = "BACKUP DATABASE [" + dbname + "] TO DISK='" + receiptdir + "\" + "SmartSales" + "-" + DateTime.Now.ToString("yyyy-MM-dd--HH-mm-ss") + ".bak'";

您遇到了很多问题:

  • 您的主要问题:您依靠 con.Database 的(实际上是空的)值来将数据库设置为备份。如果你真的需要从连接字符串中解析出来,你可以使用SqlConnectionStringBuilder,或者你可以先打开连接
  • 您正在使用 AttachDbFilename, which has been deprecated and is generally a bad idea
  • BACKUP命令完全可以参数化,你应该这样做
  • 您需要使用 using
  • 处理您的连接和命令对象
String receiptdir = @"D:\SmatSalesReports\Backup";
if (!Directory.Exists(receiptdir))
{
    Directory.CreateDirectory(receiptdir);
}
            
const string backupdb = @"
BACKUP DATABASE @dbname TO DISK = @location;
";
var location = Path.Combine(receiptdir, @"\SmartSales-" + DateTime.Now.ToString("yyyy-MM-dd--HH-mm-ss") + ".bak");

using(var con = new SqlConnection(MyConnection())
using(var cm = new SqlCommand(backupdb, con) { CommandTimeout = 120})
{
    con.Open();
    cm.Parameters.Add("@dbname", SqlDbType.NVarChar, 128) = con.Database.ToString();
    cm.Parameters.Add("@location", SqlDbType.NVarChar, 255) = location;
    cm.ExecuteNonQuery();
}