c# - Types that own disposable fields should be disposable. how to solve this warning? -


i tried using run code analysis option in visualstudio 2012, result of got warning

ca1001  types own disposable fields should disposable implement idisposable on 'dbconnectivity'  because creates members of following idisposable types: 'sqlconnection', 'sqlcommand'. 

i referred question in so, couldn't catch point regarding idisposable , following class, responsible warning.

class dbconnectivity     {         public sqlconnection connection = null;         public sqlcommand command = null;         public sqldatareader datareader = null;         public string connectionstring = null;         public list<mastertableattributes> mastertablelist;         public dbconnectivity()         {             connectionstring = configurationmanager.connectionstrings["master"].connectionstring;             connection = new sqlconnection(connectionstring.tostring());              //-----master table results              connection.open();             string masterselectquery = "select * master_table";             command = new sqlcommand(masterselectquery, connection);             datareader = command.executereader();             mastertablelist = new list<mastertableattributes>();              while (datareader.read())             {                 mastertableattributes mastertableattribute = new mastertableattributes()                 {                     fileid = convert.toint32(datareader["id"]),                     filename = convert.tostring(datareader["filename"]),                     frequency = convert.tostring(datareader["frequency"]),                     scheduledtime = convert.tostring(datareader["scheduled_time"])                 };                 mastertablelist.add(mastertableattribute);             }             datareader.close();             connection.close();         }     } 

i confused in implementing idisposable. appreciated?

i agree compiler - need dispose fields here, or (as noted) - not make them fields in first place:

class dbconnectivity : idisposable // caveat! read below first {     public void dispose() {         if(connection != null) { connection.dispose(); connection = null; }         if(command != null) { command.dispose(); command = null; }         if(datareader != null) { datareader.dispose(); datareader = null; }     } 

note use type via using(...)


however! looks static method more appropriate:

static class dbconnectivity {     public static list<mastertableattributes> getmastertablelist()     {         var connectionstring = configurationmanager.connectionstrings["master"].connectionstring;         using(var connection = new sqlconnection(connectionstring))         {             connection.open();             const string masterselectquery = "select * master_table";             using(var command = new sqlcommand(masterselectquery, connection))             using(var datareader = command.executereader())             {                 var mastertablelist = new list<mastertableattributes>();                  while (datareader.read())                 {                     mastertableattributes mastertableattribute = new mastertableattributes()                     {                         fileid = convert.toint32(datareader["id"]),                         filename = convert.tostring(datareader["filename"]),                         frequency = convert.tostring(datareader["frequency"]),                         scheduledtime = convert.tostring(datareader["scheduled_time"])                     };                     mastertablelist.add(mastertableattribute);                 }                 return mastertablelist;             }         }     } } 

or perhaps simpler tool "dapper":

static class dbconnectivity {     public static list<mastertableattributes> getmastertablelist()     {         var connectionstring = configurationmanager.connectionstrings["master"].connectionstring;         using(var connection = new sqlconnection(connectionstring))         {             connection.open();             const string sql = "select id [fileid], filename, frequency, scheduled_time [scheduledtime] master_table";             return connection.query<mastertableattributes>(sql).tolist();         }     } } 

Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -