Visual C#数据库编程

本文详细介绍使用Visual C#进行数据库编程的基本方法,包括连接数据库、数据浏览、记录的增删改等核心操作。通过实例演示了如何针对Access和SQL Server数据库进行有效管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
祥解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象是使用FormBindingContext来得到。下列代是以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://
Datasetbooks数据表
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://
Datasetperson数据表
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://
Datasetbooks数据表
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://
Datasetbooks数据表
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数据库为模板程的完成源程序代了。所以本文就不再提供了。

  七.总结

  数据库编程始是程序程内容中的一个重点和点。而以上介些操作又是数据库编程中最基本,也是最重要的内容。那些复杂程无非是以上理的若干个叠加。
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值