DataGridView 上增删改同步Access数据库

准备工作:

用Visual Studio新建一个WinForm项目,在Form1中放上一个datagridview控件,两个按钮控件.

在项目所在文件夹目录bin/debug/ 下新建一个Access数据库,名字Database. 这里要注意,数据库后缀名最好是.mdb (access 2002-2003数据库). 如果用.accdb(access2007)格式,就需要下载并安装Access 2007 runtime, 因为07版使用的Provider Engine与原来不同,而且Office2007中没有自带这个.在Database中新建一张表,名字tblMat,加入主键字段"ID",类型自动递增,其他字段随意.

编写代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 using System.Data.OleDb;
11 
12 namespace DataGridViewTest
13 {
14     public partial class Form1 : Form
15     {
16         //连接字符串,用来连接Database数据库;
17         //如果没有密码请去掉JET OLEDB:Database Password=***;
18         public static string connString = @"
19                 Provider=Microsoft.Jet.OLEDB.4.0;
20                 Data Source=Database.mdb;
21                 JET OLEDB:Database Password=***;
22         ";
23         //SQL查询语句,用来从Database数据库tblMat表中获取所有数据;
24         private string sqlString = "SELECT * from tblMat";
25         //dataadapter,使数据库的表和内存中的表datatable通讯
26         private OleDbDataAdapter da;
27         //bindingsource,使内存中的表datatable与窗体的显示控件datagridview通讯
28         private BindingSource bs;
29         public Form1()
30         {
31             InitializeComponent();
32             //新建连接
33             OleDbConnection conn = new OleDbConnection(connString);
34             //新建dataadapter
35             da = new OleDbDataAdapter(sqlString, conn);
36             //新建datatable
37             DataTable dt = new DataTable();
38             //如果数据适配器填充内存表时,没有设置主键列,而access数据表有,那么设置主键;
39             //如果access数据表的主键是自动递增,那么设置内存表的主键也是自动递增.
40             da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
41             //填充内存表
42             da.Fill(dt);
43             //如果设置了MissingSchemaAction,下面语句可以省略;
44             //dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
45             //dt.Columns[0].AutoIncrement = true;
46             //dt.Columns[0].AutoIncrementSeed = 1;
47             //dt.Columns[0].AutoIncrementStep = 1;
48 
49             //新建bindingsource
50             bs = new BindingSource();
51             //bindingsource绑定内存表
52             bs.DataSource = dt;
53             //datagridview绑定bindingsource
54             dataGridView1.DataSource = bs;
55         }
56 
57         //更新数据库,包括增删改,如果需要实时更新,并且希望客户端和数据库通讯不要太频繁,
58         //把下面代码内容设置在datagridview的RowLeave事件中
59         private void button1_Click(object sender, EventArgs e)
60         {
61             //通过数据适配器的select command text语句自动生成其他SQL语句
62             //包括 Update, Insert, Delete
63             OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
64             //以下语句经测试不需要
65             //da.UpdateCommand = cb.GetUpdateCommand();
66             //da.InsertCommand = cb.GetInsertCommand();
67             //da.DeleteCommand = cb.GetDeleteCommand();
68             //dataGridView1.EndEdit();
69             //conn.open();
70 
71             //用dataadapter的update方法自动更新access数据库
72             da.Update((DataTable)bs.DataSource);
73             //conn.close();
74         }
75 
76         //定位到datagridview的新增行第二个单元格,
77         //第一个单元格式"ID",自动递增.
78         private void button2_Click(object sender, EventArgs e)
79         {
80             dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.NewRowIndex].Cells[1];
81         }
82     }
83 }

 

posted @ 2014-12-21 17:02  prosys  阅读(1054)  评论(0编辑  收藏  举报