vb.net 中datagridview内数据修改后保存代码怎么写
如题,我datagirdview能显示sql中表的数据,但是数据改过后保存代码不会写请高手帮忙,最好有代码下面我的代码也是复制别人的,反正不能用还有最好是只保存修改的数据...
如题,我datagirdview能显示sql中表的数据,但是数据改过后保存代码不会写
请高手帮忙 ,最好有代码
下面我的代码也是复制别人的,反正不能用
还有最好是只保存修改的数据,不是全部保存的那种
要求:修改数据后,点击命令按键保存修改过的数据,要求代码的
能用的话还可以加分
Dim conn As New System.Data.SqlClient.SqlConnection '创建一个新连接
conn.ConnectionString = "Data Source=FO525FSWNFBJVA8\SQLEXPRESS;Initial Catalog=MyDatabase;Persist Security Info=True;User ID=sa;Password=1234"
conn.Open()
Dim AAADataSet1 As New DataSet
Dim shujubiaoadapter As New System.Data.SqlClient.SqlDataAdapter
Try
If AAADataSet1.HasChanges Then
shujubiaoadapter.Update(AAADataSet1.GetChanges)
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(0).Cells(0)
MessageBox.Show("修改成功!")
Else
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(0).Cells(0)
MessageBox.Show("无修改的内容!")
End If
Catch ex As Exception
MessageBox.Show("修改出错!")
End Try
conn.Close()
没有其他人回答,分给你了
我自己弄出来了 展开
请高手帮忙 ,最好有代码
下面我的代码也是复制别人的,反正不能用
还有最好是只保存修改的数据,不是全部保存的那种
要求:修改数据后,点击命令按键保存修改过的数据,要求代码的
能用的话还可以加分
Dim conn As New System.Data.SqlClient.SqlConnection '创建一个新连接
conn.ConnectionString = "Data Source=FO525FSWNFBJVA8\SQLEXPRESS;Initial Catalog=MyDatabase;Persist Security Info=True;User ID=sa;Password=1234"
conn.Open()
Dim AAADataSet1 As New DataSet
Dim shujubiaoadapter As New System.Data.SqlClient.SqlDataAdapter
Try
If AAADataSet1.HasChanges Then
shujubiaoadapter.Update(AAADataSet1.GetChanges)
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(0).Cells(0)
MessageBox.Show("修改成功!")
Else
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(0).Cells(0)
MessageBox.Show("无修改的内容!")
End If
Catch ex As Exception
MessageBox.Show("修改出错!")
End Try
conn.Close()
没有其他人回答,分给你了
我自己弄出来了 展开
3个回答
展开全部
'--------下面是整个类代码-----------------------------------------------------------
Imports System.Data.OleDb
Public Class Form1
Dim ds As DataSet
Dim tb As DataTable
Dim ap As OleDbDataAdapter
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '手动显示
Me.DataGridView1.AllowUserToAddRows = False
tb = New DataTable
ap = New OleDbDataAdapter("select * from electic ", cn)'这里是SQL命令,Cn是数据库连接,修改成你的棚陪Conn
ap.Fill(tb)
Me.BindingSource1.DataSource = tb
Me.BindingNavigator1.BindingSource = Me.BindingSource1
Me.DataGridView1.DataSource = Me.BindingSource1
End Sub
Private Sub ToolStrip_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStrip_Save.Click '调用保存
Save_Data()
End Sub
Private Function Save_Data() As Boolean '保存数据库扒让的操春和局作
Try
Me.DataGridView1.EndEdit()
Me.BindingSource1.EndEdit()
Dim bd As New OleDbCommandBuilder(ap)
ap.UpdateCommand = bd.GetUpdateCommand
ap.Update(Me.BindingSource1.DataSource)
Return True
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
Private Sub ToolStrip_Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStrip_Cancel.Click '撤销操作
Button1_Click(sender, e)
End Sub
Imports System.Data.OleDb
Public Class Form1
Dim ds As DataSet
Dim tb As DataTable
Dim ap As OleDbDataAdapter
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '手动显示
Me.DataGridView1.AllowUserToAddRows = False
tb = New DataTable
ap = New OleDbDataAdapter("select * from electic ", cn)'这里是SQL命令,Cn是数据库连接,修改成你的棚陪Conn
ap.Fill(tb)
Me.BindingSource1.DataSource = tb
Me.BindingNavigator1.BindingSource = Me.BindingSource1
Me.DataGridView1.DataSource = Me.BindingSource1
End Sub
Private Sub ToolStrip_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStrip_Save.Click '调用保存
Save_Data()
End Sub
Private Function Save_Data() As Boolean '保存数据库扒让的操春和局作
Try
Me.DataGridView1.EndEdit()
Me.BindingSource1.EndEdit()
Dim bd As New OleDbCommandBuilder(ap)
ap.UpdateCommand = bd.GetUpdateCommand
ap.Update(Me.BindingSource1.DataSource)
Return True
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
Private Sub ToolStrip_Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStrip_Cancel.Click '撤销操作
Button1_Click(sender, e)
End Sub
展开全部
以下是我现成的操作OLEDB数据库的,也就是基裂Access2003那种,你自己修改成SQL的就可以用
首先在脊敬窗体上加DataGridView1,BindingSource1,BindingNavigator1,Button1控件各一个,然后在BindingNavigator1控搏野闭件上增加一个按钮ToolStrip_Save
修改的关键是OleDbCommandBuilder(你自己把其改成SqlClient.SqlCommandBuilder)
'--------下面是整个类代码-----------------------------------------------------------
Imports System.Data.OleDb
Public Class Form1
Dim ds As DataSet
Dim tb As DataTable
Dim ap As OleDbDataAdapter
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '手动显示
Me.DataGridView1.AllowUserToAddRows = False
tb = New DataTable
ap = New OleDbDataAdapter("select * from electic ", cn)'这里是SQL命令,Cn是数据库连接,修改成你的Conn
ap.Fill(tb)
Me.BindingSource1.DataSource = tb
Me.BindingNavigator1.BindingSource = Me.BindingSource1
Me.DataGridView1.DataSource = Me.BindingSource1
End Sub
Private Sub ToolStrip_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStrip_Save.Click '调用保存
Save_Data()
End Sub
Private Function Save_Data() As Boolean '保存数据库的操作
Try
Me.DataGridView1.EndEdit()
Me.BindingSource1.EndEdit()
Dim bd As New OleDbCommandBuilder(ap)
ap.UpdateCommand = bd.GetUpdateCommand
ap.Update(Me.BindingSource1.DataSource)
Return True
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
Private Sub ToolStrip_Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStrip_Cancel.Click '撤销操作
Button1_Click(sender, e)
End Sub
首先在脊敬窗体上加DataGridView1,BindingSource1,BindingNavigator1,Button1控件各一个,然后在BindingNavigator1控搏野闭件上增加一个按钮ToolStrip_Save
修改的关键是OleDbCommandBuilder(你自己把其改成SqlClient.SqlCommandBuilder)
'--------下面是整个类代码-----------------------------------------------------------
Imports System.Data.OleDb
Public Class Form1
Dim ds As DataSet
Dim tb As DataTable
Dim ap As OleDbDataAdapter
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '手动显示
Me.DataGridView1.AllowUserToAddRows = False
tb = New DataTable
ap = New OleDbDataAdapter("select * from electic ", cn)'这里是SQL命令,Cn是数据库连接,修改成你的Conn
ap.Fill(tb)
Me.BindingSource1.DataSource = tb
Me.BindingNavigator1.BindingSource = Me.BindingSource1
Me.DataGridView1.DataSource = Me.BindingSource1
End Sub
Private Sub ToolStrip_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStrip_Save.Click '调用保存
Save_Data()
End Sub
Private Function Save_Data() As Boolean '保存数据库的操作
Try
Me.DataGridView1.EndEdit()
Me.BindingSource1.EndEdit()
Dim bd As New OleDbCommandBuilder(ap)
ap.UpdateCommand = bd.GetUpdateCommand
ap.Update(Me.BindingSource1.DataSource)
Return True
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
Private Sub ToolStrip_Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStrip_Cancel.Click '撤销操作
Button1_Click(sender, e)
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你用的哪种方法
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询