开发者

problem with getting data from database

I am trying to get the data from database by using the below code.....

if there is no data in the table it will always goes to this statement

I am using mysql.net connector for getting the data and i am doing 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 bel开发者_StackOverflowow code is for return sqlexceution...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 goes to this line

table = ds.Tables[0];

how can i reduce this .....

would any one pls help on this....


In your case if you are think that catch block will get excuted if there is no row available than you are wrong because Even if there is no data once select query is get exucuted without exception it Creates datatable with the columns but with no rows.

for this i think you can make use of ds.table[0].rows.count property which return 0 if there is no row in datatable.

if ( ds.Tables[0].Rows.Count > 0 )
     table = ds.Tables[0];
else
     table=null;


It returns an empty table. This is common behavior. If you want to have table null you should check for the row count :

If ( ds.Tables[0].Rows.Count >. 0 )
     table = ds.Tables[0];
Else
     table=0


I'm not really sure what you're asking here ... I assume you want it to skip the table = ds.tables[0] line if there is no data?

if thats the case a try/catch wont work as it wont throw an exception ... try something like this instead ...

if(ds.Tables.Count > 0 && ds.Tables[0].Rows.Count >0)
{
    table = ds.Tables[0];
}
else
{
    table = null;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜