1

我需要将 xl 表的数据导入数据库,当它保存在 db 中时,我需要向其中添加另一列并保存它。我正在使用以下代码:--------

Private Sub cmdImport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdImport.Click
    Dim _filename As String = txtFile.Text
    'Create connection object for xl sheet
    Dim _conn As String
    _conn = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & _filename & ";" & "Extended Properties=Excel 8.0;"
    Dim _connection As OleDb.OleDbConnection = New OleDb.OleDbConnection(_conn)

    'List columns you need from the Excel file   
    Dim _command As New System.Data.OleDb.OleDbCommand("Select * FROM [Sheet1$]", _connection)

    'open connection for xl sheet
    _connection.Open()

    ' Create DbDataReader to read Data from xl sheet 
    Dim dr As System.Data.OleDb.OleDbDataReader = _command.ExecuteReader()

    'open connection for database to write into it
    cnnOLEDB.Open()
    Dim chal_no As String
    Try
        'reading data from xl sheet utill the last rows
        If dr.HasRows() Then
            While dr.Read()
                'writing the read data from xl sheet into access database
                chalan_no = cmbChal_noImport.Text

                'getting the parameter values from xl sheet to write into access db
                Dim P1 As New OleDb.OleDbParameter
                P1.DbType = DbType.String
                P1.ParameterName = "sr_no"
                P1.Value = dr.GetValue(0)

                'adding the parameters in to the db
                Dim strSql As String = "INSERT INTO Vendor_Machine(sr_no,chalan_no) VALUES (@srno,@chalan_no)"
                Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(strSql, cnnOLEDB)

                cmd.Parameters.AddRange(New OleDb.OleDbParameter() {P1})
                cmd.Parameters.AddWithValue("@srno", DbType.String)
                cmd.Parameters.AddWithValue("@chalan_no", chalan_no)

                cmd.ExecuteScalar()

            End While

        End If
        MsgBox("Xl sheet Import Complete", MsgBoxStyle.OkOnly)

    Catch ex As Exception

        MsgBox(ex.Message)
        Exit Sub

    Finally

        'closing the access and xl sheet connection
        cnnOLEDB.Close()
        _connection.Close()

    End Try
    'End Using

End Sub

问题是,它没有显示任何错误,但是在将数据保存到 db 之后,在 chalan_no 列中它显示一个常量“16”。请解决我的问题..谢谢。

4

1 回答 1

1

我不确定这段代码是否能解决您的问题。但这比你的要简单一些。

Private Sub cmdImport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdImport.Click
Dim _filename As String = txtFile.Text
'Create connection object for xl sheet
Dim _conn As String
_conn = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & _filename & ";" & "Extended Properties=Excel 8.0;"
Dim _connection As OleDb.OleDbConnection = New OleDb.OleDbConnection(_conn)

'List columns you need from the Excel file   
Dim _command As New System.Data.OleDb.OleDbCommand("Select * FROM [Sheet1$]", _connection)

'open connection for xl sheet
_connection.Open()

' Create DbDataReader to read Data from xl sheet 
Dim dr As System.Data.OleDb.OleDbDataReader = _command.ExecuteReader()

'open connection for database to write into it
cnnOLEDB.Open()
Dim chal_no As String
Try
    'reading data from xl sheet utill the last rows
    If dr.HasRows() Then
        While dr.Read()
            'writing the read data from xl sheet into access database
            chalan_no = cmbChal_noImport.Text

            'getting the parameter values from xl sheet to write into access db
            Dim P1 As New OleDb.OleDbParameter
            'P1.DbType = DbType.String
            'P1.ParameterName = "sr_no"
            'P1.Value = dr.GetValue(0)

            'adding the parameters in to the db
            Dim strSql As String = "INSERT INTO Vendor_Machine(sr_no,chalan_no) VALUES (?,?)"
            Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(strSql, cnnOLEDB)
            cmd.CommandType = CommandType.Text

             With cmd.Parameters
               .Add("@p1", OleDbType.VarChar).Value =  dr.GetValue(0)
               .Add("@p2", OleDbType.VarChar).Value = cmbChal_noImport.Text
             End With
            ' cmd.Parameters.AddRange(New OleDb.OleDbParameter() {P1})
            'cmd.Parameters.AddWithValue("@srno", DbType.String)
            'cmd.Parameters.AddWithValue("@chalan_no", chalan_no)
            cmd.ExecuteNonQuery()
            'cmd.ExecuteScalar()

        End While

    End If
    MsgBox("Xl sheet Import Complete", MsgBoxStyle.OkOnly)

Catch ex As Exception

    MsgBox(ex.Message)
    Exit Sub

Finally

    'closing the access and xl sheet connection
    cnnOLEDB.Close()
    _connection.Close()

End Try
 'End Using

End Sub
于 2013-04-03T11:05:00.547 回答