Question

So I've been using LINQ to SQL (dbml) for my C# projects for a bit now as it makes integrating SQL pretty easy for me.. the only thing is everything is so automated that I don't know how to actually edit things.

Using LINQ to SQL makes it so that it automatically connects to the database IP with user and pw in the connection string it creates in the config file but say the database isn't up.. or i want to change IPs.. the app freezes on start up.

How can I test for connectivity before having it automatically connect? I can't seem to find where in the code it does this exactly.

Was it helpful?

Solution

Your settings for the Entity, I am rusty on Linq to SQL but think it is similar, are in the App.Config on the project where you CREATED you Linq to SQL model. They are similar to this(using Entity Model):

    < connectionStrings>
    < add name="Example" connectionString="metadata=res://*/ExampleDB.csdl|res://*/ExampleDB.ssdl|res://*/ExampleDB.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=ACTUALSERVER;initial catalog=ACTUALDATABASE;persist security info=True;user id=(SET USER HERE);password=(SET PASSWORD HERE);multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    < /connectionStrings>

The important part for connection is the connection string though:

&quot;data source=ACTUALSERVER;initial catalog=ACTUALDATABASE;persist security info=True;user id=(SET USER HERE);password=(SET PASSWORD HERE);

You can try to alter this to different connection strings if you want, and keeping the metadata the same. For differenent environments like "Dev", "QA", "UAT", "PROD" etc... And then copy and paste the connection block starting with the '< add name="' and till the end portion of 'providerName='(thing)' />. Then just alter the connection string and give it a different name. Then you could have the calling code use a different context or connection like:

using(MyContext context = new MyContext())
{
context.Connection = (new connection)

 (your data return method)
}

You may be able to do this directly in the constructor of your context(MyContext). Cannot recall. Generally I usually set up multiple configs "DEV", "QA", "UAT", "PROD" and have them build for different environments for a service. You can build a connection string manually but dynamic connection strings can be a pain as you as a developer now need to ensure a few things:

  1. The model of a Dev environment EXACTLY matches another with it's objects in your model. If not goodbye code return.
  2. If you are invoking a set user that you are ensuring that your user has rights on certain environments
  3. That you are not putting the integrity of your code at risk by showing too much publicly for user settings.

OTHER TIPS

(All assuming you're using Sql Server)

Expanding on djangojazz's answer.

public void TestDbConnection()
{
  Task.Factory.StartNew(() =>
  {
    bool isAvailable = false;
    using(MyContext context = new MyContext())
    {
      var connection = ((IObjectContext)context).Connection as SqlConnection;

      try
      {
        connection.Open();
        isAvailble = true;
      }
      catch (Exception ex)
      {
      }
    }
    TestDbConnectionComplete(isAvailable);
  });
}

public void TestDbConnectionComplete(bool isAvailable)
{
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top