vb.net中如何把DataGridView当前行的值跟新到ACCESS数据库
2个回答
展开全部
参考一下下面这段代码:
Imports System.Data.OleDb
Public Class Form1
Dim SqlCOM As OleDbConnection
Dim SqlUse As OleDbDataAdapter
Dim Dt As DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
'连接数据库
SqlCOM = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\LP\Documents\Database1.accdb")
'执行数据命令获取表
SqlUse = New OleDbDataAdapter("SELECT * FROM 表1", SqlCOM)
Dt = New DataSet
SqlUse.Fill(Dt) '放入dataset中
Catch
MsgBox(Err.Description, 48)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
DataGridView1.DataSource = Dt.Tables(0).DefaultView 'DVG与DADASET同步数据显示
Catch
MsgBox(Err.Description, 32)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Dim ass As New OleDbCommandBuilder(SqlUse) '关联DataSet和数据库的操作(更新)
SqlUse.UpdateCommand = ass.GetUpdateCommand() '执行更新所需的 OleDbCommand 对象
SqlUse.Update(Dt.Tables(0)) '执行OleDbDataAdapter的Update更新数据库
Catch
MsgBox(Err.Description, 32)
Finally
MsgBox("更新成功", 64)
End Try
End Sub
End Class
Imports System.Data.OleDb
Public Class Form1
Dim SqlCOM As OleDbConnection
Dim SqlUse As OleDbDataAdapter
Dim Dt As DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
'连接数据库
SqlCOM = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\LP\Documents\Database1.accdb")
'执行数据命令获取表
SqlUse = New OleDbDataAdapter("SELECT * FROM 表1", SqlCOM)
Dt = New DataSet
SqlUse.Fill(Dt) '放入dataset中
Catch
MsgBox(Err.Description, 48)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
DataGridView1.DataSource = Dt.Tables(0).DefaultView 'DVG与DADASET同步数据显示
Catch
MsgBox(Err.Description, 32)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Dim ass As New OleDbCommandBuilder(SqlUse) '关联DataSet和数据库的操作(更新)
SqlUse.UpdateCommand = ass.GetUpdateCommand() '执行更新所需的 OleDbCommand 对象
SqlUse.Update(Dt.Tables(0)) '执行OleDbDataAdapter的Update更新数据库
Catch
MsgBox(Err.Description, 32)
Finally
MsgBox("更新成功", 64)
End Try
End Sub
End Class
2011-05-05
展开全部
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\db.mdb"
Dim con As New OleDb.OleDbConnection(cstring)
Dim sql As String = "select max(字段名) from 表名"
con.Open()
Dim cmd As New OleDb.OleDbCommand(sql, con)
Dim myreader As OleDbDataReader = cmd.ExecuteReader
myreader.Read
TextBox1.Text = myreader(0)
con.Close()
End Sub
另外,虚机团上产品团购,超级便宜
Dim cstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\db.mdb"
Dim con As New OleDb.OleDbConnection(cstring)
Dim sql As String = "select max(字段名) from 表名"
con.Open()
Dim cmd As New OleDb.OleDbCommand(sql, con)
Dim myreader As OleDbDataReader = cmd.ExecuteReader
myreader.Read
TextBox1.Text = myreader(0)
con.Close()
End Sub
另外,虚机团上产品团购,超级便宜
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询