【问题标题】:Keyword not supported: '@"data source'.'不支持关键字:'@"数据源'。'
【发布时间】:2018-04-20 03:15:33
【问题描述】:

我将连接字符串的路径插入到数据库(A)。用于连接数据库(B)的路径。当我从数据库(A)中选择该路径并尝试连接数据库(B)时。这是提示信息显示

不支持关键字:'@"数据源'。'

这是数据库(A)中的连接字符串:

@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\it_staff\source\repos\MonitorProduction_Client\MonitorProduction_Client\App_Data\monitorclientdb.mdf;Integrated Security=True"

connect_db.conn.Close();
connect_db.conn.Open();
SqlCommand cmd_select_connect_string = new SqlCommand("select board_connect_string from[board] where board_id='" + board_id_[0] + "'", connect_db.conn);
        if (cmd_select_connect_string.ExecuteNonQuery() != 0)
              {
                    SqlDataReader rd = cmd_select_connect_string.ExecuteReader();
                    while (rd.Read())
                    {
                        var board_connect_string = rd[0];
                        connect_db.connb=new SqlConnection(board_connect_string.ToString());  
                    }
                }

【问题讨论】:

  • 您需要共享引发错误的代码。
  • 您是否在 web.config 中设置了该连接字符串?这样做可能会导致配置解析器错误,因为您在连接字符串中间添加换行符并在打开双引号之前使用@,这是不允许的。
  • 但是当我改变 board_connect_string.ToString() 是 (@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\it_staff\source\repos\MonitorProduction_Client\MonitorProduction_Client\App_Data\monitorclientdb .mdf;Integrated Security=True") 可以连接
  • 为什么在SELECT 查询中同时使用ExecuteNonQueryExecuteReader?做ExecuteReader之后可以直接使用HasRows属性,这里不需要使用ExecuteNonQuery。还要检查连接字符串是否包含错误的换行符或错误的providerName
  • 只检查是否可以查询。但是谢谢你给我指导。但是我的问题还是没有解决。

标签: c# sql asp.net-mvc


【解决方案1】:

只需从数据库中的 connect_string 中删除 @ 和 "

数据源=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\it_staff\source\repos\MonitorProduction_Client\MonitorProduction_Client\App_Data\monitorclientdb.mdf;Integrated Security=True

【讨论】:

    猜你喜欢
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多