现在的位置: 首页 > 综合 > 正文

利用C#2005实现数据表的基本操作

2012年11月20日 ⁄ 综合 ⁄ 共 4488字 ⁄ 字号 评论关闭

摘  要   通过运用C#2005中的DataGridView控件、BindingSource和DataAdapter类,实现数据表
的基本操作。
关键词   数据库,C#编程,插入,删除,修改,更新

 
一、前言
    熟悉数据库编程的读者知道,数据表的基本操作就是指对数据表进行记录的插入、删除和修改操作。在C#2003中,笔者通过使用DataGrid控件、CurrencyManager和DataAdapter类,实现了数据表的基本操作,但在C#2005中,将DataGrid更改为DataGridView控件后,发现CurrencyManager对象控制的内存数据表很难与DataGridView显示的数据一致起来。为此,本文在研究C#2005对数据表操作的原理基础上,采用DataGridView控件、BindingSource和DataAdapter类,实现了数据表的基本操作。
二、基本原理
    DataGridView控件提供了一种强大而灵活的以表格形式显示数据的方式,直接将数据显示给用户。BindingSource 类通过提供一个间接寻址层、当前项管理、更改通知和其他服务简化了窗体中控件到数据的绑定,这是通过将BindingSource 组件附加到数据源然后将窗体中的控件绑定到BindingSource 组件来实现的;与数据的所有进一步交互,包括定位、排序、筛选和更新,都通过调用BindingSource组件实现。DataAdapter 用作DataSet和数据源之间的桥接器以便检索和保存数据,直接将内存数据表中记录更新保存到物理数据库中。这三者之间的关系如图1。

图1 基本原理
三、设计与实现
    1.设计数据库
    为了简单起见,本文采用Access数据库,数据库名为db1.mdb,在数据库中建一个db数据表,字段名称和字段类型如图2所示。 

图2 数据表字段名和类型
    注:id字段为主键。
    2.设计界面
    在【解决方案管理器】中,打开默认生成的“Form1”窗体,在窗体放置如图3所示的控件。
 

图3 界面部署图
    Form1窗体的控件属性设置如下表所示。
表1  Form1窗体控件清单
   

控件类型

对象名

属性

取值(说明)

Form

Form1

Text

利用C#2005实现数据表的基本操作

 

 

StartPosition

CenterScreen

DataGridView

dataGridView1

SelectionMode

FullRowSelect(实现整行选取)

其他

 

 

如图3所示

    3.编码实现
    (1)添加使用命名空间
    using System.Data.OleDb;
    (2)声明私有变量
private DataTable temptable = new DataTable();
private BindingSource bindingSource1 = new BindingSource();
private OleDbDataAdapter dataAdapter = new 
OleDbDataAdapter();
    (3)编写数据绑定控件状态的设置方法
private void setBool(bool b)
{
this.comboBox1.Enabled = b;
this.textBox1.Enabled = b;
this.textBox2.Enabled = b;
this.textBox3.Enabled = b;
this.dateTimePicker1.Enabled = b;            
}
    (4)窗体初始化过程中执行的代码
String connectionString = "Provider = 
Microsoft.Jet.OLEDB.4.0;Data source= db1.mdb";
//用户可以更改数据库后缀名(如改为db1.dll),提高数
//据库的安全性
String selectCommand = "select id,姓名,学号,出生时间,性别,德才表现 from db";
dataAdapter = new OleDbDataAdapter(selectCommand, 
connectionString);
OleDbCommandBuilder commandBuilder = new 
OleDbCommandBuilder(dataAdapter); 
dataAdapter.Fill(temptable);
//数据绑定
bindingSource1.DataSource = temptable;
dataGridView1.DataSource = bindingSource1;
dataGridView1.Columns[0].HeaderText = "姓名";
dataGridView1.Columns[0].DataPropertyName = "姓名";
dataGridView1.Columns[0].Width = 80;
dataGridView1.Columns[1].HeaderText = "学号";
dataGridView1.Columns[1].DataPropertyName = "学号";
dataGridView1.Columns[1].Width = 80;
dataGridView1.Columns[2].Visible = false;
dataGridView1.Columns[3].Visible = false;
dataGridView1.Columns[4].Visible = false;
dataGridView1.Columns[5].Visible = false;
textBox1.DataBindings.Add("Text", bindingSource1, "姓名");
textBox2.DataBindings.Add("Text", bindingSource1, "学号");            
textBox3.DataBindings.Add("Text", bindingSource1, "德才表现");            
comboBox1.DataBindings.Add("Text", bindingSource1, 
"性别"); 
dateTimePicker1.DataBindings.Add("Text", bindingSource1, 
"出生时间");
setBool(false);
    (5)记录定位
//首记录
bindingSource1.Position = 0;
//上记录
if (bindingSource1.Position > 0)
  bindingSource1.Position--; 
//下记录
if (bindingSource1.Position < bindingSource1.Count - 1)
  bindingSource1.Position++;
//尾记录     
bindingSource1.Position = bindingSource1.Count - 1;
    (6)插入记录
//新增记录
bindingSource1.AddNew();
setBool(true);  
    (7)删除记录
//删除记录
if (MessageBox.Show("确认删除这条记录吗?", "询问", 
MessageBoxButtons.YesNo) == DialogResult.Yes)

try
{
if (bindingSource1.Count > 0)
{ bindingSource1.RemoveAt(bindingSource1.Position);}
else
{ MessageBox.Show("没有可删除的数据", "提示", 
MessageBoxButtons.OK, MessageBoxIcon.Error);}
}
catch (Exception ex)
{ MessageBox.Show(ex.Message, "错误提示", 
MessageBoxButtons.OK, MessageBoxIcon.Error);}
}
    (8)修改记录
//修改记录
setBool(true);
    (9)取消操作
//取消操作
try
{ bindingSource1.CancelEdit();}
catch (Exception ex)
{ MessageBox.Show(ex.Message, "错误提示!", 
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
    (10)保存更新
//更新数据
bindingSource1.EndEdit();
if (temptable.GetChanges() != null)
{
try
{ this.dataAdapter.Update(temptable);}
catch (Exception ex)
{ MessageBox.Show(ex.Message, "错误提示!", 
MessageBoxButtons.OK, MessageBoxIcon.Information);}
}
setBool(false);
MessageBox.Show("数据更新完毕!", "信息提示!", 
MessageBoxButtons.OK, MessageBoxIcon.Information);
    (11)关闭窗体
//关闭窗体
if (temptable.GetChanges() != null)
{
if (MessageBox.Show("数据有改动,是否保存更新!", "询问
", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
try
{ this.dataAdapter.Update(temptable);}
catch (Exception ex)
{ MessageBox.Show(ex.Message, "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);}
}
}
this.Close();
四、结语
    本文程序采用的是Visual Studio 2005 C# 编写的,所述代码均已在Windows XP Professional和Windows Server 2003 Enterprise Edition+SP1中运行通过。
    通过C#编程实现了对数据表的基本操作。用户在数据字段简单(如不含备注字段)的情况下,可以直接将所有的字段在DataGridView控件中显示出来,通过操作DataGridView实现数据表的基本操作。同时,用户可以通过改变SQL语句,在所选取的字段中包含关键字段的前提下实现对多个表(数据视图)的操作,也可以以此为模型,通过更改SQL语句来实现数据的查询操作。

参考文献
[1] 王晟编著.Visual C#.NET 数据库开发经典案例解析.清华大学出版社,2005
[2] Glenn JohnSon著.ADO.NET 2.0高级编程.清华大学出版社,2006
[3] Microsoft Visual Studio 2005 Documentation

抱歉!评论已关闭.