VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • VB.NET中把String类型数据转化成Date型,并写到数据库

'从excel中拿到有时间的字符串并截取,

Dim reportDateFromExcel = xlSheet.Cells(2, 2).Value().ToString ' 'Report Date 31/12/2018'
Dim reprotDateStr = Strings.Right(reportDateFromExcel, 10)

Dim reportDate = DateConvertUtil.ConvertStringToDate(reprotDateStr)

'写方法进行转化

 Public Shared Function ConvertStringToDate(ByRef reportDate As String) As Date

        Dim year As String = reportDate.Substring(6, 4)
        Dim month As String = reportDate.Substring(3, 2)
        Dim day As String = reportDate.Substring(0, 2)
        Dim dt As Date = CDate(year + "/" + month + "/" + day)
        Return dt

    End Function

'写到数据库

  Dim toDBRecList As List(Of it_automation_AG_report_result) = New List(Of it_automation_AG_report_result)
  Dim db = LINQHelper.CreateDataContext()

  If Math.Round((totalScTbondValue - 1) * 100, 2) <> 0.00 Or Math.Round((totalScTbondWeightedReturn - 1) * 100, 2) <> 0.00 Then
                Dim newRec = New it_automation_AG_report_result With {
            .Portfolio_Id = 1,
            .Reutrn = Math.Round((totalScTbondValue - 1) * 100, 2),
            .Weighted_Return = Math.Round((totalScTbondWeightedReturn - 1) * 100, 2),
            .Report_DateTime = reportDate
        }
                toDBRecList.Add(newRec)

            End If

    db.it_automation_AG_report_results.InsertAllOnSubmit(toDBRecList)
    db.SubmitChanges()

注:根据数据库的表在程序中创建XXX.dbml文件,同时会自动生成XXX.designer.vb文件,it_automation_AG_report_result在自动生成文件中有个Partial Public Class it_automation_AG_report_result类,类中是对应数据库的字段所生成的属性。


相关教程