日期:2008-11-01  浏览次数:20393 次

Question:

When I'm inserting blank value into a SQL Server database, for a DateTime column, my database it is inserting 1/1/1900 even if I assign Null to the variable in my application. Here is an example:


string xx = null;
//T-SQL = "INSERT INTO tempTable (DateTimeColumn) VALUES (xx);"

Can you help me with this problem?

Thanks & regards,

Ravi

Answer:

Ravi, you're in luck. This is actually an easy fix - you just have to know what to do (and unfortunately sometimes finding the right thing to do is half the battle).

When you are inserting data into a database, the ADO.NET data providers and your database may distinguish between a null object and an uninitialized value. In your case, inserting a null into a DateTime column causes the database to seed the field with the default initialized value - 1/1/1900. In reality you want to tell the database that the field in question should remain uninitialized. To do that there is a singleton class in the .NET Framework that is designed for passing to a database to represent an uninitialized value. That class is System.DBNull; and specifically the Value property of the class.

To insert a record into your database, and maintain the uninitialized state of the DateTime fields you would do something like this:

[C#]
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "INSERT INTO myTable (Name, RegisteredDate, CancelDate) " +
    "VALUES (@Name, @RegisteredDate, @CancelDate)";
cmd.Parameters.Add("@Name", "Doug Seven");
cmd.Parameters.Add("@RegisteredDate", DateTime.Today);
//Use System.DBNull.Value to leave the field uninitialized
cmd.Parameters.Add("@CancelDate", System.DBNull.Value);

[Visual Basic .NET]
Dim cmd As New SqlCommand()
cmd.Connection = con
cmd.CommandText = "INSERT INTO myTable (Name, RegisteredDate, CancelDate) " & _
    "VALUES (@Name, @RegisteredDate, @CancelDate)"
cmd.Parameters.Add("@Name", "Doug Seven")
cmd.Parameters.Add("@RegisteredDate", DateTime.Today)
'Use System.DBNull.Value to leave the field uninitialized
cmd.Parameters.Add("@CancelDate", System.DBNull.Value)

In many cases I will actually perform checks on the value passed to my insert or update operation to determine if DBNull.Value should be sent to the stored procedure I am invoking. If you were to spend some time looking at the code I write in the real world, you would see something like this (this is code direct from a client application):

If user.FirstName = String.Empty Then
  cmd.Parameters("@FirstName").Value = System.DBNull.Value
Else
  cmd.Parameters("@FirstName").Value = user.FirstName
End If

If user.LastName = String.Empty Then
  cmd.Parameters("@LastName").Value = System.DBNull.Value
Else
  cmd.Parameters("@LastName").Value = user.LastName
End If

If user.RegisteredDate = Nothing Then
  cmd.Parameters("@RegisteredDate").Value = System.DBNull.Value
Else
  cmd.Parameters("@RegisteredDate").Value = user.RegisteredDate
End If  

Summary

To set values in a database to their uninitialized state, use the System.DBNull.Value structure. You can pass this value in using a T-SQL command, or a stored procedure - passing DBNull.Value as a parameter.