【问题标题】:A network-related or instance-specific error与网络相关或特定于实例的错误
【发布时间】:2010-04-07 02:41:49
【问题描述】:

我正在尝试从类库项目访问数据库。我在 App.config 文件中定义了这样的连接字符串

add name="EditRegionConnectionString" connectionString="Data Source=.\SQLExpress;Initial Catalog=testdb;Integrated Security=True"
            providerName="System.Data.SqlClient" 

我正在尝试通过代码访问此连接字符串

Table<cont> table1 = new DataContext("EditRegionConnectionString").GetTable<cont>();  
var t1 = from t in table1 select t;  
//i am getting error here  
t1.FirstOrDefault();

错误

与网络相关或特定于实例的 建立时发生错误 连接到 SQL Server。服务器 未找到或无法访问。 验证实例名称是否为 正确,并且 SQL Server 是 配置为允许远程 连接。 (提供者:命名管道 提供者,错误:40 - 无法打开 连接到 SQL Server)

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您需要实际使用连接字符串,而不仅仅是它的名称。尝试类似:

    string conString = ConfigurationManager
                          .ConnectionStrings["EditRegionConnectionString"]
                          .ConnectionString;
    
    Table<cont> table1 = new DataContext(conString).GetTable<cont>();  
    

    【讨论】:

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