C#winform sql查询数据库,提示: 从字符串转换日期和/或时间时,转换失败。
DateTime Tmpstart = DateTime.Parse(StartData.Value.ToString("yyyy/MM/dd 0:00:00"));
DateTime Tmpend = DateTime.Parse(EndData.Value.ToString("yyyy/MM/dd 23:59:59"));
//string Tmpstart = StartData.Value.ToString();
// string Tmpend = EndData.Value.ToString();
string MySelect = "select 租赁时间,租金 from GuiderData where 租赁时间 > 'Tmpstart' and 租赁时间< 'Tmpend' ";
consql.Open();
/*SqlCommand sqlcmd = new SqlCommand(MySelect,consql);
sqlcmd.ExecuteNonQuery();*/
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(MySelect, consql);
da.Fill(ds);
RentData.DataSource = ds.Tables[0];
------解决方案--------------------直接写
DateTime Tmpstart = StartData.Value.Date;
DateTime Tmpend = EndData.Value.Date.AddDays(1).AddMinutes(-1);
不用写
DateTime Tmpstart = DateTime.Parse(StartData.Value.ToString("yyyy/MM/dd 0:00:00"));
DateTime Tmpend = DateTime.Parse(EndData.Value.ToString("yyyy/MM/dd 23:59:59"));
------解决方案--------------------"select * from users where 租赁时间 between @Tmpstart and @Tmpend"
这样写就可以了
------解决方案--------------------
"select 租赁时间,租金 from GuiderData where 租赁时间 > '"+Tmpstart+"' and 租赁时间< '"+Tmpend+"' ";