想把 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,,,,,
代码应该怎么写?? 展开
,代码如下:
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,,,,,
代码应该怎么写?? 展开
2个回答
展开全部
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;
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;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询