我有以下代码显示来自我的一个SQL表的数据并将其显示在datagridview中。
我现在正在尝试在ViewCustomerForm上放置一个保存按钮,以便将记录保存回我的SQL表。
以下是显示数据的代码。
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Using con As New SqlClient.SqlConnection("server=barry-laptop\SQLEXPRESS; database=BillingReferenceData; integrated security=yes")
Dim cmd As SqlCommand = New SqlCommand("Select * FROM Customers", con)
con.Open()
Dim myDA As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim myDataSet As DataSet = New DataSet()
myDA.Fill(myDataSet, "Customers")
ViewCustomersForm.DataGridView1.DataSource = myDataSet.Tables("Customers")
con.Close()
ViewCustomersForm.Show()
End Using
End Sub
有人可以帮我解决“保存”按钮的正确语法
非常感谢任何帮助。
答案 0 :(得分:0)
这样的东西?
Private Sub button1_Click(sender As Object, e As EventArgs)
For i As Integer = 0 To dataGridView1.Rows.Count - 1
Dim strQuery As String = ("INSERT INTO tableName VALUES (" + dataGridView1.Rows(i).Cells("ColumnName").Value & ", ") + dataGridView1.Rows(i).Cells("ColumnName").Value & ");"
Try
Using conn = New SqlConnection(ConnString)
Using comm = New SqlCommand(strQuery, conn)
conn.Open()
comm.ExecuteNonQuery()
End Using
End Using
Catch generatedExceptionName As InvalidCastException
End Try
Next
End Sub
请记住,您仍然需要使用datagridview等变量。
grtz
FOSA