0

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();
    }
}

1 Answer 1

1

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

1 Comment

Can I cannot with a IP adress?

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.