我需要將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解決我的問題.. 謝謝。
你在chalan_no變量中得到什麼? – DevelopmentIsMyPassion
您始終從循環內的相同文本框中獲取參數「@ chalan_no」的值。應該預計它不會改變。 – Steve
我從chalan_no文本框中獲取文本....和@ steve..yes所有記錄即時導入chalan_no保持不變... – Harabati