2010-08-27 52 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(); 
}