使用 SQL 服务器连接字符串时出错

Getting an error using SQL Server connection string

public void DataGrid_Data()
{
    // 2 second delay before loading DataGrid
    var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(2) };
    timer.Start();
    timer.Tick += (sender, args) =>
        {
            timer.Stop();

            // Attempt to connect to SQL Server database and populate DataGrid with database tables. 
            try
            {
                string connectionString = (@"Data Source=ComputerName\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;");
                SqlConnection connection = new SqlConnection(connectionString);

                SqlCommand cmd = new SqlCommand("SELECT `hb_disputes`.`DSP_ID`, `hb_disputes`.`ACCOUNT`, `hb_disputes`.`CUST_NAME`,`hb_disputes`.`PREM_ADDR`, `hb_status`.`Status`, `hb_disputes`.`OPENED`, `hb_disputes`.`DEADLINE`, `hb_rpttype`.`ReportType`, `hb_ratetype`.`Rate Type`FROM `hb_disputes` LEFT JOIN `hb_status` ON `hb_disputes`.`STATUS` = `hb_status`.`STSID` LEFT JOIN `hb_rpttype` ON `hb_disputes`.`RPTTYPE` = `hb_rpttype`.`RPTID` LEFT JOIN `hb_ratetype` ON `hb_disputes`.`REV_CLS` = `hb_ratetype`.`RTID`; ", connection);
                connection.Open();

                DataTable dt = new DataTable();
                dt.Load(cmd.ExecuteReader());

                connection.Close();

                dtGrid.DataContext = dt;
            }
            catch
            {
                MessageBox.Show("Database connection is not available at this time. Please contact your database administrator ");
            }
        };
}

我一直在为我的应用程序使用 MYSQL,我决定切换到 SQL 服务器。我在 SQL 服务器中重建了数据库并将其连接到 Visual Studio,这为我提供了我的连接字符串。但是,由于计算机名称和 SQLEXPRESS 之间有一个“\”,我得到一个

Unrecognized escape sequence

错误。我试过使用“@”和“\”,但我仍然无法连接到数据库。我也只是简单地将 MySqlCommand 替换为 SqlCommand

由此

   string connectionString = (@"Data Source=ComputerName\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;");

   string connectionString = ("Data Source=ComputerName\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;");

如果有帮助请告诉我

2 个反斜杠

欢迎访问该站点...从我的 C#/SQL-服务器工作,我的连接字符串不同,例如

Server=myServerName\theInstanceName; Database=myDataBase; Trusted_Connection=yes;

与您的相比

Data Source=ComputerName\SQLEXPRESS;Initial Catalog=CustomerRelations;Integrated Security=True;

不确定这是否是您的连接问题所在。