Click here to Skip to main content
15,888,321 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I work on asp.net core 2.2 I use static SQL connection I already treat with multi database

at run time and exist on multi server so I need to know which is best to my case static

connection or public connection .
when I use connection i create it static I use it as below :

dbConnection = (SqlConnection)InitializeConnection();


suppose Now I need to change connection at run time to get data from another server have another database ?
so Are static connection is good or not ?

What I have tried:

startup.cs
 public void ConfigureServices(IServiceCollection services)
        {
            
          
            services.AddMvc();
       
            GlobalVariables.con = Microsoft
     .Extensions
     .Configuration
     .ConfigurationExtensions
     .GetConnectionString(this.Configuration, "DashBoardSQLConnection");
}


static IDbConnection InitializeConnection()
    {
        switch (_Provider)
        {
            case Providers.SQLServer:
               
                return new SqlConnection(GlobalVariables.con);
             


        }
        return new SqlConnection();
    }
Posted
Updated 2-Jun-20 4:51am
v2
Comments
Maciej Los 2-Jun-20 10:50am    
Best - in what aspect?

1 solution

That isn't a static connection - with a static connection there is only one per application, and you don't want that.

What you should be doing is creating your connection, opening it, using it, and disposing of it when you are finished - the easiest way to do that is with a using block:
C#
using (SqlConnection con = new SqlConnection(strConnect)
   {
   con.Open();
   ... use it here ...
   }
The system will then Close and Dispose the connection object for you automatically when it's no longer available. You should also be doing this for SqlCommands, SqlDataReaders, SqlDataAdapters, and so forth.

This prevents some of the nastier problems that crop up later, such as a DataReader being accidentally being left "open" on a connection - which prevents anything else being done with it later.

If you need a connectiuon to another server / DB, you just create that Connection when you needit inside another using block and let the system handle it.
 
Share this answer
 
Comments
ahmed_sa 2-Jun-20 10:58am    
so my question are using connection as I used is correct or what ?
OriginalGriff 2-Jun-20 11:22am    
Read what I said...
ahmed_sa 2-Jun-20 11:27am    
I read it but I need ask are my work is wrong and if I change my connection above to get query from another server what issue I will solve
please help me
ahmed_sa 2-Jun-20 11:47am    
I got what you mean you mean then when I make return new SQL connection this meaning
this is not static because I take new object my question are connection with this way is good or bad
Richard MacCutchan 2-Jun-20 11:51am    
The good way is to do what OriginalGriff told you. All other ways are bad.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900