想把 DataGridView 中所做的更改保存到ACCESS数据库中出现的问题(vb.net 2005).

界面上面有三个控件:Datagridview1,和两个按钮,代码如下:ImportsSystem.Data.OleDbPublicClassForm1PublicLeaf... 界面上面有三个控件:Datagridview1,和两个按钮
,代码如下:
Imports System.Data.OleDb
Public Class Form1
Public LeafFileName As String = CurDir() & "\Access.mdb" '这里是数据库文件路径和名称,你自己改
Public LeafFormName As String = "projects" '这里是你要读取这个数据库内的表的名称
Public LeafPassWord As String = "leafsoftpassword" '这里是数据库密码,如果没有密码就随便
Public LeafData As DataTable
Public LeafNewRow As DataRow
Dim LeafOleDbConnection As OleDbConnection
Dim LeafOleDbCommand As OleDbCommand
Dim LeafOleDbDataAdapter As OleDbDataAdapter
Dim LeafCommandBuilder As OleDbCommandBuilder

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1.Text = "读取"
Button2.Text = "保存"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
LeafOleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & LeafFileName & ";" & "Jet OLEDB:Database Password='" & LeafPassWord & "';")
LeafOleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM " & LeafFormName, LeafOleDbConnection)
LeafData = New DataTable
LeafOleDbDataAdapter.Fill(LeafData)
LeafNewRow = LeafData.NewRow
DataGridView1.DataSource = LeafData
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

\\\\??怎么写啊??\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
End Sub
End Class

单击按钮1可以将Access的数据显示在Datagridview中,,想在修改过其中的数据后再点击按钮2就可以将Datagridview的数据写入Access,,,,,

代码应该怎么写??
展开
 我来答
tao1203
2008-11-22
知道答主
回答量:7
采纳率:0%
帮助的人:9.6万
展开全部
mysql = new SqlDataAdapter("Select * from xs", conn);
mysql.Fill(myTable);
dataGrid1.DataSource = myTable;
cmOrders = (CurrencyManager)BindingContext[myTable];
cb = new SqlCommandBuilder(mysql);//有这条才能使用更新命令.表中必须有主键..

更新的代码
cmOrders.EndCurrentEdit();
if (myTable.GetChanges() != null)
{
try
{
mysql.Update(myTable);
MessageBox.Show("数据保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception express)
{
MessageBox.Show(express.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
myTable.RejectChanges();
}
}
注:声明~~~~`````
private static DataTable myTable;
private CurrencyManager cmOrders;
SqlDataAdapter mysql;
private SqlCommandBuilder cb;
charmlh
2008-11-21 · TA获得超过325个赞
知道答主
回答量:29
采纳率:0%
帮助的人:0
展开全部
有一个比较笨的办法:
dim inStr as string =""
for i as integer =0 to datagridview1.rows.count
'中间拼保存语句 然后保存
instr =instr & " update 表名 set ..,..,..,"

...
next
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式