开发者

A network-related or instance-specific error

开发者 https://www.devze.com 2022-12-25 16:03 出处:网络
I am trying to access database from a Class Library Project. I have connection string defined in App.config file like this

I am trying to access database from a Class Library Project. I have connection string defined in App.config file like this

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

I am trying to access this conenction string via code

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

Error

A network-related or instance-specific error occurred while establishing a connection 开发者_StackOverflowto SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)


You need to actually use the connection string, and not just the name of it. Try something like:

string conString = ConfigurationManager
                      .ConnectionStrings["EditRegionConnectionString"]
                      .ConnectionString;

Table<cont> table1 = new DataContext(conString).GetTable<cont>();  
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号