Visual C#数据库编程

 
祥解 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 数据 库为 模板 程的完成源程序代 了。所以本文就不再提供了。

  七. 总结

  数据 库编 程始 是程序 程内容中的一个重点和 点。而以上介 些操作又是数据 库编 程中最 基本,也是最 重要的内容。那些 复杂 程无非是以上 理的若干个叠加。
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值