2011-02-01 114 views
0

我有一个DataGrid表tblpaymentview,我想从数据库 我宣布concstring 获取值并编写代码为查询返回什么

private void btnsearch_Click(object sender, EventArgs e) 
{ 
    try 
    { 
    DateTime time = dtpfromDate.Value; 
    String query = "Select jobcode,companyName,vehicleno,shipmentdate,totalamount,advance,drivername,fromPlace,destination from jobmastertable where (shipmentdate='"+time+"')"; 
    OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString); 
    DataSet ds = new DataSet(); 
    dAdapter.Fill(ds); 
    tblpaymentview.DataSource = ds.Tables["tblpaymentview"].DefaultView; 
    } 
    catch (Exception) 
    { 
    MessageBox.Show("The application had met with some errors please restart the application :\n error:closer MSAccess files"); 
    } 
} 

请谁能帮助我。我也试过(Datepicker)dtpfromdate.value.toshortdateString()也

+0

不是一个答案,但你真的应该换数据集,OleDbDataAdapter的,等在using语句 – Manatherin 2011-02-01 13:54:19

回答

0

你确定你没有得到例外?

其次,你为什么假设该表格被称为tblpaymentview当它来自jobmastertable

0

我不知道这是否是您的问题,但ACCESS通常使用#代替日期格式。

+0

是日期/时间型 – sreenath 2011-02-01 13:39:29

+0

soory好友之后所有这些东西都添加了相同的异常发生IAM新到.net并且无法处理它谢谢ALL – sreenath 2011-02-01 13:51:10

0

在这种情况下,你可以使用OleDBParameters:

String query = "Select jobcode,companyName,vehicleno,shipmentdate,totalamount,advance,drivername,fromPlace,destination from jobmastertable where ([email protected])"; 
dAdapter.SelectCommand.Parameters.AddWithValue("@shipmentdate", time); 
+0

ya iam得到异常“标准表达式中的数据类型不匹配”。 – sreenath 2011-02-01 13:32:21

0

我想从你的表中查询一些其他的条件,只是为了让你的“shipmentdate”栏目的真实数据类型的例子。这真的是约会吗?但实际上是日期/时间字段。或作为字符存储的日期/时间。我会将其更改为参数化查询,并根据需要添加类型...如日期时间字段,而不是通过字符串连接。

0

尝试的BindingSource如下:

BindingSource bindingsource1; 
DataSet ds; 
String query = "Select jobcode,companyName,vehicleno,shipmentdate,totalamount,advance,drivername,fromPlace,destination from jobmastertable where (shipmentdate='"+time+"')"; 
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query,Your Connection); 
ds = new DataSet(); 
dAdapter.Fill(ds); 
bindingsource1 = new BindingSource(); 
bindingsource1.DataSource = ds; 
bindingsource1.DataMember = ds.Tables[0].TableName; 
tblpaymentview.DataSource = bindingsource1; 

希望这个作品......