本文介绍了如何通过vb.net代码在SQL Server 2008的DATETIME字段中插入NULL值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 这是我用来保存id和日期的代码。 如果我没有给出datetextbox的值,它将作为系统的默认日期时间。 我想要如果我没有为datetextbox提供任何值,它会在sql中存储一个空值。请给出编码 SaveID(IDTextbox.text,DateTextbox.text)私人函数SaveID(ByVal ID As String ,ByVal Date As DateTime) SaveID = False 尝试 Me.MySqlConnect(My.Settings.AAConnectionString) Dim MySqlCmd As SqlClient.SqlCommand MySqlCmd = New SqlCommand( INSERT INTO [AA]。[dbo]。 [tbl_ID]([ID],[Date])VALUES(@ ID ,, @ Date),Me.Connection) MySqlCmd.Parameters.Add(New SqlParameter( @ ID,SqlDbType.NVarChar, 10 )) MySqlCmd.Parameters( @ ID)。Value = ID MySqlCmd.Parameters .Add(New SqlParameter( @ Date,SqlDbType.DateTime)) MySqlCmd.Parameters( @ Date)。Value = Convert.ToDateTime(Date) MySqlCmd.CommandType = CommandType.Text MySqlCmd.ExecuteNonQuery() Me.Connection.Close() SaveID = True Catch e As Exception MsgBox(e。消息)结束尝试 END函数 解决方案 为什么不使用 Nullable(Of DateTime) [ ^ ]? 使用这个代码它在SQL中存储空白 SaveID(IDTextbox.text,DateTextbox.text) 私有函数SaveID(ByVal ID为字符串,ByVal日期为DateTime) SaveID = False 尝试 Me.MySqlConnect(My.Settings) .AAConnectio nString) Dim MySqlCmd作为SqlClient.SqlCommand MySqlCmd =新SqlCommand(INSERT INTO [AA]。[dbo]。[tbl_ID]([ID],[Date] ])VALUES(@ ID ,, @ Date),Me.Connection) MySqlCmd.Parameters.Add(New SqlParameter(@ ID,SqlDbType.NVarChar,10)) MySqlCmd.Parameters(@ ID)。值= ID 如果日期<>什么都没有 MySqlCmd.Parameters.Add(新的SqlParameter(@ Date,SqlDbType.DateTime)) MySqlCmd.Parameters(@ Date)。Value = Convert .ToDateTime(Date) Else Dim getDate As SqlDateTime getDate = SqlDateTime.Null MySqlCmd.Parameters .Add(New SqlParameter(@ Date,SqlDbType.NVarChar,100)) MySqlCmd.Parameters(@ Date)。Value = getDate end if MySqlCmd.CommandType = CommandType.Text MySqlCmd.ExecuteNonQuery() Me.Connection.Close() SaveID = True Catch e As Exception MsgBox(e.Message) 结束尝试 END功能 This is the code i used to save id and date.If I did not give the value for datetextbox it takes as the default datetime of a system.I want that If i did not give any value for datetextbox it stores a null value in sql. Please give the codingsSaveID(IDTextbox.text,DateTextbox.text)Private Function SaveID(ByVal ID As String,ByVal Date As DateTime)SaveID = False Try Me.MySqlConnect(My.Settings.AAConnectionString) Dim MySqlCmd As SqlClient.SqlCommand MySqlCmd = New SqlCommand("INSERT INTO [AA].[dbo].[tbl_ID]([ID],[Date]) VALUES (@ID,,@Date)", Me.Connection) MySqlCmd.Parameters.Add(New SqlParameter("@ID", SqlDbType.NVarChar, 10)) MySqlCmd.Parameters("@ID").Value = ID MySqlCmd.Parameters.Add(New SqlParameter("@Date", SqlDbType.DateTime)) MySqlCmd.Parameters("@Date").Value = Convert.ToDateTime(Date) MySqlCmd.CommandType = CommandType.Text MySqlCmd.ExecuteNonQuery() Me.Connection.Close() SaveID = True Catch e As Exception MsgBox(e.Message) End TryEND Function 解决方案 Why don't you use a Nullable (Of DateTime)[^]?USE THIS CODE IT DOES STORE NULL IN SQLSaveID(IDTextbox.text,DateTextbox.text)Private Function SaveID(ByVal ID As String,ByVal Date As DateTime)SaveID = False Try Me.MySqlConnect(My.Settings.AAConnectionString) Dim MySqlCmd As SqlClient.SqlCommand MySqlCmd = New SqlCommand("INSERT INTO [AA].[dbo].[tbl_ID]([ID],[Date]) VALUES (@ID,,@Date)", Me.Connection) MySqlCmd.Parameters.Add(New SqlParameter("@ID", SqlDbType.NVarChar, 10)) MySqlCmd.Parameters("@ID").Value = ID If Date<> Nothing Then MySqlCmd.Parameters.Add(New SqlParameter("@Date", SqlDbType.DateTime)) MySqlCmd.Parameters("@Date").Value = Convert.ToDateTime(Date) Else Dim getDate As SqlDateTime getDate = SqlDateTime.Null MySqlCmd.Parameters.Add(New SqlParameter("@Date", SqlDbType.NVarChar, 100)) MySqlCmd.Parameters("@Date").Value = getDateend if MySqlCmd.CommandType = CommandType.Text MySqlCmd.ExecuteNonQuery() Me.Connection.Close() SaveID = True Catch e As Exception MsgBox(e.Message) End TryEND Function 这篇关于如何通过vb.net代码在SQL Server 2008的DATETIME字段中插入NULL值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-16 09:58