祥解
Visual C
#数据
库编
程
| |
关
于数据
库编
程,微
软
提供了一个
统
一的数据
对
象
访问
模型,在
Visual Studio6.0
中称
为
ADO
,在
.NET
中
则统
一
为
ADO.NET,
掌握
ADO.NET
就等于掌握了数据
库编
程的核心。
针对 数据 库编 程始 终 是程序 设计语 言的一个重要方面的内容,也是一个 难 点。数据 库编 程的内容十分丰富,但最 为 基本 编 程的也就是那 么 几点,譬如: 连 接数据 库 、得到需要的数据和 针对 数据 记录 的 浏览 、 删 除、修改、插入等操作。其中又以后面 针对 数据 记录 的数据操作 为 重点。本文就来着重探 讨 一下 Visual C #数据 库 基本 编 程,即:如何 浏览记录 、修改 记录 、 删 除 记录 和插入 记录 。 一.程序 设计 和运行的 环 境 设 置: ( 1 ) . 视 窗 2000 服 务 器版 ( 2 ) .Microsoft Data Acess Component 2.6 以上版本 ( MDAC 2.6 ) ( 3 ) ..Net FrameWork SDK Beta 2 为 了更清楚的 说 明 问题 ,在数据 库 的 选 用上,采用了当前比 较 典型的数据 库 ,一个是本地数据 库 Access 2000 ,另外一个是 远 程数据 库 Sql Server 2000 。其中本地数据 库 名称 为 "db.mdb" ,在其中定 义 了一 张 数据表 "person" , "person" 表的数据 结 构如下表: 字段名称 字段 类 型 字段意思 id 数字 序号 xm 文本 姓名 xb 文本 性 别 nl 文本 年 龄 zip 文本 邮 政 编码 远 程数据 库 Sql Server 2000 的数据 库 服 务 器名称 为 "Server1", 数据 库 名称 为 "Data1" ,登 陆 的 ID 为 "sa" ,口令 为 空,在数据 库 也定 义 了一 张 "person" 表,数据 结 构如上表。 二.如何 浏览 数据: 在《 Visual C #的数据 绑 定》中,已 经 了解了如何把数据集中的某些字段 绑 定到 WinForm 组 件的某个属性上, 这样 程序 员 就可以根据以 WinForm 组 件的来定制数据 显 示的形式,并且此 时 的 WinForm 组 件 显 示内容就可以随着 记录 指 针 的 变 化而改 变 。至此可 见 , 浏览 数据 记录 的 关键 就是如何改 变记录 指 针 。要 实现这种 操作,就要使用到 BindingManagerBase 类 ,此 类 的主要作用是管理 对 于那些 实现 了 对 同一个数据源 进 行 绑 定的 对 象。 说 的具体些,就是能 够 使得 Windows 窗体上的已 经对 同一数据源 进 行数据 绑 定的 组 件保持同 步 。在 BindingManagerBase 类 中定 义 了一个属性 "Position" ,通 过这 个属性就可以改 变 BindingManagerBase 对 象中的数据指 针 。 创 建 BindingManagerBase 对 象必 须 要使用到 BindingContext 类 ,其 实每 一个由 Control 类 中 继 承而得到的 对 象,都有 单 一的 BindingContext 对 象,在大多数 创 建窗体中 实现 数据 绑 定 组 件的 BindingManagerBase 对 象是使用 Form 类 的 BindingContext 来得到。下列代 码 是以 Access 2000 数据 库为 模型, 创 建的一个名称 为 "myBind" 的 BindingManagerBase 对 象。 // 创 建一个 OleDbConnection string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; string strCom = " SELECT * FROM person " ; file:// 创 建一个 DataSet myDataSet = new DataSet ( ) ; myConn.Open ( ) ; file:// 用 OleDbDataAdapter 得到一个数据集 OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ; file:// 把 Dataset 绑 定 books 数据表 myCommand.Fill ( myDataSet , "person" ) ; file:// 关闭 此 OleDbConnection myConn.Close ( ) ; myBind = this.BindingContext [ myDataSet , "person" ] ; 下列代 码 是以 Sql Server 2000 数据 库为 模型, 创 建一个名称 为 "myBind" 的 BindingManagerBase 对 象。 // 设 定数据 连 接字符串,此字符串的意思是打 开 Sql server 数据 库 ,服 务 器名称 为 server1, 数据 库为 data1 string strCon = "Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID = sa ; Initial Catalog = data1 ; Data Source = server1 " ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; myConn.Open ( ) ; string strCom = " SELECT * FROM person " ; file:// 创 建一个 DataSet myDataSet = new DataSet ( ) ; file:// 用 OleDbDataAdapter 得到一个数据集 OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ; file:// 把 Dataset 绑 定 person 数据表 myCommand.Fill ( myDataSet , " person " ) ; file:// 关闭 此 OleDbConnection myConn.Close ( ) ; myBind = this.BindingContext [ myDataSet , "person" ] ; 得到了是同一数据源的 BindingManagerBase 对 象,通 过 改 变 此 对 象的 "Position" 属性 值 , 这样绑 定数据的 组 件 显 示的数据就随 之 变 化,从而 实现导 航数据 记录 。 < I > . 导 航按 钮 " 上一条 " 实现 方法: protected void GoPrevious ( object sender , System.EventArgs e ) { if ( myBind.Position == 0 ) MessageBox.Show ( " 已 经 到了第一条 记录 ! " , " 信息提示! " , MessageBoxButtons.OK , MessageBoxIcon.Information ) ; else myBind.Position -= 1 ; } < II > . 导 航按 钮 " 下一条 " 实现 方法: protected void GoNext ( object sender , System.EventArgs e ) { if ( myBind.Position == myBind.Count -1 ) MessageBox.Show ( " 已 经 到了最后一条 记录 ! ", " 信息提示! " , MessageBoxButtons.OK , MessageBoxIcon.Information ) ; else myBind.Position += 1 ; } < III > . 导 航按 钮 " 至尾 " 实现 方法: protected void GoLast ( object sender , System.EventArgs e ) { myBind.Position = myBind.Count - 1 ; } < IV > . 导 航按 钮 " 至首 " 实现 方法: protected void GoFirst ( object sender , System.EventArgs e ) { myBind.Position = 0 ; } 注 释 : "Count" 是 BindingManagerBase 对 象的另外一个重要的属性,是数据集 记录 的 总 数。 三. 实现删 除 记录 : 在 对 数据 记录进 行操作的 时 候,有二点必 须 十分清晰: 其一:在 对 数据 记录进 行操作的 时 候,我想有一些程序 员 一定有 这样 一个疑惑,当 对 数据 库 服 务 器 请 求数据集的 时 候,就会 产 生 "DataSet" 对 象,用以管理数据集, 这样 如果 这 些 对 数据 库 服 务 器的 请 求非常多,同 样 也就会 产 生很多的 "DataSet" 对 象,达到一定 时 候必然会使得数据 库 服 务 器崩 溃 。 这种 想法是自然的,但和 实际 并不相符,因 为 "DataSet" 对 象并不是在服 务 器端 产 生的,而是在客 户 端 产 生的。所以面 对 众多的数据 请 求的 时 候 对 数据 库 服 务 器的影响并不十分太大。 其二: 记 得在用 Delphi 编 写三 层 数据模型的 时 候的, 每 一次 对 数据 库 的修改其 实 只是 对 第二 层产 生的数据集的修改,要真正修改数据 库 , 还 必 须调 用一个另外的方法。在用 ADO.NET 处 理数据 库 的 时 候, 虽 然 处 理的直接 对 象是数据 库 ,但此 时 "DataSet" 对 象中的内容并没有随之改 变 ,而 绑 定的数据 组 件 显 示的数据又来源于 "DataSet" 对 象, 这样 就会 产 生一个 错觉 ,就是修改了的 记录 并没有修改掉, 删 除的 记录 并没有 删 除掉。所以 对 数据 记录进 行操作的 时 候,在修改数据 库 后, 还 要 对 "DataSet" 对 象 进 行必要的修改, 这样 才能保 证 "DataSet" 对 象和数据 库 内容一致、同 步 。下面代 码 是 删 除当前 绑 定 组 件 显 示的 记录 的程序代 码 ,此代 码 是以 Access 2000 数据 库为 模板的: protected void Delete_record ( object sender , System.EventArgs e ) { DialogResult r = MessageBox.Show ( " 是否 删 除当前 记录 ! " , " 删 除当前 记录 ! " , MessageBoxButtons.YesNo , MessageBoxIcon.Question ) ; int ss = ( int ) r ; if ( ss == 6 ) // 按 动 " 确定 " 按 钮 { try{ file:// 连 接到一个数据 库 string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb " ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; myConn.Open ( ) ; string strDele = "DELETE FROM person WHERE id= " + t_id.Text ; OleDbCommand myCommand = new OleDbCommand ( strDele , myConn ) ; file:// 从数据 库 中 删 除指定 记录 myCommand.ExecuteNonQuery ( ) ; file:// 从 DataSet 中 删 除指定 记录 myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . Delete ( ) ; myDataSet.Tables [ "person" ] . AcceptChanges ( ) ; myConn.Close ( ) ; } catch ( Exception ed ) { MessageBox.Show ( " 删 除 记录错误 信息: " + ed.ToString ( ) , " 错误 ! " ) ; } } } 四.插入数据 记录 : 对 数据 库进 行插入 记录 操作和 删 除 记录 操作基本的思路是一致的,就是通 过 ADO.NET 首先插入数据 记录 到数据 库 ,然后 对 "DataSet" 对 象 进 行必要的修改。下列代 码 就是以 Access 2000 数据 库为 模型修改当前 记录 的代 码 : protected void Update_record ( object sender , System.EventArgs e ) { int i = myBind.Position ; try{ file:// 连 接到一个数据 库 string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb " ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; myConn.Open ( ) ; myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . BeginEdit ( ) ; file:// 从数据 库 中修改指定 记录 string strUpdt = " UPDATE person SET xm = ''" + t_xm.Text + "'' , xb = ''" + t_xb.Text + "'' , nl = " + t_nl.Text + " , zip = " + t_books.Text + " WHERE id = " + t_id.Text ; OleDbCommand myCommand = new OleDbCommand ( strUpdt , myConn ) ; myCommand.ExecuteNonQuery ( ) ; myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . EndEdit ( ) ; myDataSet.Tables [ "person" ] . AcceptChanges ( ) ; myConn.Close ( ) ; } catch ( Exception ed ) { MessageBox.Show ( " 修改指定 记录错误 : " + ed.ToString ( ) , " 错误 ! " ) ; } myBind.Position = i ; } 由于 对 Sql Server 2000 数据 记录 修改操作和 Access 2000 数据 记录 修改操作的差异只在于不同的数据 链 接,具体的代 码 可以参考 " 删 除数据 记录 " 中的代 码 ,在 这 里就不提供了。 五.插入数据 记录 : 和前面二 种 操作在思路是一致的,就是通 过 ADO.NET 首先插入数据 记录 到数据 库 ,然后 对 "DataSet" 对 象 进 行必要的修改。下列代 码 就是以 Access 2000 数据 库为 模型插入一条数据 记录 的代 码 protected void Insert_record ( object sender , System.EventArgs e ) { try { file:// 判断所有字段是否添完,添完 则执 行,反之 弹 出提示 if ( t_id.Text != "" && t_xm.Text != "" && t_xb.Text != "" && t_nl.Text != "" && t_books.Text != "" ) { string myConn1 = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ; OleDbConnection myConn = new OleDbConnection ( myConn1 ) ; myConn.Open ( ) ; string strInsert = " INSERT INTO person ( id , xm , xb , nl , zip ) VALUES ( " ; strInsert += t_id.Text + ", ''" ; strInsert += t_xm.Text + "'', ''" ; strInsert += t_xb.Text + "'', " ; strInsert += t_nl.Text + ", " ; strInsert += t_books.Text + ")" ; OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ; inst.ExecuteNonQuery ( ) ; myConn.Close ( ) ; myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . BeginEdit ( ) ; myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . EndEdit ( ) ; myDataSet.Tables [ "person" ] . AcceptChanges ( ) ; } else { MessageBox.Show ( " 必 须 填 满 所有字段 值 ! " , " 错误 ! " ) ; } } catch ( Exception ed ) { MessageBox.Show ( " 保存数据 记录发 生 " + ed.ToString ( ) , " 错误 ! " ) ; } } 同 样对 Sql Server 2000 数据 库进 行插入 记录 操作和 Access 2000 数据 库 插入 记录 操作的差异也只在于不同的数据 链 接,具体的代 码 可以参考 " 删 除数据 记录 " 中的代 码 ,在 这 里就不提供了。 六. Visual C #数据 库编 程的完成源代 码 和程序运行的主界面: 掌握了上面要点, 编 写一个完整的数据 库编 程的程序就 显 得非常容易了,下面是 Visual C # 进 行数据 库编 程的完整代 码 ( Data01.cs ),此代 码 是以 Access 2000 数据 库为 模型 设计 的,具体如下: using System ; using System.Drawing ; using System.ComponentModel ; using System.Windows.Forms ; using System.Data.OleDb ; using System.Data ; public class Data : Form { private System.ComponentModel.Container components = null ; private Button lastrec ; private Button nextrec ; private Button previousrec ; private Button firstrec ; private TextBox t_books ; private TextBox t_nl ; private ComboBox t_xb ; private TextBox t_xm ; private TextBox t_id ; private Label l_books ; private Label l_nl ; private Label l_xb ; private Label l_xm ; private Label l_id ; private Label label1 ; private DataSet myDataSet ; private Button button1 ; private Button button2 ; private Button button3 ; private Button button4 ; private BindingManagerBase myBind ; public Data ( ) { file:// 连 接到一个数据 库 GetConnected ( ) ; // 对 窗体中所需要的内容 进 行初始化 InitializeComponent ( ) ; } file:// 清除在程序中使用 过 的 资 源 protected override void Dispose( bool disposing ) { if( disposing ) { if ( components != null ) { components.Dispose ( ) ; } } base.Dispose( disposing ) ; } public static void Main ( ) { Application.Run ( new Data ( ) ) ; } public void GetConnected ( ) { try { file:// 创 建一个 OleDbConnection string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; string strCom = " SELECT * FROM person " ; file:// 创 建一个 DataSet myDataSet = new DataSet ( ) ; myConn.Open ( ) ; file:// 用 OleDbDataAdapter 得到一个数据集 OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ; file:// 把 Dataset 绑 定 books 数据表 myCommand.Fill ( myDataSet , "person" ) ; file:// 关闭 此 OleDbConnection myConn.Close ( ) ; } catch ( Exception e ) { MessageBox.Show ( " 连 接 错误 ! " + e.ToString ( ) , " 错误 " ) ; } } private void InitializeComponent ( ) { file:// 添加控件,略 this.Name = "Data" ; this.Text = "Visual C #的数据 库编 程! " ; this.ResumeLayout(false) ; myBind = this.BindingContext [ myDataSet , "person" ] ; } protected void New_record ( object sender , System.EventArgs e ) { t_id.Text = ( myBind.Count + 1 ).ToString ( ) ; t_xm.Text = "" ; t_xb.Text = "" ; t_nl.Text = "" ; t_books.Text = "" ; } protected void Insert_record ( object sender , System.EventArgs e ) { try { file:// 判断所有字段是否添完,添完 则执 行,反之 弹 出提示 if ( t_id.Text != "" && t_xm.Text != "" && t_xb.Text != "" && t_nl.Text != "" && t_books.Text != "" ) { string myConn1 = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ; OleDbConnection myConn = new OleDbConnection ( myConn1 ) ; myConn.Open ( ) ; string strInsert = " INSERT INTO person ( id , xm , xb , nl , zip ) VALUES ( " ; strInsert += t_id.Text + ", ''" ; strInsert += t_xm.Text + "'', ''" ; strInsert += t_xb.Text + "'', " ; strInsert += t_nl.Text + ", " ; strInsert += t_books.Text + ")" ; OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ; inst.ExecuteNonQuery ( ) ; myConn.Close ( ) ; myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . BeginEdit ( ) ; myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . EndEdit ( ) ; myDataSet.Tables [ "person" ] . AcceptChanges ( ) ; } else { MessageBox.Show ( " 必 须 填 满 所有字段 值 ! " , " 错误 ! " ) ; } } catch ( Exception ed ) { MessageBox.Show ( " 保存数据 记录发 生 " + ed.ToString ( ) , " 错误 ! " ) ; } } protected void Update_record ( object sender , System.EventArgs e ) { int i = myBind.Position ; try{ file:// 连 接到一个数据 库 string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb " ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; myConn.Open ( ) ; myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . BeginEdit ( ) ; file:// 从数据 库 中修改指定 记录 string strUpdt = " UPDATE person SET xm = ''" + t_xm.Text + "'' , xb = ''" + t_xb.Text + "'' , nl = " + t_nl.Text + " , zip = " + t_books.Text + " WHERE id = " + t_id.Text ; OleDbCommand myCommand = new OleDbCommand ( strUpdt , myConn ) ; myCommand.ExecuteNonQuery ( ) ; myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . EndEdit ( ) ; myDataSet.Tables [ "person" ] . AcceptChanges ( ) ; myConn.Close ( ) ; } catch ( Exception ed ) { MessageBox.Show ( " 修改指定 记录错误 : " + ed.ToString ( ) , " 错误 ! " ) ; } myBind.Position = i ; } protected void Delete_record ( object sender , System.EventArgs e ) { DialogResult r = MessageBox.Show ( " 是否 删 除当前 记录 ! " , " 删 除当前 记录 ! " , MessageBoxButtons.YesNo , MessageBoxIcon.Question ) ; int ss = ( int ) r ; if ( ss == 6 ) // 按 动 " 确定 " 按 钮 { try{ file:// 连 接到一个数据 库 string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb " ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; myConn.Open ( ) ; string strDele = "DELETE FROM person WHERE id= " + t_id.Text ; OleDbCommand myCommand = new OleDbCommand ( strDele , myConn ) ; file:// 从数据 库 中 删 除指定 记录 myCommand.ExecuteNonQuery ( ) ; file:// 从 DataSet 中 删 除指定 记录 myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . Delete ( ) ; myDataSet.Tables [ "person" ] . AcceptChanges ( ) ; myConn.Close ( ) ; } catch ( Exception ed ) { MessageBox.Show ( " 删 除 记录错误 信息: " + ed.ToString ( ) , " 错误 ! " ) ; } } } file:// 按 钮 " 尾 记录 " 对 象事件程序 protected void GoLast ( object sender , System.EventArgs e ) { myBind.Position = myBind.Count - 1 ; } file:// 按 钮 " 下一条 " 对 象事件程序 protected void GoNext ( object sender , System.EventArgs e ) { if ( myBind.Position == myBind.Count -1 ) MessageBox.Show ( " 已 经 到了最后一条 记录 ! ", " 信息提示! " , MessageBoxButtons.OK , MessageBoxIcon.Information ) ; else myBind.Position += 1 ; } file:// 按 钮 " 上一条 " 对 象事件程序 protected void GoPrevious ( object sender , System.EventArgs e ) { if ( myBind.Position == 0 ) MessageBox.Show ( " 已 经 到了第一条 记录 ! " , " 信息提示! " , MessageBoxButtons.OK , MessageBoxIcon.Information ) ; else myBind.Position -= 1 ; } file:// 按 钮 " 首 记录 " 对 象事件程序 protected void GoFirst ( object sender , System.EventArgs e ) { myBind.Position = 0 ; } } 对 于以 Sql Server 2000 数据 库为 模型的程序代 码 ,只要把 Data01.cs 中的数据 链 接,即: string myConn1 = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ; 改 换 成: string strCon = "Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID = sa ; Initial Catalog = data1 ; Data Source = server1 " ; 注 释 :此数据 链 接代表的意思是:打 开 Sql server 数据 库 ,服 务 器名称 为 server1, 数据 库为 data1 就可以得到 Visual C # 针对 Sql Server 2000 数据 库为 模板 编 程的完成源程序代 码 了。 所以本文就不再提供了。 七. 总结 : 数据 库编 程始 终 是程序 编 程内容中的一个重点和 难 点。而以上介 绍 的 这 些操作又是数据 库编 程中最 为 基本,也是最 为 重要的内容。 那些 复杂 的 编 程无非是以上 这 些 处 理的若干个叠加。 |
using System ;
using System.Drawing ; using System.ComponentModel ; using System.Windows.Forms ; using System.Data.OleDb ; using System.Data ; public class Data : Form { private System.ComponentModel.Container components = null ; private Button lastrec ; private Button nextrec ; private Button previousrec ; private Button firstrec ; private TextBox t_books ; private TextBox t_nl ; private ComboBox t_xb ; private TextBox t_xm ; private TextBox t_id ; private Label l_books ; private Label l_nl ; private Label l_xb ; private Label l_xm ; private Label l_id ; private Label label1 ; private DataSet myDataSet ; private Button button1 ; private Button button2 ; private Button button3 ; private Button button4 ; private BindingManagerBase myBind ; public Data ( ) { file:// 连 接到一个数据 库 GetConnected ( ) ; // 对 窗体中所需要的内容 进 行初始化 InitializeComponent ( ) ; } file:// 清除在程序中使用 过 的 资 源 protected override void Dispose( bool disposing ) { if( disposing ) { if ( components != null ) { components.Dispose ( ) ; } } base.Dispose( disposing ) ; } public static void Main ( ) { Application.Run ( new Data ( ) ) ; } public void GetConnected ( ) { try { file:// 创 建一个 OleDbConnection string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; string strCom = " SELECT * FROM person " ; file:// 创 建一个 DataSet myDataSet = new DataSet ( ) ; myConn.Open ( ) ; file:// 用 OleDbDataAdapter 得到一个数据集 OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ; file:// 把 Dataset 绑 定 books 数据表 myCommand.Fill ( myDataSet , "person" ) ; file:// 关闭 此 OleDbConnection myConn.Close ( ) ; } catch ( Exception e ) { MessageBox.Show ( " 连 接 错误 ! " + e.ToString ( ) , " 错误 " ) ; } }
private void InitializeComponent ( )
{ file:// 添加控件,略 this.Name = "Data" ; this.Text = "Visual C #的数据 库编 程! " ; this.ResumeLayout(false) ; myBind = this.BindingContext [ myDataSet , "person" ] ; } protected void New_record ( object sender , System.EventArgs e ) {
t_id.Text = ( myBind.Count + 1 ).ToString ( ) ;
t_xm.Text = "" ; t_xb.Text = "" ; t_nl.Text = "" ; t_books.Text = "" ;
}
protected void Insert_record ( object sender , System.EventArgs e ) { try { file:// 判断所有字段是否添完,添完 则执 行,反之 弹 出提示 if ( t_id.Text != "" && t_xm.Text != "" && t_xb.Text != "" && t_nl.Text != "" && t_books.Text != "" ) { string myConn1 = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ; OleDbConnection myConn = new OleDbConnection ( myConn1 ) ; myConn.Open ( ) ; string strInsert = " INSERT INTO person ( id , xm , xb , nl , zip ) VALUES ( " ; strInsert += t_id.Text + ", '" ; strInsert += t_xm.Text + "', '" ; strInsert += t_xb.Text + "', " ; strInsert += t_nl.Text + ", " ; strInsert += t_books.Text + ")" ; OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ; inst.ExecuteNonQuery ( ) ; myConn.Close ( ) ; myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . BeginEdit ( ) ; myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . EndEdit ( ) ; myDataSet.Tables [ "person" ] . AcceptChanges ( ) ; } else { MessageBox.Show ( " 必 须 填 满 所有字段 值 ! " , " 错误 ! " ) ; } } catch ( Exception ed ) { MessageBox.Show ( " 保存数据 记录发 生 " + ed.ToString ( ) , " 错误 ! " ) ; } }
protected void Update_record ( object sender , System.EventArgs e )
{ int i = myBind.Position ; try{ file:// 连 接到一个数据 库 string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb " ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; myConn.Open ( ) ; myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . BeginEdit ( ) ; file:// 从数据 库 中修改指定 记录 string strUpdt = " UPDATE person SET xm = '" + t_xm.Text + "' , xb = '" + t_xb.Text + "' , nl = " + t_nl.Text + " , zip = " + t_books.Text + " WHERE id = " + t_id.Text ; OleDbCommand myCommand = new OleDbCommand ( strUpdt , myConn ) ; myCommand.ExecuteNonQuery ( ) ; myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . EndEdit ( ) ; myDataSet.Tables [ "person" ] . AcceptChanges ( ) ; myConn.Close ( ) ; } catch ( Exception ed ) { MessageBox.Show ( " 修改指定 记录错误 : " + ed.ToString ( ) , " 错误 ! " ) ; } myBind.Position = i ; } protected void Delete_record ( object sender , System.EventArgs e ) { DialogResult r = MessageBox.Show ( " 是否 删 除当前 记录 ! " , " 删 除当前 记录 ! " , MessageBoxButtons.YesNo , MessageBoxIcon.Question ) ; int ss = ( int ) r ; if ( ss == 6 ) // 按 动 " 确定 " 按 钮 { try{ file:// 连 接到一个数据 库 string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb " ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; myConn.Open ( ) ; string strDele = "DELETE FROM person WHERE id= " + t_id.Text ; OleDbCommand myCommand = new OleDbCommand ( strDele , myConn ) ; file:// 从数据 库 中 删 除指定 记录 myCommand.ExecuteNonQuery ( ) ; file:// 从 DataSet 中 删 除指定 记录 myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . Delete ( ) ; myDataSet.Tables [ "person" ] . AcceptChanges ( ) ; myConn.Close ( ) ; } catch ( Exception ed ) { MessageBox.Show ( " 删 除 记录错误 信息: " + ed.ToString ( ) , " 错误 ! " ) ; } } }
file://
按
钮
"
尾
记录
"
对
象事件程序
protected void GoLast ( object sender , System.EventArgs e ) { myBind.Position = myBind.Count - 1 ; } file:// 按 钮 " 下一条 " 对 象事件程序 protected void GoNext ( object sender , System.EventArgs e ) { if ( myBind.Position == myBind.Count -1 ) MessageBox.Show ( " 已 经 到了最后一条 记录 ! ", " 信息提示! " , MessageBoxButtons.OK , MessageBoxIcon.Information ) ; else myBind.Position += 1 ; } file:// 按 钮 " 上一条 " 对 象事件程序 protected void GoPrevious ( object sender , System.EventArgs e ) { if ( myBind.Position == 0 ) MessageBox.Show ( " 已 经 到了第一条 记录 ! " , " 信息提示! " , MessageBoxButtons.OK , MessageBoxIcon.Information ) ; else myBind.Position -= 1 ; } file:// 按 钮 " 首 记录 " 对 象事件程序 protected void GoFirst ( object sender , System.EventArgs e ) { myBind.Position = 0 ; } } |
|
对
于以
Sql Server 2000
数据
库为
模型的程序代
码
,只要把
Data01.cs
中的数据
链
接,即:
string myConn1 = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;
|
改
换
成:
string strCon = "Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID = sa ; Initial Catalog = data1 ; Data Source = server1 " ;
|
注
释
:此数据
链
接代表的意思是:打
开
Sql server
数据
库
,服
务
器名称
为
server1,
数据
库为
data1
就可以得到 Visual C # 针对 Sql Server 2000 数据 库为 模板 编 程的完成源程序代 码 了。所以本文就不再提供了。
七. 总结 :
数据 库编 程始 终 是程序 编 程内容中的一个重点和 难 点。而以上介 绍 的 这 些操作又是数据 库编 程中最 为 基本,也是最 为 重要的内容。那些 复杂 的 编 程无非是以上 这 些 处 理的若干个叠加。
就可以得到 Visual C # 针对 Sql Server 2000 数据 库为 模板 编 程的完成源程序代 码 了。所以本文就不再提供了。
七. 总结 :
数据 库编 程始 终 是程序 编 程内容中的一个重点和 难 点。而以上介 绍 的 这 些操作又是数据 库编 程中最 为 基本,也是最 为 重要的内容。那些 复杂 的 编 程无非是以上 这 些 处 理的若干个叠加。