0

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 to 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)

1 Answer 1

2

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>();  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.