查询返回空数据集或异常 [英] Query returns empty data set or exception

查看:103
本文介绍了查询返回空数据集或异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,jobmaster表,其中有一个现场发货日期(短时间格式的日期/时间类型).
在窗口形式中,我要调用表中的所有数据,其中发货日期=数据选择器中的值.

我尝试了很多.

代码是:

I had a table, jobmaster table, with a field shipment date (date/time type in shorttime format).
In a window form I want to call all data from the table where shipment date = value from a data picker.

I tried a lot with it.

The code is:

DateTime time = dtpfromDate.Value; 
String query = "Select jobcode,companyName,vehicleno,shipmentdate,totalamount,advance,drivername,fromPlace,destination from jobmastertable  where (shipmentdate=" + @shipmentdate + ")"; 
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
dAdapter.SelectCommand.Parameters.AddWithValue("@shipmentdate", time);
DataSet ds = new DataSet(); dAdapter.Fill(ds, "tbljobmastertable");
tblpaymentview.DataSource = ds.Tables["tblpaymentview"].DefaultView;

推荐答案

两个代码段中唯一缺少的是变量前后的单引号('').
以下是更新的代码段:
代码段1:
The only thing you are missing in both the snippet is the single quote('') before and after the variable.
Below are the updated code snippets:
Snippet 1:
DateTime time = dtpfromDate.Value;
String query = "Select jobcode,companyName,vehicleno,shipmentdate,totalamount,advance,drivername,fromPlace,destination from jobmastertable  where (shipmentdate=''"+@shipmentdate+"'')";
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query,connString);
dAdapter.SelectCommand.Parameters.AddWithValue("@shipmentdate", time);
DataSet ds = new DataSet();dAdapter.Fill(ds,"tbljobmastertable");
tblpaymentview.DataSource = ds.Tables["tblpaymentview"].DefaultView;



代码段2:



Snippet 2:

public void showjobcodebySearchDate(DateTime fromdate, DateTime todate)
{
  try
  {
    String query = "Select jobcode,companyName,vehicleno,shipmentdate,totalamount,advance,drivername, vehicletype from jobmastertable where shipmentdate >=''" + dtpfromDate.Value.ToShortDateString() + "''";
    OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
    DataSet ds = new DataSet();
    dAdapter.Fill(ds, "tbluserdataview");
    tbljobcodeview.DataSource = ds.Tables["tbluserdataview"].DefaultView;
  }
  catch (Exception exp)
  {
    MessageBox.Show("The application had met with some errors please restart  the application :\n error:close MSAccess files");
  }
}



希望这能回答您的问题



Hope this answer your question


DateTime time = dtpfromDate.Value; 
String query = "Select jobcode,companyName,vehicleno,shipmentdate,totalamount,advance,drivername,fromPlace,destination from jobmastertable  where (datepart(dd,shipmentdate)=" + @shipmentdateday + ") and datepart(mm,shipmentdatemonth)="+@shipmentdatemonth+" and datepart(yyyy,shipmentdateyear)="+@shipmentdateyear+"" ; 
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
dAdapter.SelectCommand.Parameters.AddWithValue("@shipmentdatemday", time.Day);
dAdapter.SelectCommand.Parameters.AddWithValue("@shipmentdatemmonth", time.Month);
dAdapter.SelectCommand.Parameters.AddWithValue("@shipmentdatemYear", time.Year);
DataSet ds = new DataSet(); 
dAdapter.Fill(ds, "tbljobmastertable");
tblpaymentview.DataSource = ds.Tables["tblpaymentview"].DefaultView;



您只需更改您的sql查询即可,并且可以100%尝试使用此方法



you just change your sql query and that works 100% try this


谢谢朋友,我发现它是因为dtpdate.value中也包含了时间,无论如何我还是向其中添加了.date属性非常感谢您


thank you friends i find it out it was because time is also included in dtpdate.value i added .date property to it anyway thank you alot


try
            {
                DateTime time = dtpfromDate.Value.Date;
                MessageBox.Show("" + time);
                String query = "Select jobcode,companyName,vehicleno,shipmentdate,totalamount,advance,drivername,fromPlace,destination from jobmastertable  where (shipmentdate= @shipmentdate)";
                OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
                dAdapter.SelectCommand.Parameters.AddWithValue("@shipmentdate", time);
                DataSet ds = new DataSet();
                dAdapter.Fill(ds, "tbljobmastertable");
                tbljobcodeview.DataSource = ds.Tables["tbljobmastertable"].DefaultView;
            }
            catch (Exception eps)
            {
                MessageBox.Show("" + eps);
            }


这篇关于查询返回空数据集或异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆