简体   繁体   English

值以字符串形式而不是整数形式传递

[英]Values being passed as strings and not integers

I am trying to collect a series of values as a string and then insert them to an SQL server dB. 我试图收集一系列值作为字符串,然后将它们插入到SQL Server dB中。 I have this working fine with just 'VarChar', but when I try to load collumns that contain numbers is fails. 我只能使用'VarChar'正常工作,但是当我尝试加载包含数字的列时会失败。

It keeps throwing the below error about converting from string to integer. 它不断抛出以下有关从字符串转换为整数的错误。

"Failed to convert parameter value from a String to a Int32." “无法将参数值从字符串转换为Int32。”

Please can somebody help me with the correct syntax. 请有人帮我正确的语法。

Below is a copy of my code: 以下是我的代码的副本:

    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

I have had a search on here and believe I might need to convert them, but I am not sure what I am converting them to or from. 我在这里进行了搜索,并认为可能需要对其进行转换,但是我不确定我要与之转换的内容。

Any help greatly appreciated. 任何帮助,不胜感激。

Thanks 谢谢

Try to mark your columns with data type like 尝试用以下数据类型标记列

table.Columns.Add("CallType", GetType(Integer))
table.Columns.Add("ChargeCode", GetType(Integer))
table.Columns.Add("Destination", GetType(string))
table.Columns.Add("TariffUsed", GetType(string))

I have just marked them randomly. 我刚刚对它们进行了随机标记。 This will work for you as Column will be of specific data type and solve your issue of conversion. 这将对您有用,因为Column将具有特定的数据类型并解决您的转换问题。

The SQL Command has parameter values with two words: @Minimum Charge,@Charge Cap,@Initial Units You can't do that. SQL命令的参数值包含两个单词: @Minimum Charge,@Charge Cap,@Initial Units您不能这样做。 Do this instead: @MinimumCharge,@ChargeCap,@InitialUnits 改为执行此操作: @MinimumCharge,@ChargeCap,@InitialUnits

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM