c# - Getting SQL Connection fragmentation, different way to connect to DB's -


we have multiple db servers. on 1 of servers have master config table holds instructions db server , database name agency supposed use.

currently each database has 2 connections on them, if they're not being used (which fixing). however, we're trying find way make our connections not on place, , relieve of stress on our db servers.

after lot of research found articles saying connections central location, , change database we're using through sqlconnection object. seems bit roundabout, work.

so i'm wondering others in situation?

the current path is:

-user logs in -system access configtable find out database user going connect to. -system loads agency connection settings memory (session) user. -every request directly hits users database.

is there more efficient way of doing this?

open connections late, , close them early.

for example:

string result; using (var con = new sqlconnection(...)) {     con.open();     var com = con.createcommand();     com.commandtext = "select 'hello world'";     result = com.executescalar(); } 

the windows os make sure efficiently pool , reuse connections. , since you're using connections when need them, there no idle connections lying around.

edit: windows caches connection strings literally same, if use initial catalog=<dbname> in connection string, hurt performance requiring 500+ "connection pools" 1 server.

so if have 4 servers lot of databases, make sure use 4 connection strings. after connecting, switch database with:

com.commandtext = "use <dbname>"; com.executenonquery(); 

or query three-part name like:

select * <dbname>.dbo.yourtable 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -