Consuming sqlcompact connection with using statement
-
hi, I use below code to create/consume and dispose sql connections.But as there are more than twenty methods/functions that I use it, same code block repeats. So, is it goog practice? Your valuable comments are welcomed.
private void islemKayitlariniYukle(string select)
{
using (SqlCeConnection sqlCEBaglantisi = new SqlCeConnection(Properties.Settings.Default.sqlBag))
using (SqlCeDataAdapter sqlAdaptor = new SqlCeDataAdapter(select, sqlCEBaglantisi))
using (DataTable dtTablo = new DataTable())
{
sqlCEBaglantisi.Open();
sqlAdaptor.Fill(dtTablo);SqlCeCommand islemTipleriniAl = new SqlCeCommand("Select IslemTipi From IslemKaydi Group By IslemTipi", sqlCEBaglantisi); using (SqlCeDataReader oku1 = islemTipleriniAl.ExecuteReader()) { while (oku1.Read()) { cmbIGIslemTipiSec.Items.Add(oku1.GetString(0).Trim()); } }
-
hi, I use below code to create/consume and dispose sql connections.But as there are more than twenty methods/functions that I use it, same code block repeats. So, is it goog practice? Your valuable comments are welcomed.
private void islemKayitlariniYukle(string select)
{
using (SqlCeConnection sqlCEBaglantisi = new SqlCeConnection(Properties.Settings.Default.sqlBag))
using (SqlCeDataAdapter sqlAdaptor = new SqlCeDataAdapter(select, sqlCEBaglantisi))
using (DataTable dtTablo = new DataTable())
{
sqlCEBaglantisi.Open();
sqlAdaptor.Fill(dtTablo);SqlCeCommand islemTipleriniAl = new SqlCeCommand("Select IslemTipi From IslemKaydi Group By IslemTipi", sqlCEBaglantisi); using (SqlCeDataReader oku1 = islemTipleriniAl.ExecuteReader()) { while (oku1.Read()) { cmbIGIslemTipiSec.Items.Add(oku1.GetString(0).Trim()); } }
teknolog123 wrote:
same code block repeats.
You could wrap some things in their own method :)
teknolog123 wrote:
So, is it goog practice?
Not if you're religious about the DRY-principle. In that case, there'd be a factory to return an
IDbConnection
, and the query-string would be passed to a method containing the database-connection logic. Still, with or without the repeating code; I could work with it. If you'd use English names for your variables, I might even be able to understand it's intention :)Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
teknolog123 wrote:
same code block repeats.
You could wrap some things in their own method :)
teknolog123 wrote:
So, is it goog practice?
Not if you're religious about the DRY-principle. In that case, there'd be a factory to return an
IDbConnection
, and the query-string would be passed to a method containing the database-connection logic. Still, with or without the repeating code; I could work with it. If you'd use English names for your variables, I might even be able to understand it's intention :)Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
thanks let me explain more. I just want to dispose sqceConnection, sqlceCommand, SqlCeCommandBuilder and sqlceAdapter as soon as I finish processing the data. for example does the below code dispose the objects in that manner? I think no but wanna make sure. (note: I didn't use sqladapter and commnadbuilder below to keep the code short on purpose)
public SqlCeConnection sqlSetCon()
{
SqlCeConnection sqlCon = new SqlCeConnection();
sqlCon.ConnectionString = Properties.Settings.Default.sqlConnStr;
sqlCon.Open();
return sqlCon;
}
public DataTable returnDataTable(string selectString)
{
SqlCeDataAdapter sqlAdaptor = new SqlCeDataAdapter(selectString, sqlSetCon());
SqlCeCommandBuilder sqlKomut = new SqlCeCommandBuilder(sqlAdaptor);
DataTable dtTablo = new DataTable();
sqlAdaptor.Fill(dtTablo);
return dtTablo;
}private void processList() { //does this using block dispose everything? using (DataTable dt = returnDataTable("Select \* From Customers Order By Surname")) { for (int i = 0; i < dt.Rows.Count; i++) { this.lblSurname.Text = dt.Rows\[i\]\["surname"\].ToString(); } } }
-
thanks let me explain more. I just want to dispose sqceConnection, sqlceCommand, SqlCeCommandBuilder and sqlceAdapter as soon as I finish processing the data. for example does the below code dispose the objects in that manner? I think no but wanna make sure. (note: I didn't use sqladapter and commnadbuilder below to keep the code short on purpose)
public SqlCeConnection sqlSetCon()
{
SqlCeConnection sqlCon = new SqlCeConnection();
sqlCon.ConnectionString = Properties.Settings.Default.sqlConnStr;
sqlCon.Open();
return sqlCon;
}
public DataTable returnDataTable(string selectString)
{
SqlCeDataAdapter sqlAdaptor = new SqlCeDataAdapter(selectString, sqlSetCon());
SqlCeCommandBuilder sqlKomut = new SqlCeCommandBuilder(sqlAdaptor);
DataTable dtTablo = new DataTable();
sqlAdaptor.Fill(dtTablo);
return dtTablo;
}private void processList() { //does this using block dispose everything? using (DataTable dt = returnDataTable("Select \* From Customers Order By Surname")) { for (int i = 0; i < dt.Rows.Count; i++) { this.lblSurname.Text = dt.Rows\[i\]\["surname"\].ToString(); } } }
Something similar to below; wrap the disposable's in a using-section, and they'll be disposed when they go out of scope.
public DataTable returnDataTable(string selectString)
{
using (var con = sqlSetCon())
using (SqlCeDataAdapter sqlAdaptor = new SqlCeDataAdapter(selectString, con))
{
DataTable dtTablo = new DataTable();
sqlAdaptor.Fill(dtTablo);
return dtTablo;
}
}Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
Something similar to below; wrap the disposable's in a using-section, and they'll be disposed when they go out of scope.
public DataTable returnDataTable(string selectString)
{
using (var con = sqlSetCon())
using (SqlCeDataAdapter sqlAdaptor = new SqlCeDataAdapter(selectString, con))
{
DataTable dtTablo = new DataTable();
sqlAdaptor.Fill(dtTablo);
return dtTablo;
}
}Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
thanks Eddy, by the way, is it only those that are written in parentheses disposed or anything in curly braces?
-
thanks Eddy, by the way, is it only those that are written in parentheses disposed or anything in curly braces?
teknolog123 wrote:
thanks Eddy, by the way, is it only those that are written in parentheses disposed or anything in curly braces?
Anything that's disposable would be best in a
using
-clause. You can try it by putting it in a using, if it's not disposable you'll get a compiler-error saying so. Alternatively, you can check MSDN. If you can't limit it to a scope, then you'd best dispose it from code later.Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]