值作为字符串而不是整数传递

时间:2014-04-28 13:23:17

标签: sql-server vb.net visual-studio-2010

我试图将一系列值作为字符串收集,然后将它们插入SQL服务器dB。只有' VarChar'我才能正常工作,但是当我尝试加载包含数字的列时失败。

它不断抛出有关从字符串转换为整数的错误。

"无法将参数值从String转换为Int32。"

请有人帮我正确的语法。

以下是我的代码的副本:

    Dim table As New DataTable()

    table.Columns.Add("CallType")
    table.Columns.Add("ChargeCode")
    table.Columns.Add("Destination")
    table.Columns.Add("TariffUsed")
    table.Columns.Add("Peak")
    table.Columns.Add("OffPeak")
    table.Columns.Add("Weekend")
    table.Columns.Add("Setup")
    table.Columns.Add("Minimum Charge")
    table.Columns.Add("Charge Cap")
    table.Columns.Add("Initial Units")
    table.Columns.Add("Initial Charge")
    table.Columns.Add("Initial Peak")
    table.Columns.Add("Initial Off Peak")
    table.Columns.Add("Initial Weekend")
    table.Columns.Add("Billing Unit")
    table.Columns.Add("Minimum Units")
    table.Columns.Add(" RateType")

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

    openFileDialog1.InitialDirectory = "C:\Users\Barry\Documents\Indigo Billing dB\Daisy Call Rates"
    openFileDialog1.Filter = "CSV files (*.csv)|*.csv"
    openFileDialog1.FilterIndex = 2
    openFileDialog1.RestoreDirectory = True

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

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

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

        MessageBox.Show("New Rate Card Loaded...", "Indigo Billing", _
        MessageBoxButtons.OK, MessageBoxIcon.Information)

        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

        '--Create SQL query
        Dim strSql As String = "INSERT INTO DaisyRatesImport (CallType,ChargeCode,Destination,TariffUsed,Peak,OffPeak,Weekend,Setup,Minimum Charge,Charge Cap,Initial Units,Initial Charge,Initial Peak,Initial Off Peak,Initial Weekend,Billing Unit,Minimum Units, RateType) VALUES (@CallType,@ChargeCode,@Destination,@TariffUsed,@Peak,@OffPeak,@Weekend,@Setup,@Minimum Charge,@Charge Cap,@Initial Units,@Initial Charge,@Initial Peak,@Initial Off Peak,@Initial Weekend,@Billing Unit,@Minimum Units, @RateType)"
        Dim SqlconnectionString As String = "server=barry-laptop\SQLEXPRESS; database=Test; integrated security=yes"
        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("@ChargeCode", SqlDbType.VarChar, 30, "ChargeCode")
                .Add("@Destination", SqlDbType.VarChar, 30, "Destination")
                .Add("@TariffUsed", SqlDbType.VarChar, 30, "TariffUsed")
                .Add("@Peak", SqlDbType.Int, 5, "Peak")
                .Add("@OffPeak", SqlDbType.Int, 5, "OffPeak")
                .Add("@Weekend", SqlDbType.Int, 5, "Weekend")
                .Add("@Setup", SqlDbType.Int, 5, "Setup")
                .Add("@Minimum Charge", SqlDbType.Int, 5, "Minimum Charge")
                .Add("@Charge Cap", SqlDbType.Int, 5, "Charge Cap")
                .Add("@Initial Units", SqlDbType.Int, 5, "Initial Units")
                .Add("@Initial Charge", SqlDbType.Int, 5, "Initial Charge")
                .Add("@Initial Peak", SqlDbType.Int, 5, "Initial Peak")
                .Add("@Initial Off Peak", SqlDbType.Int, 5, "Initial Off Peak")
                .Add("@Initial Weekend", SqlDbType.Int, 5, "Initial Weekend")
                .Add("@Billing Unit", SqlDbType.Int, 5, "Billing Unit")
                .Add("@Minimum Units", SqlDbType.Int, 5, "Minimum Units")
                .Add("@ RateType", SqlDbType.VarChar, 30, " RateType")

            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
    Else
        MessageBox.Show("No File Selected", "Indigo Billing", _
    MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End If

    'displayes file name in Textbox1
    TextBox1.Text = strFileName

End Sub

我在这里搜索并相信我可能需要转换它们,但我不确定我将它们转换成来自哪里。

非常感谢任何帮助。

由于

2 个答案:

答案 0 :(得分:1)

尝试使用类似

的数据类型标记列
table.Columns.Add("CallType", GetType(Integer))
table.Columns.Add("ChargeCode", GetType(Integer))
table.Columns.Add("Destination", GetType(string))
table.Columns.Add("TariffUsed", GetType(string))

我刚刚随机标记了它们。这将对您有用,因为Column将具有特定的数据类型并解决您的转换问题。

答案 1 :(得分:0)

SQL命令的参数值包含两个单词:@Minimum Charge,@Charge Cap,@Initial Units您无法执行此操作。请改为:@MinimumCharge,@ChargeCap,@InitialUnits