Calling the new SqlConnection () program

I have me at a standstill. I am not even trying to connect to the database. When this code hits the line where I create a new SqlConnection object, it just hangs there, without throwing an exception or anything else. I tried compiling it for 2.0. 3.5 and 4.0, and they all freeze. Of course, this works on both my car and yours. But I'm trying to run this code on an x64 server for Windows Server 2008, and it will not 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

+6
source share
6 answers

This is probably a faulty performance counter. I had this problem and I used Procmon to determine what happened. My .NET application crashed when it came to loading registry keys for the performance monitor ".NET Data Provider for SqlServer"

I unloaded the counter with the following command to make it work:

 unlodctr ".NET Data Provider for SqlServer" 
+6
source

Your installation must be broken. The code really does nothing vital, so there is no reason to hang it.

The SqlConnection constructor does the following:

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

Thus, it increments the variable, copies it to the property, calls the base constructor, removes the object from the finalizer queue, and copies the link. It's all.

The base constructor ( DbConnection ) does the following:

 protected DbConnection() { } 

So, there is nothing here that actually is actually related to the actual connection to the database. All this is done when you really open the connection.

Your program may also hang after the first call to Console.WriteLine and may not even reach the SqlConnection creating an SqlConnection object.

+2
source

Suggest two steps:

  • reset IIS to clear all connection pools. (Perhaps restart Windows?)
  • change the code to 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 application from this machine create any other SqlConnection objects?

This is obviously an environmental issue, as your published code will work on any other machine. I suspect that he went beyond the tipping point, and using will help protect against this in the future.

+1
source

I had the same problem and it started working after I 1. Changed the target structure from 4.0 to 3.5 and 2. changed the debug settings to x64 in visual studio.

0
source

I had the same problem after updating a number of nuget packages. For me, the solution was:

  1. In Visual Studio, go to "Tools" → "Options"
  2. Search "Web Projects" (in the "Projects and Solutions" section)
  3. Check the box next to “Use IIS Express 64-bit for Websites and Projects.”
0
source

Solution found! I had the same problem on a PC Many . Framework 4.5.2

Run this command as an administrator in the CMD:

 unlodctr ".NET Data Provider for SqlServer" 

You may need to enter it manually, since a copy of pasta does not work with quotation marks.

A source:

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

I know this thread is outdated, but maybe someone else will get this error. this is probably a faulty performance counter, which is the problem. I had this problem and I used Procmon to determine what happened. My .NET application hanged when it came to downloading registry keys for the performance monitor ".NET Data" Provider for SqlServer "I unloaded the counter with the following command: get it to work: C: Windowsinf> unlodctr" .NET Data Provider for SqlServer "

-2
source

Source: https://habr.com/ru/post/903243/


All Articles