2013-01-25 44 views
7

Tôi đã cố gắng làm cho mã của mình càng nhỏ càng tốt.SQL Server DateTime Chấp nhận NULL

Sử dụng Microsoft SQL Server, .NET 2.0

Tôi có một trường ngày trong cơ sở dữ liệu của tôi mà chấp nhận giá trị null

LeaseExpiry(datetime, null) 

tôi lấy giá trị của hộp văn bản và chuyển nó sang datetime.

DateTime leaseExpiry = Convert.ToDateTime(tbLeaseExpiry.Text); 

INSERT_record(leaseExpiry); 

Vấn đề tôi gặp phải là nếu biểu mẫu được gửi và hộp văn bản trống. Tôi nhận được lỗi này:

Chuỗi không được nhận dạng là Ngày giờ hợp lệ.

Làm cách nào để thiết lập mã của mình để hộp văn bản trống, hàng được tạo trong cơ sở dữ liệu với NULL?

Tôi đã cố gắng khởi tạo biến tôi để NULL nhưng nhận được một lỗi trong Visual Studio

DateTime leaseExpiry = null; 

Không thể chuyển đổi null để 'System.DateTime' bởi vì nó là một loại giá trị không nullable.

Đây là Data Access Layer nếu điều đó giúp

public string INSERT_record(DateTime leaseExpiry) 
{ 
    //Connect to the database and insert a new record 
    string cnn = ConfigurationManager.ConnectionStrings[connname].ConnectionString; 

    using (SqlConnection connection = new SqlConnection(cnn)) 
    { 
     string SQL = string.Empty; 
     SQL = "INSERT INTO [" + dbname + "].[dbo].[" + tblAllProperties + "] ([LeaseExpiry]) VALUES (@leaseExpiry); 

     using (SqlCommand command = new SqlCommand(SQL, connection)) 
     { 
       command.Parameters.Add("@leaseExpiry", SqlDbType.DateTime); 
       command.Parameters["@leaseExpiry"].Value = leaseExpiry; 
     } 

     try 
     { 
       connection.Open(); 
       command.ExecuteNonQuery(); 
       return "Success"; 
     } 
     catch (Exception ex) 
     { 
       return ex.Message; 
     } 
    } 
} 

Cảm ơn bạn

+0

Nó phải được viết là 'leaseExpiry' (không phải' leaseExpirey') - bạn có cả hai phiên bản trong mã của bạn, điều này thực sự khó hiểu. Tôi đã sửa nó thành 'Expiry' mọi lúc. –

Trả lời

13

Thật vậy, DateTime không thể null. Nhưng: DateTime? có thể. Cũng lưu ý rằng trên tham số, null có nghĩa là "không gửi"; bạn sẽ cần:

public string INSERT_record(DateTime? leaseExpirey) 
{ 
    // ... 
    command.Parameters.Add("@leaseExpirey", SqlDbType.DateTime); 
    command.Parameters["@leaseExpirey"].Value = 
       ((object)leaseExpirey) ?? DBNull.Value; 
    // ... 
} 
+0

Cảm ơn bạn đã giúp. – Scott

2

Bạn có thể làm leaseExpirey một nullable DateTime - tức là DateTime? leaseExpirey

Sau đó, bạn có thể nói:

DateTime? leaseExpirey; 
if (!string.IsNullOrEmpty(tbLeaseExpiry.Text.Trim())) 
    leaseExpirey = Convert.ToDateTime(tbLeaseExpiry.Text); 

INSERT_record(leaseExpirey); 

Bạn cũng sẽ cần phải thay đổi INSERT_record để chấp nhận một tham số DateTime? thay vì DateTime.

3

Hãy thử sử dụng một DateTime nullable và TryParse()

DateTime? leaseExpirey = null; 
DateTime d; 
if(DateTime.TryParse(tbLeaseExpiry.Text, out d)) 
{ 
    leaseExpirey = d; 
} 

INSERT_record(leaseExpirey); 
+0

Cảm ơn bạn tôi đã sử dụng điều này quá. – Scott

0

Bạn nên sử dụng DateTime.MinValue, vì DateTime là không bao giờ null.

Các vấn đề liên quan