Sql server 使用VB.NET将持续时间从CSV上载到SQL dB

Sql server 使用VB.NET将持续时间从CSV上载到SQL dB,sql-server,vb.net,csv,Sql Server,Vb.net,Csv,我正在尝试将一些持续时间值从CSV文件上载到我的SQL Server数据库 我尝试加载的值采用以下格式hh:mm:ss 我正在使用下面的代码,但它在转换数据类型时会抛出各种错误。当前错误为“输入字符串格式不正确。无法存储在CallTime列中。类型应为Int32。” 代码如下: Imports System.Data.SqlClient Public Class DaisyBillingForm Private Sub Button1_Click(sender As Object, e As

我正在尝试将一些持续时间值从CSV文件上载到我的SQL Server数据库

我尝试加载的值采用以下格式hh:mm:ss

我正在使用下面的代码,但它在转换数据类型时会抛出各种错误。当前错误为“输入字符串格式不正确。无法存储在CallTime列中。类型应为Int32。”

代码如下:

Imports System.Data.SqlClient

Public Class DaisyBillingForm

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click



    '--First create a datatable with the same cols as CSV file, the cols order in both should be same
    Dim table As New DataTable()

    table.Columns.Add("CallType", GetType(String))
    table.Columns.Add("CustomerCLI", GetType(String))
    table.Columns.Add("TelephoneNumber", GetType(String))
    table.Columns.Add("CallDate", GetType(Date))
    table.Columns.Add("CallTime", GetType(Integer))
    table.Columns.Add("Duration", GetType(Integer))
    table.Columns.Add("Mb", GetType(String))
    table.Columns.Add("Description", GetType(String))
    table.Columns.Add("TimeBand", GetType(String))
    table.Columns.Add("SalesPrice", GetType(Decimal))
    table.Columns.Add("Extension", GetType(String))
    table.Columns.Add("User", GetType(String))
    table.Columns.Add("Department", GetType(String))
    table.Columns.Add("CountryOfOrigin", GetType(String))
    table.Columns.Add("Network", GetType(String))
    table.Columns.Add("ChargeCode", GetType(String))
    table.Columns.Add("Tariff", GetType(String))
    table.Columns.Add("MobileClass", GetType(String))
    table.Columns.Add("RemoteNetwork", GetType(String))


    'open file dialog and store filename'
    Dim openFileDialog1 As New OpenFileDialog
    Dim strFileName As String
    Dim FileNameOnly As String

    openFileDialog1.InitialDirectory = "C:\Indigo\SharePoint\Team Site - Shared Documents\Billing\Daisy"
    openFileDialog1.Filter = "Daisy Calls File|*calls.csv"
    openFileDialog1.FilterIndex = 2
    openFileDialog1.RestoreDirectory = True

    If openFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

    End If
    strFileName = openFileDialog1.SafeFileName
    If strFileName <> "" Then

        FileNameOnly = System.IO.Path.GetFileNameWithoutExtension(openFileDialog1.FileName)

        'Create new table based on specified Tarrif name
        Using con = New SqlConnection("server=barry-laptop\SQLEXPRESS; database=DaisyBilling; integrated security=yes")
            Using cmda = New SqlCommand("CREATE TABLE [" + FileNameOnly + "] (CallType VarChar(30),CustomerCLI VarChar(30),TelephoneNumber VarChar(30),CallDate Date,CallTime time,Duration time,Mb VarChar(30),[Description] VarChar(30),TimeBand VarChar(30),SalesPrice Float,Extension VarChar(30),[User] VarChar(30),Department VarChar(30),CountryOfOrigin VarChar(30),Network VarChar(30),ChargeCode VarChar(30),Tariff VarChar(30),MobileClass VarChar(30),RemoteNetwork VarChar(30));", con)
                con.Open()
                cmda.ExecuteNonQuery()
                con.Close()
            End Using
        End Using

        '--TextField Parser is used to read the files 
        Dim parser As New FileIO.TextFieldParser(openFileDialog1.FileName)


        parser.Delimiters = New String() {","} ' fields are separated by comma
        parser.HasFieldsEnclosedInQuotes = True ' each of the values is enclosed with double quotes
        parser.TrimWhiteSpace = True

        '--First line is skipped , its the header
        parser.ReadLine()

        '-- Add all the rows to datatable
        Do Until parser.EndOfData = True
            table.Rows.Add(parser.ReadFields())
        Loop

        '--Connect to datasource
        Dim SqlconnectionString As String = "server=barry-laptop\SQLEXPRESS; database=DaisyBilling; integrated security=yes"

        '--Import selected file to new tarrif table
        Dim strSql As String = "INSERT INTO [" + FileNameOnly + "] (CallType,CustomerCLI,TelephoneNumber,CallDate,CallTime,Duration,Mb,Description,TimeBand,SalesPrice,Extension,[User],Department,CountryOfOrigin,Network,ChargeCode,Tariff,MobileClass,RemoteNetwork) VALUES (@CallType,@CustomerCLI,@TelephoneNumber,@CallDate,@CallTime,@Duration,@Mb,@Description,@TimeBand,@SalesPrice,@Extension,@User,@Department,@CountryOfOrigin,@Network,@ChargeCode,@Tariff,@MobileClass,@RemoteNetwork)"

        Using connection As New SqlClient.SqlConnection(SqlconnectionString)

            Dim cmd As New SqlClient.SqlCommand(strSql, connection) ' create command objects and add parameters
            With cmd.Parameters

                .Add("@CallType", SqlDbType.VarChar, 30, "CallType")
                .Add("@CustomerCLI", SqlDbType.VarChar, 30, "CustomerCLI")
                .Add("@TelephoneNumber", SqlDbType.VarChar, 30, "TelephoneNumber")
                .Add("@CallDate", SqlDbType.Date, 30, "CallDate")
                .Add("@CallTime", SqlDbType.Time, "CallTime")
                .Add("@Duration", SqlDbType.Time, "Duration")
                .Add("@Mb", SqlDbType.VarChar, 30, "Mb")
                .Add("@Description", SqlDbType.VarChar, 30, "Description")
                .Add("@TimeBand", SqlDbType.VarChar, 30, "TimeBand")
                .Add("@SalesPrice", SqlDbType.Float, 5, "SalesPrice")
                .Add("@Extension", SqlDbType.VarChar, 30, "Extension")
                .Add("@User", SqlDbType.VarChar, 30, "User")
                .Add("@Department", SqlDbType.VarChar, 30, "Department")
                .Add("@CountryOfOrigin", SqlDbType.VarChar, 30, "CountryOfOrigin")
                .Add("@Network", SqlDbType.VarChar, 30, "Network")
                .Add("@ChargeCode", SqlDbType.VarChar, 30, "ChargeCode")
                .Add("@Tariff", SqlDbType.VarChar, 30, "Tariff")
                .Add("@MobileClass", SqlDbType.VarChar, 30, "MobileClass")
                .Add("@RemoteNetwork", SqlDbType.VarChar, 30, "RemoteNetwork")


            End With


            Dim adapter As New SqlClient.SqlDataAdapter()
            adapter.InsertCommand = cmd


            '--Update the original SQL table from the datatable
            Dim iRowsInserted As Int32 = _
                adapter.Update(table)
        End Using

        MessageBox.Show(strFileName & " has been imported...", "Indigo Billing", _
        MessageBoxButtons.OK, MessageBoxIcon.Information)


    Else
        MessageBox.Show("No File Selected", "Indigo Billing", _
    MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End If



End Sub

End Class
导入System.Data.SqlClient
公共类DaisyBillingForm
私有子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
'--首先创建一个与CSV文件具有相同COL的数据表,两个文件中的COL顺序应相同
Dim表作为新数据表()
table.Columns.Add(“CallType”,GetType(String))
table.Columns.Add(“CustomerCLI”,GetType(String))
table.Columns.Add(“电话号码”,GetType(字符串))
table.Columns.Add(“CallDate”,GetType(Date))
table.Columns.Add(“CallTime”,GetType(Integer))
table.Columns.Add(“持续时间”,GetType(整数))
table.Columns.Add(“Mb”,GetType(String))
table.Columns.Add(“Description”,GetType(String))
table.Columns.Add(“时间带”,GetType(字符串))
table.Columns.Add(“SalesPrice”,GetType(十进制))
table.Columns.Add(“扩展名”,GetType(字符串))
table.Columns.Add(“用户”,GetType(字符串))
table.Columns.Add(“部门”,GetType(字符串))
table.Columns.Add(“CountryOfOrigin”,GetType(String))
table.Columns.Add(“网络”,GetType(字符串))
table.Columns.Add(“ChargeCode”,GetType(String))
table.Columns.Add(“关税”,GetType(字符串))
table.Columns.Add(“MobileClass”,GetType(String))
table.Columns.Add(“RemoteNetwork”,GetType(String))
'打开文件对话框并存储文件名'
将openFileDialog1变暗为新OpenFileDialog
将strFileName设置为字符串
仅将文件名设置为字符串
openFileDialog1.InitialDirectory=“C:\Indigo\SharePoint\Team Site-共享文档\Billing\Daisy”
openFileDialog1.Filter=“Daisy Calls File |*Calls.csv”
openFileDialog1.FilterIndex=2
openFileDialog1.RestoreDirectory=True
如果openFileDialog1.ShowDialog=Windows.Forms.DialogResult.OK,则
如果结束
strFileName=openFileDialog1.SafeFileName
如果strFileName为“”,则
FileNameOnly=System.IO.Path.GetFileNameWithoutExtension(openFileDialog1.FileName)
'根据指定的Tarrif名称创建新表
使用con=newsqlconnection(“server=barry laptop\SQLEXPRESS;database=DaisyBilling;integratedsecurity=yes”)
使用cmda=New-SqlCommand(“CREATE TABLE[“+FileNameOnly+”)(CallType VarChar(30)、CustomerCLI VarChar(30)、TelephoneNumber VarChar(30)、CallDate日期、CallTime时间、持续时间、Mb VarChar(30)、[Description]VarChar(30)、TimeBand VarChar(30)、saleprice Float、Extension VarChar(30)、Department VarChar(30)、CountryOfOrigin VarChar(30)、网络VarChar(30)、计费代码VarChar(30)、资费VarChar(30)、移动类VarChar(30)、远程网络VarChar(30));“,con)
con.Open()
cmda.ExecuteNonQuery()
con.Close()
终端使用
终端使用
'--TextField解析器用于读取文件
Dim解析器作为新的FileIO.TextFieldParser(openFileDialog1.FileName)
parser.Delimiters=newstring(){,“}”字段用逗号分隔
parser.HasFieldsEnclosedInQuotes=True'每个值都用双引号括起来
parser.TrimWhiteSpace=True
“--跳过第一行,它是标题
parser.ReadLine()
“--将所有行添加到datatable
直到parser.EndOfData=True为止
table.Rows.Add(parser.ReadFields())
环
“--连接到数据源
Dim SqlconnectionString As String=“server=barry laptop\SQLEXPRESS;database=DaisyBilling;integrated security=yes”
“--将所选文件导入新tarrif表
Dim strSql As String=“插入[“+FileNameOnly+”](呼叫类型、客户号码、电话号码、呼叫日期、呼叫时间、持续时间、Mb、说明、时区、销售价格、分机、[用户]、部门、原产国、网络、费用代码、费率、移动类、远程网络)值(@CallType、@CustomerCLI、@TelephoneNumber、@CallDate、@CallTime、@Duration、@Mb、@Description、@TimeBand、@SalesPrice、@Extension、@User、@Department、@CountryOfOrigin、@Network、@ChargeCode、@private、@MobileClass、@RemoteNetwork))
将连接用作新的SqlClient.SqlConnection(SqlconnectionString)
将cmd设置为新的SqlClient.SqlCommand(strSql,connection)'创建命令对象并添加参数
使用cmd.Parameters
.Add(“@CallType”,SqlDbType.VarChar,30,“CallType”)
.Add(“@CustomerCLI”,SqlDbType.VarChar,30,“CustomerCLI”)
.Add(“@TelephoneNumber”,SqlDbType.VarChar,30,“TelephoneNumber”)
.Add(“@CallDate”,SqlDbType.Date,30,“CallDate”)
.Add(“@CallTime”,SqlDbType.Time,“CallTime”)
.Add(“@Duration”,SqlDbType.Time,“Duration”)
.Add(“@Mb”,SqlDbType.VarChar,30,“Mb”)
.Add(“@Description”,SqlDbType.VarChar,30,“Description”)
.Add(“@TimeBand”,SqlDbType.VarChar,30,“TimeBand”)
.Add(“@SalesPrice”,SqlDbType.Float,5,“SalesPrice”)
.Add(“@Extension”,SqlDbType.VarChar,30,“Extension”)
.Add(“@User”,SqlDbType.VarChar,30,“User”)
.Add(“@Department”,SqlDbType.VarChar,30,“Department”)
.Add(“@CountryOfOrigin”,SqlDbType.VarChar,30,“CountryOfOrigin”)
.Add(“@Network”,SqlDbType.VarChar,30,“Network”)
.Add(“@ChargeCode”,SqlDbType.VarChar,30,“ChargeCode”)
.Add(“@Tariff”,SqlDbType.VarChar,30,“Tariff”)
.Add(“@MobileClass”,SqlDbType.VarChar,30,“MobileClass”)
.Add(“@RemoteNetwork”,SqlDbType.VarChar,30,“RemoteNetw