球高人帮我看看这错在哪了。
Imports System.Data.SqlClientImports System.Data
Public Class Form1
Dim cn As SqlConnection
Dim da As SqlDataAdapter
Dim ds As DataSet
Dim dt As DataTable
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
cn = New SqlConnection()
cn.ConnectionString = "Data Source=XP-201311231519;Initial Catalog=小型数据库;Integrated Security=False;User ID=sa;Password=123456;"
cn.Open()
da = New SqlDataAdapter("select * from 加工量", cn)
ds = New DataSet
da.Fill(ds, "mytb1")
Dim dr As DataRow
dr = ds.Tables("mytb1").NewRow
For i = 0 To DataGridView1.Rows.Count - 1
dr("产品编码") = DataGridView1.Rows(i).Cells(1).Value
dr("产品名称") = DataGridView1.Rows(i).Cells(2).Value
dr("批次") = DataGridView1.Rows(i).Cells(3).Value
ds.Tables("mytb1").Rows.Add(dr)
Dim scb As New SqlCommandBuilder(da)
da.Update(ds, "mytb1")
DataGridView1.DataSource = ds.Tables("mytb1")
Next
cn.Close()
Catch ex As Exception
End Try
End Sub
End Class
要不帮我看一下这个,为什么会有问题?就是dr("产品编码") = DataGridView1.Rows(i).Cells(1).Value
这句话,Rows(i)这个i用数字代替就可以用不出错,用变量就不行。求高人指点迷津。(代码是从书上抄的,源代码DataGridView1.Rows(i).Cells(1).Value这里是文本框)。
想要用DataGridView1批量更新数据库。