c# - the perfect way to connect to database? -


public class sqlhelper { public sqlhelper() { } public static sqlconnection getconnection() {     sqlconnection conn = new sqlconnection();     conn.connectionstring = @"data source=.\sqlexpress;attachdbfilename=" +     system.web.httpcontext.current.server.mappath(@"~\app_data\learn.mdf") + ";integrated security=true;user instance=true";     return conn; } public static sqldatareader executereader(string sql) {     sqlconnection con = getconnection();     con.open();     sqlcommand cmd = new sqlcommand(sql, con);     sqldatareader dr = null;     try     {         dr = cmd.executereader(commandbehavior.closeconnection);     }     catch     {         con.close();         return null;     }     return dr; } public static object executescalar(string sql) {     sqlconnection con = getconnection();     con.open();     sqlcommand cmd = new sqlcommand(sql, con);     object val = null;     try     {         val = cmd.executescalar();     }     catch     {         con.close();         return null;     }         {         con.close();     }     return val;  } public static dataset executedataset(string sql) {     sqlconnection con = getconnection();     sqlcommand cmd = new sqlcommand(sql, con);     dataset ds = new dataset();     sqldataadapter adapt = new sqldataadapter(cmd);     try     {         adapt.fill(ds);     }     catch     {         con.close();     }     return ds; } public static void executenonquery(string sql) {     sqlconnection con = getconnection();     con.open();     sqlcommand cmd = new sqlcommand(sql, con);     try     {         cmd.executenonquery();     }         {         con.close();     } } } 

this class use implement every access database . think way connection database little bit overblown cause have hit connect function every time need . other users going same kills performance.
perfect way connect database - , stay connected if better . note use database in many pages!
thanks

first, should using "using" statements ensure ado.net objects disposed of in event of failure:

public static void executenonquery(string sql)  {          using(var con = getconnection())     {         con.open();              using(var cmd = new sqlcommand(sql, con))         {                      cmd.executenonquery();              }          } } 

however, having said that, don't see problem approach. advantage connections, commands, adapters , whatnot disposed of every time execute bit of sql. if make single static sqlconnection instance, you'd escalate chances connection in use (when, example, iterating on contents of sqldatareader).

if concerned it, provide overloads take connection parameter:

public static void executenonquery(string sql, sqlconnection connection)  {          using(var cmd = new sqlcommand(sql, con))     {                  cmd.executenonquery();          }      } 

this way, callers can either execute bit of sql doesn't require multiple calls, or can call getconnectionmethod obtain connection, , pass multiple calls.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -