c#操作数类型冲突:日期与int不兼容

时间:2016-03-14 03:00:07

标签: c#

当我执行以下查询时,收到错误消息date is incompatible with int

textBox4.Text = dateTimePicker1.Value.ToShortDateString();

DataSet ds1 = new DataSet();

cmd.CommandText = "select ID_REPAS FROM CONSOMMER C , GROUPE G WHERE G.REF_GROUPE=C.REF_GROUPE AND C.dateEFF=G.dateEff and G.REF_GROUPE=" + textBox3.Text + "and C.dateEFF=" +dateTimePicker1.Value.ToShortDateString();
da2.Fill(ds1, "idrepas");

textBox2.Text = ds1.Tables["idrepas"].Rows[0][0].ToString();

2 个答案:

答案 0 :(得分:1)

最清楚,最安全的方法是使用SQL参数......

cmd.CommandText = "select ID_REPAS FROM CONSOMMER C , GROUPE G WHERE G.REF_GROUPE=C.REF_GROUPE AND C.dateEFF=G.dateEff and G.REF_GROUPE=@txtBox3Val and C.dateEFF=@shortDate";

command.Parameters.Add(new SqlParameter("txtBox3Val" , textBox3.Text));
command.Parameters.Add(new SqlParameter("shortDate", dateTimePicker1.Value.ToShortDateString());

这有助于避免难以管理(' +')代码并防止注入攻击。

答案 1 :(得分:0)

尝试在日期字符串周围加上单引号(否则日期字符串中的数字将被视为整数):

... + "and C.dateEFF='" + dateTimePicker1.Value.ToShortDateString() + "'";