Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a API setup doing all of calls to the database, but there is one occasion in a website that cannot make a call api call for some reason. And only thing I can think of is just doing a quick call to the database in the website itself. I just want to run a quick linq query against my database. What is the proper hard coded connectionstring settings, and how can I plug in my GlobalDAtacontext as well. Thanks for any help.

//Example of what I would like to do
using (var conn = new SqlConnection("WHAT IS THE PROPER CONNECTIONSTRING FORMAT")
{
    conn.open();
    using (var context = new dbcontext())
    {
          var user = (from x in context.users where x.ID == userid).FirstOrDefault();
    }
}
share|improve this question

1 Answer 1

up vote 1 down vote accepted

The connection string depends on your database configuration. What kind of configuration you are using to connect to database, whether you are using windows auth or user/pass auth. Default database comes into play.

Various Sql Server Connection Strings connection are given here. You can choose one that suits your need.

You need to pass the SqlConnection object to the DbContext constructor while initializing it.

using (var conn = new SqlConnection("Server=myServerAddress;Database=myDataBase;User Id=myUsername; Password=myPassword;")
{
    conn.open();
    using (var context = new dbcontext(conn, true /* it can be false too, */))
    {
        var user = (from x in context.users where x.ID == userid).FirstOrDefault();
    }
}
share|improve this answer
    
Can I cannot with a IP adress? –  user516883 Jul 13 '13 at 5:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.