0

I need every thread to connect to one database and execute some query and maybe some thread in one time can execute the query.

I create a static class for connecting to SQL Server database with ADO.NET:

public static class ADOFire
{
    static System.Data.SqlClient.SqlConnection Cnn = new System.Data.SqlClient.SqlConnection();

    public static string CnnString { get; set; }

    public static void CreateConnection()
    {
        if (Cnn.State == ConnectionState.Open)
            return;

        Cnn.ConnectionString = CnnString = ConfigurationManager.ConnectionStrings["CnnString"].ToString();

        if (Cnn.State != System.Data.ConnectionState.Open) 
            Cnn.Open();
    }

    public static System.Data.DataTable GetTable(System.Data.SqlClient.SqlCommand Com, System.Data.SqlClient.SqlDataAdapter Ada, string ComText)
    {
        CreateConnection();
        Com.Connection = Cnn;
        Ada.SelectCommand = Com;

        try
        {
            System.Data.DataTable T = new System.Data.DataTable();
            Com.CommandText = ComText;
            Ada.Fill(T);
            return T;
        }
        catch { return null; }
    }
}

And in here in each thread I call static function like this:

System.Data.SqlClient.SqlCommand Com = new System.Data.SqlClient.SqlCommand();
System.Data.SqlClient.SqlDataAdapter Ada = new System.Data.SqlClient.SqlDataAdapter();
Datatable dt =  ADOFire.GetTable(Com, Ada, "Some Query 'select * from x'");

Based on this link, doesn't make much difference between open a new connection or use from existing connection

Whenever a user calls Open on a connection, the pooler looks for an available connection in the pool. If a pooled connection is available, it returns it to the caller instead of opening a new connection. When the application calls Close on the connection, the pooler returns it to the pooled set of active connections instead of closing it. Once the connection is returned to the pool, it is ready to be reused on the next Open call

My questions are:

  1. Can a connection serve different threads at the same time? (one connection for all)

  2. Isn't the problem of data clutter due to the static function?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • possible this would help : https://stackoverflow.com/questions/23942364/net-the-sqlconnection-object-and-multi-threading – iSR5 Apr 04 '20 at 07:35
  • 1
    Re-read the StackOverflow question and answer you posted. Static methods of SqlConnection are thread-safe - e.g.: you can have many threads calling SqlConnection.ChangePassword() and SqlConnection.ClearAllPools() - but the instance methods are not guaranteed to be thread-safe. Don't try to share one SqlConnection between threads, obtain a new SqlConnection via Connection Pools for each thread that needs one. – AlwaysLearning Apr 04 '20 at 07:47

1 Answers1

1
  1. No, ADO.NET does not two threads accessing the same.conection at the same time
  2. Yes, but it isn't the statix method that is a problem - it is the static connection field that is a huge problem

What you should do is have the static method create (new) and return the connection to the caller, and remove the field completely. Typical usage:

using (var conn = CreateConnection()) {
   //... Use it!
}

I also have grave concerns about: - why you're passing in a command and command text and adapter - the lack of parameters; suggests a huge security problem (SQL injection) - the use of data table (which is almost never the most appropriate tool)

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • -Gravell tnx, if I change my GetTable method to your solution, when many threads call this method at the same time Given that it is static, No problem? – Pedram-Faraji Apr 04 '20 at 08:40