Setting Database Values to Their Uninitialized State

來源:互聯網
上載者:User
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.



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。