问题描述
我想从数据库中选择记录,其中date = date在datetimepicker中我正在使用这个但是出现错误从字符串转换日期/时间时转换失败。
datetimepicker格式为long,日期列的SQL数据类型为date
SqlDataAdapter com = new SqlDataAdapter(select * from Farmers where Date = + dateTimePicker1.Value.Date +,con);
DataSet ds = new DataSet(Farmers);
con.Open();
com.Fill(ds);
con.Close();
I want to select records from data base where date = date in datetimepicker I''m using this but Error occurs "Conversion failed when converting date/time from character string".
datetimepicker format is "long" and in SQL datatype of Date Column is date
SqlDataAdapter com = new SqlDataAdapter("select * from Farmers where Date="+dateTimePicker1.Value.Date+"", con);
DataSet ds = new DataSet("Farmers");
con.Open();
com.Fill(ds);
con.Close();
推荐答案
SqlDataAdapter com = new SqlDataAdapter("SELECT * FROM Farmers WHERE Date=@DT", con);
com..SelectCommand.Parameters.AddWithValue("@DT", dateTimePicker1.Value.Date);
DataSet ds = new DataSet("Farmers");
con.Open();
com.Fill(ds);
con.Close();
这篇关于使用C#usind datetime picker从SQL中选择数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!