Sql server 在VB.NET中使用ADO.NET将sql Server中的datatime转换为DateTime
我在VisualStudio2005中使用VB.NET语言创建了一个Web服务。此Web服务通过ADO.NET与Sql Server上的数据库交互。 使用ADO.NET,我得到了一个包含以下代码的表Sql server 在VB.NET中使用ADO.NET将sql Server中的datatime转换为DateTime,sql-server,vb.net,web-services,ado.net,datatable,Sql Server,Vb.net,Web Services,Ado.net,Datatable,我在VisualStudio2005中使用VB.NET语言创建了一个Web服务。此Web服务通过ADO.NET与Sql Server上的数据库交互。 使用ADO.NET,我得到了一个包含以下代码的表 conn = New SqlConnection(Utilities.ConnectionString) sqlcmd = New SqlClient.SqlCommand() sqlcmd.Connection = conn sqlcmd.CommandType =
conn = New SqlConnection(Utilities.ConnectionString)
sqlcmd = New SqlClient.SqlCommand()
sqlcmd.Connection = conn
sqlcmd.CommandType = CommandType.Text
sqlcmd.CommandText = "select * from myTable"
da = New SqlClient.SqlDataAdapter()
da.SelectCommand = sqlcmd
table = New DataTable()
da.Fill(table)
其中,Utilities是我创建的用于管理和返回ConnectionString的类。
表myTable包含多个字符串类型的字段和一个名为LastChangeDate的datetime字段。
我希望选择myTable行中的所有日期,因此我使用以下代码:
Dim d As DateTime
For Each row In table.Rows
d = row.Item("LastChangeDate")
'
' other codes for managing the retuned dates
'
问题是,在此操作之后,d包含空值
如何将VB.NET中从Item方法返回的datetime正确转换为datetime如果数据库中的
LastChangeDate
列允许空值,则无法将空值转换为日期。在尝试使用行将其转换为日期之前,您需要检查空值。IsNull(“LastChangeDate”)
我知道,根据我的数据,LastChangeDate字段中不包含任何空值。那么我想您的意思是d包含无任何内容。
来自@shahkalpesh的anser应该可以帮助您。
dim isDate as Boolean
dim lastChangeDate as Date
isDate = Date.TryParse(row.Item("LastChangeDate"), lastChangeDate)
If isDate Then
'** code that should work with the extracted date ...
End If