I am trying to get data from a database using the code below .....
if there is no data in the table, it will always be this statement
I use a mysql.net connector to receive data, and I make winforms applications using C #
public DataTable sales(DateTime startdate, DateTime enddate) { const string sql = @"SELECT memberAccTran_Source as Category, sum(memberAccTran_Value) as Value FROM memberacctrans WHERE memberAccTran_DateTime BETWEEN @startdate AND @enddate GROUP BY memberAccTran_Source"; return sqlexecution(startdate, enddate, sql); }
and the code below is for sqlexceution return function ...
private static DataTable sqlexecution(DateTime startdate, DateTime enddate, string sql) { var table = new DataTable(); using (var conn = new MySql.Data.MySqlClient.MySqlConnection(connectionstring)) { conn.Open(); var cmd = new MySql.Data.MySqlClient.MySqlCommand(sql, conn); var ds = new DataSet(); var parameter = new MySql.Data.MySqlClient.MySqlParameter("@startdate", MySql.Data.MySqlClient.MySqlDbType.DateTime); parameter.Direction = ParameterDirection.Input; parameter.Value = startdate.ToString(dateformat); cmd.Parameters.Add(parameter); var parameter2 = new MySql.Data.MySqlClient.MySqlParameter("@enddate", MySql.Data.MySqlClient.MySqlDbType.DateTime); parameter2.Direction = ParameterDirection.Input; parameter2.Value = enddate.ToString(dateformat); cmd.Parameters.Add(parameter2); var da = new MySql.Data.MySqlClient.MySqlDataAdapter(cmd); da.Fill(ds); try { table = ds.Tables[0]; } catch { table = null; } } return table; }
even if there is no data, the process flow will flow to this line
table = ds.Tables [0];
how can i reduce this .....
will anyone help with this ....
source share