System.Data.OleDb.OleDbException 无效参数

Posted

技术标签:

【中文标题】System.Data.OleDb.OleDbException 无效参数【英文标题】:System.Data.OleDb.OleDbException invalid argument 【发布时间】:2013-11-02 21:32:10 【问题描述】:

我正在上传一个 Excel 工作表,并且想将其数据移动到数据库中的另一个表中。最近两天我遇到了一个错误(System.Data.OleDb.OleDbException invalid argument),但是,如果我设计一个非常简单的应用程序,它就可以正常工作。请帮帮我

使用 conn 作为新的 SqlConnection(ConfigurationManager.ConnectionStrings("TERAMSConnectionString").ConnectionString) 暗淡路径为字符串 = FileUpload1.PostedFile.FileName Dim excelConnectionString As String = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + (path + ";Extended Properties=Excel 12.0;Persist Security Info=False")) 将 excelConnection 调暗为 OleDbConnection = New OleDbConnection(excelConnectionString) conn.Open() excelConnection.Open() Dim cmd As OleDbCommand = New OleDbCommand("Select * from [Sheet1$]", excelConnection) '清除任何以前的数据 Dim sClearSQL = "DELETE FROM Desktop_Compare" Dim SqlCmd As SqlCommand = New SqlCommand(sClearSQL, conn) SqlCmd.ExecuteNonQuery() Dim dReader As OleDbDataReader = cmd.ExecuteReader Dim sqlBulk As SqlBulkCopy = New SqlBulkCopy(conn) excelConnection.Close() 连接关闭() 结束使用 第 74 行:Dim excelConnection As OleDbConnection = New OleDbConnection(excelConnectionString) 第 75 行:conn.Open() 第 76 行:**excelConnection.Open()** 第 77 行:Dim cmd As OleDbCommand = New OleDbCommand("Select * from [Sheet1$]", excelConnection) 第 78 行:'清除任何以前的数据

【问题讨论】:

您在什么时候得到异常?作为旁注,我建议使用& 来连接字符串而不是+(我已经看到+ 导致一些奇怪的问题,因为它试图将字符串“添加”在一起而不是加入它们) @Grahamvs 好的,我在“excelConnection.Open()”处遇到错误,告诉用户代码未处理。 【参考方案1】:

尝试将连接字符串更改为:

Dim excelConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & path & ";"

如果这不起作用,请尝试一次更改/添加 Extended PropertiesPersist Security Info,因为这似乎是您的 ACE 驱动程序有问题,或者其他参数之一导致问题

至于您的用户收到未处理的错误,您可以尝试以下方法:

        Dim excelConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & path & ";"

        Using excelConnection As OleDb.OleDbConnection = New OleDb.OleDbConnection(excelConnectionString)
            Try
                excelConnection.Open()

                Using cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("Select * from [Sheet1$]", excelConnection)

                    'Clears any previous data
                    Dim sClearSQL As String = "DELETE FROM Desktop_Compare "
                    Using SqlCmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(sClearSQL, conn)
                        SqlCmd.ExecuteNonQuery()
                    End Using

                    Dim dReader As OleDb.OleDbDataReader = cmd.ExecuteReader
                    Dim sqlBulk As SqlClient.SqlBulkCopy = New SqlClient.SqlBulkCopy(conn)
                    excelConnection.Close()

                End Using

            Catch ex As Exception
                ' Handle errors here

            Finally
                If Not excelConnection.State = ConnectionState.Closed Then
                    Try
                        excelConnection.Close()
                    Catch ex As Exception
                    End Try
                End If

            End Try

        End Using

【讨论】:

【参考方案2】:

首先将 excel 数据加载到 datagridview 中。之后,来自gridview的所有数据,您必须将它们插入到表数据库中。它会解决你的问题。 你必须试试这个!

先放--

1 个按钮 - btnOpen -

1 个文本框 - txtOpen -

再次1个按钮-btnLoad-

1 数据网格视图-DataGridView3-

1 个组合框 - cboSheet

在 OpenButton 中。放这段代码

 Dim OpenFileDialog1 As New OpenFileDialog()
    Dim constr As String
    Dim con As OleDb.OleDbConnection

    Try

        OpenFileDialog1.Filter = "Excel Files | *.xlsx; *.xls; *.xlsm;"

        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Me.txtOpen.Text = OpenFileDialog1.FileName

            constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtOpen.Text + ";Excel 12.0 Xml;HDR=YES"
            con = New OleDb.OleDbConnection(constr)
            con.Open()

            cboSheet.DataSource = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
            cboSheet.DisplayMember = "TABLE_NAME"
            cboSheet.ValueMember = "TABLE_NAME"
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

在加载按钮中把这段代码放在下面

 Dim constr As String
    Dim dt As DataTable
    Dim con As OleDbConnection
    Dim sda As OleDbDataAdapter
    Dim row As DataRow

    Try
        constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtOpen.Text + ";Excel 12.0 Xml;HDR=YES"
        con = New OleDbConnection(constr)
        sda = New OleDbDataAdapter("Select * from [" + cboSheet.SelectedValue + "]", con)
        dt = New DataTable
        sda.Fill(dt)

        For Each row In dt.Rows
            DataGridView3.DataSource = dt

        Next
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

然后将所有数据保存到表数据库中。在下面使用此代码

'Dim nu As NullReferenceException
    Dim cmd As OleDbCommand
    connection.Open()
    For i As Integer = 0 To DataGridView3.Rows.Count - 2 Step +1

        'cmd = New OleDbCommand("INSERT INTO QAtable([CompanyCode],[Vendor],[G/L_Account],[DocumentType],[DocumentNumber],[DocumentDate],[EntryDate],[PostingDate],[NetDueDate],[ClearingDate],[ClearingDocument],[DocumentCurrency],[AmountInDocumentCurrency],[LocalCurrency],[AmountInLocalCurrency],[LocalCurrency2],[AmountInLocalCurrency2],[LocalCurrency3],[AmountInLocalCurrency3],[PartnerBankType],[PaymentBlock],[PaymentMethod],[Text],[DocumentHeaderText],[Assignment],[Username],[ABSvalueInAUD],[High/Low],[AmountRange]) VALUES (@CompanyCode,@Vendor,@G/L_Account,@DocumentType,@DocumentNumber,@DocumentDate,@EntryDate,@PostingDate,@NetDueDate,@ClearingDate,@ClearingDocument,@DocumentCurrency,@AmountInDocumentCurrency,@LocalCurrency,@AmountInLocalCurrency,@LocalCurrency2,@AmountInLocalCurrency2,@LocalCurrency3,@AmountInLocalCurrency3,@PartnerBankType,@PaymentBlock,@PaymentMethod,@Text,@DocumentHeaderText,@Assignment,@Username,@ABSvalueInAUD,@High/Low,@AmountRange)", connection)

        cmd = New OleDbCommand("INSERT INTO QAtable([CompanyCode],[Vendor],[GLAccount],[DocumentType],[DocumentNumber],[Reference],[DocumentDate],[EntryDate],[PostingDate],[NetDueDate],[ClearingDate],[ClearingDocument],[DocumentCurrency],[AmountInDocumentCurrency],[LocalCurrency],[AmountInLocalCurrency],[LocalCurrency2],[AmountInLocalCurrency2],[LocalCurrency3],[AmountInLocalCurrency3],[PartnerBankType],[PaymentBlock],[PaymentMethod],[Text],[DocumentHeaderText],[Assignment],[Username],[IncludeExclude],[GBSNonGBS],[ABSvalueInAUD],[HighLow],[AmountRange],[User]) VALUES (@CompanyCode,@Vendor,@GLAccount,@DocumentType,@DocumentNumber,@Reference,@DocumentDate,@EntryDate,@PostingDate,@NetDueDate,@ClearingDate,@ClearingDocument,@DocumentCurrency,@AmountInDocumentCurrency,@LocalCurrency,@AmountInLocalCurrency,@LocalCurrency2,@AmountInLocalCurrency2,@LocalCurrency3,@AmountInLocalCurrency3,@PartnerBankType,@PaymentBlock,@PaymentMethod,@Text,@DocumentHeaderText,@Assignment,@Username,@IncludeExlcude,@GBSNonGBS,@ABSvalueInAUD,@HighLow,@AmountRange,@User)", connection)
        cmd.Parameters.Add("@CompanyCode", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(0).Value.ToString()
        cmd.Parameters.Add("@Vendor", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(1).Value.ToString()
        cmd.Parameters.Add("@GLAccount", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(2).Value.ToString()
        cmd.Parameters.Add("@DocumentType", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(3).Value.ToString()
        cmd.Parameters.Add("@DocumentNumber", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(4).Value.ToString()

        cmd.Parameters.Add("@Reference", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(5).Value.ToString()

        cmd.Parameters.Add("@DocumentDate", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(6).Value.ToString()
        cmd.Parameters.Add("@EntryDate", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(7).Value.ToString()
        cmd.Parameters.Add("@PostingDate", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(8).Value.ToString()
        cmd.Parameters.Add("@NetDueDate", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(9).Value.ToString()
        cmd.Parameters.Add("@ClearingDate", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(10).Value.ToString()
        cmd.Parameters.Add("@ClearingDocument", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(11).Value.ToString()
        cmd.Parameters.Add("@DocumentCurrency", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(12).Value.ToString()
        cmd.Parameters.Add("@AmountInDocumentCurrency", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(13).Value.ToString()
        cmd.Parameters.Add("@LocalCurrency", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(14).Value.ToString()
        cmd.Parameters.Add("@AmountInLocalCurrency", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(15).Value.ToString()
        cmd.Parameters.Add("@LocalCurrency2", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(16).Value.ToString()
        cmd.Parameters.Add("@AmountInLocalCurrency2", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(17).Value.ToString()
        cmd.Parameters.Add("@LocalCurrency3", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(18).Value.ToString()
        cmd.Parameters.Add("@AmountInLocalCurrency3", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(19).Value.ToString()
        cmd.Parameters.Add("@PartnerBankType", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(20).Value.ToString()
        cmd.Parameters.Add("@PaymentBlock", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(21).Value.ToString()
        cmd.Parameters.Add("@PaymentMethod", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(22).Value.ToString()
        cmd.Parameters.Add("@Text", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(23).Value.ToString()
        cmd.Parameters.Add("@DocumentHeaderText", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(24).Value.ToString()
        cmd.Parameters.Add("@Assignment", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(25).Value.ToString()
        cmd.Parameters.Add("@Username", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(26).Value.ToString()

        cmd.Parameters.Add("@IncludeExclude", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(27).Value.ToString()
        cmd.Parameters.Add("@GBSNonGBS", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(28).Value.ToString()

        cmd.Parameters.Add("@ABSvalueInAUD", OleDbType.Double).Value = DataGridView3.Rows(i).Cells(29).Value()
        cmd.Parameters.Add("@HighLow", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(30).Value.ToString()
        cmd.Parameters.Add("@AmountRange", OleDbType.VarChar).Value = DataGridView3.Rows(i).Cells(31).Value.ToString()
        cmd.Parameters.AddWithValue("@User", txtUser.Text)

        cmd.ExecuteNonQuery()




    Next
    connection.Close()
    MessageBox.Show("All Data Inserted")

我希望它会起作用! :)

【讨论】:

只需编辑我的代码。因为我将它们复制到我的系统中。谢谢!

以上是关于System.Data.OleDb.OleDbException 无效参数的主要内容,如果未能解决你的问题,请参考以下文章