我有這些代碼塊:爲什麼此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