2013-04-03 37 views
1

我需要將xl表的數據導入到數據庫中,並在數據庫中保存時,我需要向其中添加另一列並保存它。我使用下面的代碼:-------如何從xl導入數據後將新列添加到數據庫中

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 

問題是,它不顯示任何錯誤,但節省數據庫中的數據,在chalan_no列後,呈現持續「16」 。 PLZ解決我的問題.. 謝謝。

+0

你在chalan_no變量中得到什麼? – DevelopmentIsMyPassion

+0

您始終從循環內的相同文本框中獲取參數「@ chalan_no」的值。應該預計它不會改變。 – Steve

+0

我從chalan_no文本框中獲取文本....和@ steve..yes所有記錄即時導入chalan_no保持不變... – Harabati

回答

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 
+0

是的..它與小的變化一起工作......謝謝...謝謝很多 – Harabati

+0

@Harabati你是最受歡迎的。你做了什麼改變。你能告訴? – DevelopmentIsMyPassion

+0

'昏暗OleDbType作爲新OleDb.OleDbType 隨着cmd.Parameters 。新增( 「@ P1」,OleDbType.VarChar)。價值= dr.GetValue(0) 。新增( 「@ P2」,OleDbType.VarChar)。值= cmbChal_noImport.Text End With' Nothing ...只是宣佈OleDbType ....再次感謝AshReva – Harabati

相關問題