C# datagrid Access 简单问题
本程序(书中原码,一字不差)通过datagrid连接Access的示例数据库northwind中的客户表,可以连接,显示,但没法更新,请高手指点。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace CustomerEditor1
{
public partial class Form1 : Form
{
private OleDbConnection _connection;
public Form1()
{
InitializeComponent();
}
private void menuDataConnect_Click(object sender, EventArgs e)
{
Connect();//连接数据库
}
//可以连接上
public void Connect()
{
if (dialogOpenFile.ShowDialog(this) == DialogResult.OK)
{
try
{
string connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User ID=;Password=;", dialogOpenFile.FileName);
OleDbConnection newConnection = new OleDbConnection(connectionString);
newConnection.Open();
Connection = newConnection;
}
catch (Exception ex)
{
HandleException("A connection could not be made.",ex);
}
}
}
public void HandleException(string message, Exception ex)
{
MessageBox.Show(this,string.Format("{0}\n{1}:{2}",message,ex.GetType().ToString(),ex.Message));
}
public OleDbConnection Connection
{
get
{
return _connection;
}
set
{
Disconnect();
_connection = value;
}
}
public void Disconnect()
{
if (_connection != null)
{
if (_connection.State != ConnectionState.Closed)
_connection.Close();
_connection = null;
}
}
private void menuDataLoad_Click(object sender, EventArgs e)
{
LoadData();
}
public void LoadData()//datagrid可以显示northwind中的客户表
{
if (_connection == null)
{
MessageBox.Show(this,"You must connect to a database.");
return;
}
OleDbCommand command = null;
OleDbDataAdapter adapter = null;
try
{
command = _connection.CreateCommand();
command.CommandText = "客户";
command.CommandType = CommandType.TableDirect;
adapter = new OleDbDataAdapter(command);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
datagridCustomers.DataSource = dataset;
}
catch (Exception ex)
{
HandleException("The data could not be loaded.", ex);
}
finally
{
if (adapter != null)
adapter.Dispose();
if (command != null)
command.Dispose();
}
}
private void menuDataSaveChanges_Click(object sender, EventArgs e)
{
SaveChanges();//datagrid中显示的表,我把数据改动一下,然后保存,希望同时更改原表“客户”的数据
}
public void SaveChanges()//希望高手帮我看看这个方法哪儿需要改进的,感觉没有错误
{
if (_connection == null)
{
MessageBox.Show(this, "You must connect to a database.");
return;
}
DataSet dataset = (DataSet)datagridCustomers.DataSource;
if (dataset == null)
{
MessageBox.Show("You must load a DataSet.");
return;
}
OleDbCommand command = null;
OleDbDataAdapter adapter = null;
try
{
command = _connection.CreateCommand();
command.CommandText = "客户";
command.CommandType = CommandType.TableDirect;
adapter = new OleDbDataAdapter(command);
OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
adapter.UpdateCommand = builder.GetUpdateCommand();
adapter.Update(dataset);
MessageBox.Show("Changes have been saved.");
}
finally
{
if (adapter != null)
adapter.Dispose();
if (command != null)
command.Dispose();
}
}
}
} --------------------编程问答-------------------- --------------------编程问答-------------------- 保存的时候加上这句试试看
dataset.AcceptChanges(); --------------------编程问答-------------------- 我写保存入数据库都是自己写Updata语句的,呵呵! --------------------编程问答-------------------- 没用过这种更新方式...
帮不上忙 --------------------编程问答-------------------- dataset.AcceptChanges();不好用啊
补充:.NET技术 , C#