我有这些代码块:为什么此SqlCommand参数会产生SQLDateTime溢出错误?
var cmd = new SqlCommand();
cmd.CommandText = @"SELECT * FROM " + TableName + " Where ";
SqlConnection a = new SqlConnection("the setting");
cmd.Connection = a;
这些生产线下方,做工精细:
cmd.CommandText += strFromTextBox + " LIKE '%" + strUserInput + "%'";
//strFromTextBox & strUserInput is string datatype
//it looks like 'apple', 'ladder', just normal string
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
但这些的产生错误:
DateTime dtFrom = DateTime.Parse(dt1).Date;
DateTime dtTo = DateTime.Parse(dt2).Date;
//dt1 & dt2 is originally a string
//they look something like this: 2/1/2012 12:00:00 AM
cmd.Parameters.AddWithValue("@dt1", @dtFrom);
cmd.Parameters.AddWithValue("@dt2", @dtTo);
cmd.CommandText+= parameter[i].ToString() + " BETWEEN @dt1 AND @dt2";
//parameter[i] refers to the column name
它产生的误差:
System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow.
两个块是通过处理:
DataTable Table = new DataTable();
adapter.FillSchema(Table, SchemaType.Source);
adapter.Fill(Table);
最后的查询输出(字符中发现)
SELECT * FROM linkDb Where CreateDt BETWEEN @dt1 AND @dt2
能有人建议什么是错用这些代码行?
显然不是,请检查我提供的代码块..我把一条评论线显示输入样本.. – rofans91 2012-01-06 06:23:38
我已经这样做了。 TQ。事实上,我有其他朋友也帮我检查代码..它不是从输入.. – rofans91 2012-01-06 06:29:18
你能告诉我“参数[我]”这是什么? – 2012-01-06 06:33:30