字符串转Datatime问题

来源:百度知道 编辑:UC知道 时间:2024/05/22 15:22:17
protected void bnQuery_ServerClick(object sender, EventArgs e)
{
DateTime FD = Convert.ToDateTime(this.TextBox1.Text);
DateTime TD = Convert.ToDateTime(this.TextBox2.Text);
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
con.Open();

string sql = "select * from [Plan] where AddTime between '" + FD + "' and '" + TD + "' and username='" + Session["UserName"].ToString() + "'";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
this.Label1.Text = "共" + ds.Tables[0].Rows.Count + "记录";
con.Close();
}

DateTime FD = Convert.ToDateTime(this.TextBox1.Text)提示该字符串未被识别为有效的 DateTime?

DateTime dt = DateTime.Parse("2008-05-04");
输入的字符串格式是 2008-05-04 这样的吗?
--------------------
select * from [Plan] where AddTime between '" + FD.ToShortDateString() + "' and '" + TD.ToShortDateString() + "'

试试

Convert.ToDateTime 不带强转性质!
DateTime.Parse("时间") 是强制转换!

你文本框你输入的内容格式不正确。。。2008-01-25 12:00:00