下面是我的代码:
点BUTTON1 将DATAGRIDVIEW 的内容更新到数据库中
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Private conn As SqlConnection = Nothing
Private dp1 As SqlDataAdapter = Nothing
Private dt As DataSet = Nothing
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
conn.open()
dp1.Update(dt.Tables(0))
conn.Close()
MessageBox.Show("OK", "update", MessageBoxButtons.OK)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn = New SqlConnection("data source=alan;initial catalog=dbhx;user id=sa;password=sa")
conn.Open()
Dim cmd As New SqlCommand("select * from t_emp")
cmd.CommandType = CommandType.Text
cmd.Connection = conn
dp1 = New SqlDataAdapter(cmd)
dt = New DataSet
dp1.Fill(dt)
dg.DataSource = dt.Tables(0)
conn.Close()
End Sub
End Class