如题,我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()
没有其他人回答,分给你了
我自己弄出来了
vb.net 中datagridview内数据修改后保存代码怎么写
答案:3 悬赏:30 手机版
解决时间 2021-02-02 06:16
- 提问者网友:了了无期
- 2021-02-01 13:53
最佳答案
- 五星知识达人网友:千杯敬自由
- 2021-02-01 14:26
以下是我现成的操作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
全部回答
- 1楼网友:行路难
- 2021-02-01 17:21
dim accessstring as string = "select * from 示例表"
dim accessconn as new oledb.oledbconnection(accessconnectionstring)
accessconn.open()
dim accessadapter as oledbdataadapter = new oledbdataadapter(accessstring, accessconn)
dim accesscmdbulid as new oledb.oledbcommandbuilder(accessadapter)
try
accessadapter.update(datagridview1.datasource)
catch ex as exception
console.writeline(ex.message)
msgbox("数据保存失败" & vbcrlf & ex.message.tostring, msgboxstyle.critical)
finally
accessadapter.dispose()
accesscmdbulid.dispose()
end try
- 2楼网友:白昼之月
- 2021-02-01 15:55
'--------下面是整个类代码-----------------------------------------------------------
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
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯