如何将多个datagridview数据保存到vb.net中的数据库中的表

时间:2014-07-01 07:46:15

标签: vb.net

我有一个填充了数据集的datagridview,我想将它拥有的所有记录存储到数据库中的新表中。我添加了一个新列,(“pos”)到一个数据集,通过一个查询填充datagridview,以便它根据一个字段(总)排名记录。我的问题是如何循环所有记录来存储它们因为错误我get is(对象引用未设置为对象的实例)。可以有人帮助我PLIZ,av尝试了几周但没有成功,,,或者有更好的方法我可以做到这一点。 这是我的代码

Dim comm As SqlCommand = New SqlCommand()
Dim conn As SqlConnection = New SqlConnection
conn = New SqlConnection(cs1)
comm.Connection = conn
conn.Open()
Dim sname, adm, class1, yr, term, examType, eng, kisw, math, bio, chem, phy, hist, cre, geo, agr, biz, total, pos, grade As String
For i As Integer = 0 To DataGridView1.Rows.Count - 1
sname = Me.DataGridView1.Rows(i).Cells("sname").Value.ToString()
adm = Me.DataGridView1.Rows(i).Cells("adm").Value.ToString()
class1 = Me.DataGridView1.Rows(i).Cells("class").Value.ToString
examType = Me.DataGridView1.Rows(i).Cells("examType").Value.ToString
yr = Me.DataGridView1.Rows(i).Cells("yr").Value.ToString
math = Me.DataGridView1.Rows(i).Cells("math").Value.ToString
eng = Me.DataGridView1.Rows(i).Cells("eng").Value.ToString
kisw = Me.DataGridView1.Rows(i).Cells("kisw").Value.ToString
bio = Me.DataGridView1.Rows(i).Cells("bio").Value.ToString
phy = Me.DataGridView1.Rows(i).Cells("phy").Value.ToString
chem = Me.DataGridView1.Rows(i).Cells("chem").Value.ToString()
hist = Me.DataGridView1.Rows(i).Cells("hist").Value.ToString()
cre = Me.DataGridView1.Rows(i).Cells("cre").Value.ToString()
geo = Me.DataGridView1.Rows(i).Cells("geo").Value.ToString()
biz = Me.DataGridView1.Rows(i).Cells("biz").Value.ToString()
total = Me.DataGridView1.Rows(i).Cells("total").Value.ToString()
agr = Me.DataGridView1.Rows(i).Cells("agr").Value.ToString()
term = Me.DataGridView1.Rows(i).Cells("term").Value.ToString()
pos = Me.DataGridView1.Rows(i).Cells("pos").Value.ToString()
grade = Me.DataGridView1.Rows(i).Cells("grade").Value.ToString()
comm.CommandText = "insert into tblExamResults(sname,adm,class,examType,yr,math,eng,kisw,bio,chem,phy,hist,cre,geo,biz,total,agr,term,pos,grade) values('" & sname & "','" & adm & "','" & class1 & "','" & examType & "','" & yr & "','" & math & "','" & eng & "','" & kisw & "','" & bio & "','" & chem & "','" & phy & "','" & hist & "','" & cre & "','" & geo & "','" & biz & "','" & total & "','" & agr & "','" & term & "','" & pos & "','" & grade & "')"
comm.ExecuteNonQuery()
Next
con.Close()

0 个答案:

没有答案