2010-08-27 63 views
1

我有一句話如下:更新空類型日期

string strSQL = "update pl_poli set ag_vouch = ' ',ag_vdate = #, ag_vmode = null where 
pl_no = '" + textBox5.Text + "' and pl_endtno ='" + textBox6.Text + "'"; 

我不能更新,因爲錯誤「數據類型mismath」。我有填寫ag_vdate是類型日期 我想更新它 - > null 請你能幫助我。非常感謝。

+8

http://en.wikipedia.org/wiki/SQL_injection 請小心:) 您正在使用哪些DBMS? – 2010-08-27 09:28:33

+0

ag_vdate =# 它有效嗎? – Shekhar 2010-08-27 09:29:26

回答

1

你的情況,你不能將「#」在datetime列,因爲SQL Server認爲這是VARCHAR值,並不能轉換這個在日期時間等等...

嘗試做如下

using (SqlConnection connection = new SqlConnection(connectionString)) 
using (SqlCommand command = connection.CreateCommand()) 
{ 
    command.CommandText = "INSERT INTO table1 (column1, column2) VALUES (@param1, @param2)"; 

    command.Parameters.Add("@param1", SqlDbType.DateTime).Value = 
     DateTime.TryParse(txtDate.Text, out d) ? 
      (object)d : 
      DBNull.Value // inserting NULL 
    ... 

    connection.Open(); 
    command.ExecuteNonQuery(); 
}