输入不匹配表达式

时间:2015-04-17 04:18:03

标签: c# ms-access-2007

以下是我尝试执行的代码,但错误:

string g = dateTimePicker1.Value.ToString("dd-MM-yyyy");
string h = dateTimePicker2.Value.ToString("dd-MM-yyyy");

//string sql2 = @"select Loanno,PName,Duedate,sum(Rec) as rec,sum(Loanamt) as amt from Due where Duedate between [@date1] and [@date2] group by PName order by Loanno asc";

string sql2 = @"select Due.Loanno,Due.PName,Due.Duedate,sum(Due.Rec) as rec,sum(Due.Loanamt) as amt from Due  inner join Party_Det on Due.Loanno=Party_Det.Loanno where Due.Duedate between [@date1] and [@date2] group by Due.Loanno,Due.PName,Due.Duedate";

OleDbCommand cmd2 = new OleDbCommand(sql2, con);
cmd2.Parameters.AddWithValue("@date1", g);
cmd2.Parameters.AddWithValue("@date2", h);
cmd2.ExecuteNonQuery();

DataTable dt=new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd2);
da.Fill(dt);

dataGridView1.DataSource = dt;
con.Close();

1 个答案:

答案 0 :(得分:1)

试试这个

string sql2 = @"select Due.Loanno, Due.PName, Due.Duedate, sum(Due.Rec) as rec,sum(Due.Loanamt) as amt from Due  inner join Party_Det on Due.Loanno=Party_Det.Loanno where Due.Duedate between @date1 and @date2 group by Due.Loanno,Due.PName,Due.Duedate";
OleDbCommand cmd2 = new OleDbCommand(sql2, con);

cmd2.Parameters.AddWithValue("@date1", dateTimePicker1.Value.Date);
cmd2.Parameters.AddWithValue("@date2", dateTimePicker2.Value.Date);

cmd2.ExecuteNonQuery();
DataTable dt=new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd2);

da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();    

已添加:为什么需要使用Party_det表加入?您没有使用该表中的任何字段。请确保两个表中的Loanno列具有相同的数据类型。