delphi 写入SqlServer 2005的datetime 值时,如果为空值时,默认值一般为1900-1-1 ,如何在写入的时候修改默认值为空值

解决方案 »

  1.   

    if ADOQuery1.FieldByName('dt').IsNull then
      

  2.   

    用isnull...example:string  sqlStmt ; 
    string  conString ; 
    SqlConnection cn =null; 
    SqlCommand cmd =null; 
    SqlDateTime sqldatenull ; 
    try
    {
     sqlStmt = "insert into Emp (FirstName,LastName,Date) Values (@FirstName,@LastName,@Date)  ";
     conString = "server=localhost;database=Northwind;uid=sa;pwd=;";
     cn = new SqlConnection(conString);
     cmd = new SqlCommand(sqlStmt, cn); cmd.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.NVarChar, 11));
     cmd.Parameters.Add(new SqlParameter("@LastName", SqlDbType.NVarChar, 40));
     cmd.Parameters.Add(new SqlParameter("@Date", SqlDbType.DateTime)); sqldatenull = SqlDateTime.Null; cmd.Parameters["@FirstName"].Value = txtFirstName.Text;
     cmd.Parameters["@LastName"].Value = txtLastName.Text;
     if (txtDate.Text == "") 
     {
      cmd.Parameters ["@Date"].Value =sqldatenull ;
      //cmd.Parameters["@Date"].Value = DBNull.Value;
     }  
     else
     {
      cmd.Parameters["@Date"].Value = DateTime.Parse(txtDate.Text);
     }
     cn.Open();
     cmd.ExecuteNonQuery();
     Label1.Text = "Record Inserted Succesfully";
    }
    catch (Exception ex)

     Label1.Text = ex.Message;
    }
    finally
    {
     cn.Close();
    }
      

  3.   

    实体类型设为Nullable(Of DateTime)
      

  4.   

    组织一个SQL语句,用Tquery执行一下就行了,如:INSERT INTO Table1     (AA)  VALUES (NULL)