Code (csharp):
public static class Baza
{
public static OleDbCommand KreirajKomandu(string KonekcijskiString)
{
string strKonekcija = ConfigurationManager.ConnectionStrings[KonekcijskiString].ConnectionString;
OleDbConnection OleDbKonekcija = new OleDbConnection(strKonekcija);
OleDbCommand komanda = OleDbKonekcija.CreateCommand();
return komanda;
}
public static DataTable IzvrsiSqlUpit(OleDbCommand komanda)
{
DataTable rezultat = new DataTable();
try
{
komanda.Connection.Open();
OleDbDataReader citac = komanda.ExecuteReader();
rezultat.Load(citac);
citac.Close();
}
catch
{}
finally
{
if (komanda.Connection.State == ConnectionState.Open)
komanda.Connection.Close();
}
return rezultat;
}
public static void IzvrsiSqlKomandu(OleDbCommand komanda) //update,insert,delete
{
try
{
komanda.Connection.Open();
komanda.ExecuteNonQuery();
}
catch
{}
finally
{
if (komanda.Connection.State == ConnectionState.Open)
komanda.Connection.Close();
}
}
}
{
public static OleDbCommand KreirajKomandu(string KonekcijskiString)
{
string strKonekcija = ConfigurationManager.ConnectionStrings[KonekcijskiString].ConnectionString;
OleDbConnection OleDbKonekcija = new OleDbConnection(strKonekcija);
OleDbCommand komanda = OleDbKonekcija.CreateCommand();
return komanda;
}
public static DataTable IzvrsiSqlUpit(OleDbCommand komanda)
{
DataTable rezultat = new DataTable();
try
{
komanda.Connection.Open();
OleDbDataReader citac = komanda.ExecuteReader();
rezultat.Load(citac);
citac.Close();
}
catch
{}
finally
{
if (komanda.Connection.State == ConnectionState.Open)
komanda.Connection.Close();
}
return rezultat;
}
public static void IzvrsiSqlKomandu(OleDbCommand komanda) //update,insert,delete
{
try
{
komanda.Connection.Open();
komanda.ExecuteNonQuery();
}
catch
{}
finally
{
if (komanda.Connection.State == ConnectionState.Open)
komanda.Connection.Close();
}
}
}
U samom programu nije potrebno svaki put ponavljati kod za otvaranje i zatvaranje konekcije i korišćenje iste konekcije. Ovo je ujedno i jedna od prednosti korišćenja ovog pristupa.
Code (csharp):
OleDbCommand komanda = Baza.KreirajKomandu("KonekcijskiString");
komanda.Parameters.AddWithValue("Ime", TextBox1.Text);
komanda.Parameters.AddWithValue("Prezime", TextBox2.Text);
komanda.CommandText = "INSERT INTO Tabela(Ime,Prezime) VALUES(?,?)";
Baza.IzvrsiSqlKomandu(komanda);
komanda.Parameters.AddWithValue("Ime", TextBox1.Text);
komanda.Parameters.AddWithValue("Prezime", TextBox2.Text);
komanda.CommandText = "INSERT INTO Tabela(Ime,Prezime) VALUES(?,?)";
Baza.IzvrsiSqlKomandu(komanda);