我需要将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”。 PLZ解决我的问题.. 谢谢。
答案 0 :(得分: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