我是VB.NET的新手。我制作了一个程序,用于在gridDataView中显示Excel文件并将其导出到MySQL数据库 但是我得到了这个警告:
" System.ArgumentOutOfRangeException未处理"
有人可以帮忙吗?
以下是代码:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Dim cn As New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\a.malibate\Desktop\test.xls; Extended Properties=Excel 8.0;")
cn.Open()
Dim da As New OleDbDataAdapter("select * from [Sheet1$]", cn)
Dim dt As New DataTable
da.Fill(dt)
Catch ex As OleDbException
MsgBox(ex.Message)
End Try
'setting the datagrid view
With DataGridView1
.AllowUserToAddRows() = False
.BorderStyle() = BorderStyle.None
.Columns(0).HeaderText = "Id"
.Columns(1).HeaderText = "Name"
.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End With
'clearing memory
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
BtnSave.Enabled = False
Application.DoEvents()
Dim cn As New SqlConnection("server=LENEL;database=PENSION;Integrated Security=SSPI")
cn.Open()
Dim cm As New SqlCommand
With cm
.Connection = cn
For i As Integer = 0 To DataGridView1.RowCount - 1
.CommandText = "insert into member_temporary values('" & DataGridView1.Rows(i).Cells(0).Value & "','" & DataGridView1.Rows(i).Cells(1).Value & "')"
.ExecuteNonQuery()
Next
End With
cm.Dispose()
cm = Nothing
With cn
.Close()
.Dispose()
End With
cn = Nothing
MsgBox("Done")
BtnSave.Enabled = True
End Sub
答案 0 :(得分:0)
更改此
For i As Integer = 0 To DataGridView1.RowCount - 1
到这个
For i As Integer = 1 To DataGridView1.RowCount
datagridview中没有第0行。它从1到行数。