我正在使用VS 2010和基于服务的数据库和Vb.net
我想使用DataGridView将数据插入数据库。虽然DataGridView中存在的所有行都插入到数据库中,但是在包含ID的开头也插入了另外的行作为" 0"并将其他栏目留空,我根本不想要。
我附上了代码和数据库表供您查看。 请帮我解决这个问题,
Dim Sql_ConnectionString = " DataSource=.\SQLEXPRESS;AttachDbFilename=D:\databaseapplication\DataGridView\WindowsApplication1\WindowsApplication1\Database1.mdf;IntegratedSecurity=True;User Instance=True"[Database Table Pic][1]
Try
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim dataAdapter As New Data.SqlClient.SqlDataAdapter
con.ConnectionString = Sql_ConnectionString
cmd.Connection = con
con.Open()
For Each row As DataGridViewRow In DataGridView2.Rows
cmd.CommandText = "insert into Table4 (ID,Name,Marks) values ('" + row.Cells("cus_ID").Value + "','" + row.Cells("cus_Name").Value + "','" + row.Cells("cus_Marks").Value + "')"
cmd.ExecuteNonQuery()
Next
con.Close()