简体   繁体   中英

A static variable in static class asp.net mvc

I have a static class Data:

 public static class Data
    {
        public static SqlConnection connexion;


        public static bool  Connect()
        {
                System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
                builder["Initial Catalog"] = "Upload";
                builder["Data Source"] = "base";
                builder["integrated Security"] = true;
                string connexionString = builder.ConnectionString;
                connexion = new SqlConnection(connexionString);
                try { connexion.Open(); return true; }
                catch { return false; }

        }
        public static  void Disconnect()
        {
            if (connexion != null) connexion.Close();
            connexion = null;
        }

 }

in an the action Home :

  public ActionResult Home()
        {
            Data.Connect();
            if (CompteModels.Connected)
            {
                ArrayList model = new ArrayList();

                ClientModels clients = new ClientModels();
                model.AddRange(clients.Client_List());

                AdminModels admins = new AdminModels();
                model.AddRange(admins.Admin_List());


                return View(model);
            }

            else return RedirectToAction("Login", "Account");
}

the client class:

 public List<ClientModels> Client_List()
        {
            List<ClientModels> l = new List<ClientModels>();

            using (Data.connexion)
            {

                string queryString = @"select Login, Password, Mail, Name, Tentatives from Compte where User_type_id in ( select Id from User_type where Fonction = 'Client')";

                SqlCommand command = new SqlCommand(queryString, Data.connexion);


                try
                {
                    SqlDataReader reader = command.ExecuteReader();


                    do
                    {

                        while (reader.Read())
                        {
                            ClientModels admin = new ClientModels { Login = reader.GetString(0), Password = reader.GetString(1), Mail = reader.GetString(2), Name = reader.GetString(3), Tentatives = reader.GetInt32(4)  };
                            l.Add(admin);


                        }
                    } while (reader.NextResult());

                    return l;



                }
                catch { return null; }

            }

For the function AdminList , the implementation is the same like Client_List but in the class Admin .

The problem is in the static variable connexion : in the first function Client_List its value is correct and i get the list of the clients , but it's become null in the second function despite it is a static variable in static class !!!

What is the reason of this alteration ? How can i fix it?

You're either setting connexion to null somewhere or not initializing it before you use it.

Most likely, one class is calling Disconnect which sets connexion to null, while another class assumes it's not null and tries to use it.

As mentioned in the comments, keeping a static reference to a resource like a SqlConnection is not a good idea. If you want to re-use code you can create a static function that returns a new SqlConnection instance and make the connection string static, but having a static reference to a connection that's share across the entire web site will give you more problems that it's worth (as you're already seeing).

One way to do it in a static function would be:

public static SqlConnection GetConnection()
{
    System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
    builder["Initial Catalog"] = "Upload";
    builder["Data Source"] = "base";
    builder["integrated Security"] = true;
    string connexionString = builder.ConnectionString;
    connexion = new SqlConnection(connexionString);

    return connexion; 
}

Your client code would then look something like:

using (SqlConnection conn = Data.GetConnection())

This is a bad idea, as others have already mentioned.

You should simply create and open a connection when needed, and dispose it afterwards:

using (SqlConnection connection = new SqlConnection(connectionString)) 
{    
    connection.Open();
    // ... now use it
}

Of course, there are other patterns, that hide this mechanism, but that might be overkill in your case.

Your using statement disposes of the connexion using the IDisposable interface

 public List<ClientModels> Client_List()
 {
      List<ClientModels> l = new List<ClientModels>();

      using (Data.connexion) <--- here
}

Change this to create a new connection

 public List<ClientModels> Client_List()
 {
      List<ClientModels> l = new List<ClientModels>();

      using (var connection = Data.CreateConnexion())
}

similar to this

 public static class Data
 {
     public static SqlConnection CreateConnection()
     {
        System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
            builder["Initial Catalog"] = "Upload";
            builder["Data Source"] = "base";
            builder["integrated Security"] = true;
            string connexionString = builder.ConnectionString;
            var connexion = new SqlConnection(connexionString);
            connexion.Open(); 
            return connexion; 

     }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM