請幫我插入一個日期從dd-mm-yyyy格式的文本框到sql服務器。 我的代碼如下: -如何插入日期從文本框到數據庫
int prio = Convert.ToInt32(Priority.Text);
string stdate = planstart.Text;
string endate= planend.Text;
string actst = actualstart.Text;
string acten = actualend.Text;
SqlConnection myconnection = new SqlConnection(constring);
SqlCommand mycommand = new SqlCommand();
DataSet mydataset = new DataSet();
SqlDataAdapter mydataadapter = new SqlDataAdapter();
myconnection.Open();
mycommand.Connection = myconnection;
mycommand.CommandText = " insert into project_status.dbo.Project_Status_Report values('" + projectcode.Text + "','" + projectname.Text + "',(select P_Code from project_status.dbo.Project_Type where Project_Type = '" + projecttype.Text + "')," + prio + ",'" + stdate + "','" + endate + "','" + actst + "','" + acten + "','" + currentstatus.Text + "','" + remark.Text + "','no');";
mycommand.CommandType = CommandType.Text;
mycommand.ExecuteNonQuery();
,它是拋出一個異常說: - 從字符串轉換日期和/或時間時 轉換失敗。
1)查看**參數化查詢**以防止SQL注入。 2)如果你的日期應該是一個日期,不要把它當作一個字符串。驗證它是一個有效的日期,將其轉換,然後將其作爲查詢中的參數提供。 – 2012-02-10 06:15:33
你能告訴我如何轉換它嗎?因爲我已經嘗試過但未能轉換。 – 2012-02-10 06:19:09
如果它的工作比接受答案................. – 2012-02-10 06:55:32