calling new SqlConnection() hangs program

家住魔仙堡 提交于 2019-12-04 06:17:08
Jonas Lindau

It's probably a broken performance counter which is the issue. I had this problem and I used Procmon to detect what happened. My .NET application hanged when it came to loading the registry keys for the performance monitor ".NET Data Provider for SqlServer"

I unloaded the counter with the following command to get it working:

unlodctr ".NET Data Provider for SqlServer"

Your installation have to be broken. The code doesn't really do anything vital at all, so there is no reason to hang there.

The SqlConnection constructor does this:

public SqlConnection() {
  this.ObjectID = Interlocked.Increment(ref SqlConnection._objectTypeCount);
  base();
  GC.SuppressFinalize(this);
  this._innerConnection = DbConnectionClosedNeverOpened.SingletonInstance;
}

So, it increases a variable, copies it into a property, calls the base constructor, removes the object from the finaliser queue, and copies a reference. That's all.

The base (DbConnection) constructor does this:

protected DbConnection() {
}

So, there is nothing in here that actually does anything at all related to an actual database connection. All that is done when you actually open a connection.

Your program might just as well be hanging after the first Console.WriteLine call, and not even get as far as creating the SqlConnection object.

Suggest 2 steps:

  • reset IIS to clear any connection pools. (Perhaps restart Windows?)
  • change the code to have a using statement:
  public static void Main(string[] args) { 
    Console.WriteLine("start"); 
    using (SqlConnection conn = new SqlConnection())
    {
          Console.WriteLine("middle");              
    }
    Console.WriteLine("finish");             
} 

Can any other app from that machine make any other SqlConnection objects?

It's obviously an environmental problem, as your posted code will work on any other machine. Suspect that it's gone beyond some tipping point, and the using will help defend against this in the future.

I had the same problem and it began to work after I 1. Changed the target framework from 4.0 to 3.5, and 2. changed the debug settings to x64 in visual studio.

I had the exact same issue after upgrading a number of nuget packages. For me, the solution was:

  1. In Visual Studio go to "Tools" --> "Options"
  2. Search for "Web Projects" (under Projects and Solutions)
  3. Check the "Use the 64 bit version of IIS Express for web sites and projects
Ryan Susman

Solution found! I had the same problem on many PCs. Framework 4.5.2

Run this command as admin in CMD:

unlodctr ".NET Data Provider for SqlServer"

You may need to type it by hand as copy pasta doesn't work well with quotes.

Source:

http://askproblem.com/question/calling-new-sqlconnection-hangs-program/

I know this thread is old, but maybe someone else will get this error. It’s probably a broken performance counter which is the issue. I had this problem and I used Procmon to detect what happened. My .NET application hanged when it >came to loading the registry keys for the performance monitor “.NET Data Provider for SqlServer” I unloaded the counter with the following command to get it working: C: Windowsinf>unlodctr “.NET Data Provider for SqlServer”

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!