问题
This one has me stumped. I'm not even trying to connect to a database. When this code gets to the line where I instantiate a new SqlConnection object, it just hangs there, not throwing an exception or anything. I've tried compiling it for 2.0. 3.5 and 4.0, and they all hang. Of course it works on my machine and yours, too. But I'm trying to run this code on a Windows Server 2008 x64 server, and it won't budge.
// example.cs
using System;
using System.Data;
using System.Data.SqlClient;
public class MainClass {
public static void Main(string[] args) {
Console.WriteLine("start");
SqlConnection conn = new SqlConnection(); // hangs here
Console.WriteLine("finish"); // never makes it here.
}
}
compilation (2.0): c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe example.cs
回答1:
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"
回答2:
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.
回答3:
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.
回答4:
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.
回答5:
I had the exact same issue after upgrading a number of nuget packages. For me, the solution was:
- In Visual Studio go to "Tools" --> "Options"
- Search for "Web Projects" (under Projects and Solutions)
- Check the "Use the 64 bit version of IIS Express for web sites and projects
回答6:
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”
来源:https://stackoverflow.com/questions/8408236/calling-new-sqlconnection-hangs-program