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 | 文本 | 邮政编码 |
//创建一个 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数据库,服务器名称为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" ] ; |
protected void GoPrevious ( object sender , System.EventArgs e ) { if ( myBind.Position == 0 ) MessageBox.Show ( "已经到了第一条记录!" , "信息提示!" , MessageBoxButtons.OK , MessageBoxIcon.Information ) ; else myBind.Position -= 1 ; } |
protected void GoNext ( object sender , System.EventArgs e ) { if ( myBind.Position == myBind.Count -1 ) MessageBox.Show ( "已经到了最后一条记录!", "信息提示!" , MessageBoxButtons.OK , MessageBoxIcon.Information ) ; else myBind.Position += 1 ; } |
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 ; } |
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 ( ) , "错误!" ) ; } } } |
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 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 ( ) , "错误!" ) ; } } |
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 ; } } |
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数据库为模板编程的完成源程序代码了。所以本文就不再提供了。 七.总结: 数据库编程始终是程序编程内容中的一个重点和难点。而以上介绍的这些操作又是数据库编程中最为基本,也是最为重要的内容。那些复杂的编程无非是以上这些处理的若干个叠加。