我正在尝试将一些持续时间值从CSV文件上传到我的SQL Server dB。
我尝试加载的值采用以下格式:hh:mm:ss。
我正在使用下面的代码,但它会引发有关转换数据类型的各种错误。当前错误是“输入字符串格式不正确。不能在CallTime列中存储< 09:23:53>。预期类型为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
任何人都可以帮助SQL服务器使用正确的数据类型。
非常感谢任何帮助。
答案 0 :(得分:0)
GetType(TimeSpan)我认为你想要的是http://msdn.microsoft.com/en-us/library/cc716729(v=vs.110).aspx。