菜鳥求助
form1里有DataGridView1,里面的資料是從火鳥資料庫里查詢出來的(感謝“ 一碗平安”寫的程式)
form1里有DataGridView2,里面的資料是從SQL資料庫里查詢出來看的
我現在需要將DataGridView1里面的資料保存到SQL資料庫里面
請會的人在不忙的時間,幫我看下這網上找的代碼,多謝
Private Sub 保存_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 保存.Click
Dim sqlStr As String = "insert into product values("
Dim i As Integer
For i = 0 To DataGridView1.Columns.Count - 1
sqlStr &= "" & CType(DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(i).Value.ToString) & ""
If i < DataGridView1.Columns.Count - 1 Then sqlStr &= ","
Next
sqlStr &= ")"
Dim cmd As DBCommand = New DBCommand(New ConnectionString().ConnectionInfo)
If cmd.Insert(sqlStr) > 0 Then
MsgBox("保存成功!", MsgBoxStyle.DefaultButton1, "業務模塊")
Else
MsgBox("保存失敗!請聯系系統管理員。")
End If
End Sub
uj5u.com熱心網友回復:
你這個保存事件是 product添加一行資料遍歷DataGridView1的列行數的,行取最後一行數據,列取遍歷的列數,
uj5u.com熱心網友回復:
首先你要從事件中讀取到 行的資料然后在 保存到資料庫。你的流程有問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/133019.html
標籤:VB.NET
