My question is: What's the most efficient way to handle bunch of postgresql queries inside a new class/method that have different SQL queries, parameters and return values?
For example:
NpgsqlCommand ukupno_sati = new NpgsqlCommand("begin;select cast(radni_sati.sati_rada as time) from radni_sati where zaposlenik_id=@zaposlenik_id " +
" and extract(month from radni_sati.datum)=@mjesec_broj and extract(year from radni_sati.datum)=@godina;commit;",conn);
ukupno_sati.Parameters.AddWithValue("@zaposlenik_id", dtIme_prezime_odjel_bolovanje_godisnji.Rows[i][0]);
ukupno_sati.Parameters.AddWithValue("@mjesec_broj", mjesec_broj);
ukupno_sati.Parameters.AddWithValue("@godina", godina);
NpgsqlDataAdapter dz = new NpgsqlDataAdapter(ukupno_sati);
DataTable UkupnoSati = new DataTable();
dz.Fill(UkupnoSati);
or
NpgsqlCommand godisnji = new NpgsqlCommand("begin;select count(*) from godisnji where extract(month from godisnji.datum)" +
"=@mjesec_broj and extract(year from godisnji.datum)=@godina and zaposlenik_id=@zaposlenik_id;commit;",conn);
godisnji.Parameters.AddWithValue("@mjesec_broj", mjesec_broj.ToString());
godisnji.Parameters.AddWithValue("@godina", godina.ToString());
godisnji.Parameters.AddWithValue("@zaposlenik_id", dtIme_prezime_odjel_bolovanje_godisnji.Rows[i][0]);
int iskoristeni_godisnji = Convert.ToInt32(godisnji.ExecuteScalar());
So my point is: Can I make a method that handles different queries with different return values and different number of parameters depending on a queries like shown in examples?
You can use the same technique I've used for writing ADONETHelper.
Also, check out the code on the project that's called HowToUseADONETHelper.
The main method is this:
Note that the Execute method accepts an argument of type
Func<IDbCommand, T>
- that fact allows you to do all kind of interesting things:Then you can use some other execute overloads to simplify your code:
Execute non query:
That's pretty simple - the function you send to the
Execute
method is simplyc => c.ExecuteNonQuery()
- and as you know, the ExecuteNonQuery in the IDbCommand interface is returning anint
, soExecute
will also returnint
.Execute Scalar:
Here the function is a little more complicated. The
ExecuteScalar()
ofIDbCommand
returns anObject
. ThatObject
might be null, it might be DBNull, or it might be a string, int, or whatever datatype supported by your database. So what I do is use a condition that returns thedefault
ofT
when the value returned byExecuteScalar
isnull
orDBNull
, or simply cast theobject
toT
and return it.Execute Reader:
Pretty simple, right? the populate function might look something like this:
And fill a dataset:
Basically, I could have done the same thing here as with the execute reader, and write the
FillDataSet
method as a lambda expression, I don't remember why I've decided to write theFillDataSet
as a different method. However, this is the method I pass as thefunction
argument to theExecute
method. It simply use a DataAdapter to fill the dataset. Note that you should change theTAdapter
toNpgsqlDataAdapter
in your case.If you are looking to return Data Table and integer from a method then its possible in single method by using data table as return type and integer as out parameter(you can read here)