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类,类中是对应数据库的字段所生成的属性。









  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值