admin管理员组

文章数量:1531657

2024年7月2日发(作者:)

用Visual C#来增加数据记录

在本篇文章中,我们将介绍Visual C#对数据库的一个基本操作,即:如何往数据库中添加

记录。我们将通过一些数据库操作的例子,来具体说明一下。为了更清楚的说明这个问题,

在选用数据库方面采用了二种当前比较典型的数据库,其一是本地数据库--Access 2000,另

外一个是远程数据库--SQL SERVER 7.0。首先介绍如何用Visual C#来添加Access 2000数据

库的记录。

一.用Visual C#来添加Access 2000数据库的记录

(一).程序设计和运行的环境设置:

(1)视窗2000服务器版

(2)Microsoft Data Acess Component 2.6 以上版本 ( MDAC 2.6 )

(3)本文程序使用的数据库的介绍:

程序中使用的数据库名称为,在此数据库中有一张数据表books。此数据表的结

构如下:

字段名称 字段类型 代表意思

Bookid 数字 序号

booktitle 文本 书籍名称

bookauthor 文本 书籍作者

bookprice 数字 价格

bookstock 数字 书架号

(二).程序设计难点和应该注意的问题:

如何正确的往数据库中添加记录是本文要讨论的一个重点和难点,下面就是解决这一问题的

具体思路:

(1)创建并打开一个 OleDbConnection对象。

(2)创建一个插入一条记录的SQL语句。

(3)创建一个OleDbCommand对象。

(4)通过此OleDbCommand对象完成对插入一条记录到数据库的操作。

以下是在程序中实现的具体语句:

string strConn = " Provider = .4.0 ; Data Source = " ;

OleDbConnection myConn = new OleDbConnection ( strConn ) ;

( ) ;

string strInsert = " INSERT INTO books ( bookid , booktitle , bookauthor , bookprice , bookstock )

VALUES ( " ;

strInsert += t_ + ", '" ;

strInsert += t_ + "', '" ;

strInsert += t_ + "', " ;

strInsert += t_ + ", " ;

strInsert += t_ + ")" ;

OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ;

eNonQuery ( ) ;

( ) ;

(三).用Visual C#来插入记录的程序源代码( )和执行后的界面:

下图是编译后的执行界面:

源程序代码:

using System ;

using g ;

using entModel ;

using ;

using ;

using ;

//导入程序中使用到的名称空间

public class DataAdd : Form {

private Button lastrec ;

private Button nextrec ;

private Button previousrec ;

private Button firstrec ;

private Container components ;

private Label title ;

private Button t_new ;

private Button save ;

private TextBox t_bookstock ;

private TextBox t_bookprice ;

private TextBox t_bookauthor ;

private TextBox t_booktitle ;

private TextBox t_bookid ;

private Label l_bookstock ;

private Label l_bookprice ;

private Label l_bookauthor ;

private Label l_booktitle ;

private Label l_bookid ;

private DataSet myDataSet ;

private BindingManagerBase myBind ;

//定义在程序中要使用的组件

public DataAdd ( ) {

//连接到一个数据库

GetConnected ( ) ;

// 对窗体中所需要的内容进行初始化

InitializeComponent ( );

}

//释放程序使用过的所以资源

public override void Dispose ( ) {

e ( ) ;

e ( ) ;

}

public static void Main ( ) {

( new DataAdd ( ) ) ;

}

public void GetConnected ( )

{

try{

//创建一个 OleDbConnection对象

string strCon = " Provider = .4.0 ; Data Source = " ;

OleDbConnection myConn = new OleDbConnection ( strCon ) ;

string strCom = " SELECT * FROM books " ;

//创建一个 DataSet

myDataSet = new DataSet ( ) ;

( ) ;

//用 OleDbDataAdapter 得到一个数据集

OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;

//把Dataset绑定books数据表

( myDataSet , "books" ) ;

//关闭此OleDbConnection

( ) ;

}

catch ( Exception e )

{

( "连接错误! " + ng ( ) , "错误" ) ;

}

}

private void InitializeComponent ( )

{

components = new ner ( ) ;

nextrec = new Button ( ) ;

lastrec = new Button ( ) ;

previousrec = new Button ( ) ;

firstrec = new Button ( ) ;

t_bookprice = new TextBox ( ) ;

l_booktitle = new Label ( ) ;

l_bookprice = new Label ( ) ;

l_bookauthor = new Label ( ) ;

t_bookid = new TextBox ( ) ;

save = new Button ( ) ;

title = new Label ( ) ;

t_bookauthor = new TextBox ( ) ;

t_booktitle = new TextBox ( ) ;

t_new = new Button ( ) ;

l_bookstock = new Label ( ) ;

t_bookstock = new TextBox ( ) ;

l_bookid = new Label ( ) ;

//以下是对数据浏览的四个按钮进行初始化

on = new ( 65 , 312 ) ;

lor = ;

= new ( 40 , 24 ) ;

= new ("仿宋", 8f );

= "首记录";

+= new andler(GoFirst);

on = new ( 135 , 312 ) ;

lor = ;

= new (40, 24) ;

= new ( "仿宋" , 8f ) ;

= "上一条" ;

+= new andler ( GoPrevious ) ;

on = new ( 205 , 312 );

lor = ;

= new ( 40 , 24 ) ;

= new ( "仿宋" , 8f ) ;

= "下一条" ;

+= new andler ( GoNext );

on = new ( 275 , 312 ) ;

lor = ;

= new ( 40 , 24 ) ;

= new ( "仿宋" , 8f ) ;

= "尾记录" ;

+= new andler ( GoLast ) ;

//以下是对显示标签进行初始化

l_on = new ( 24 , 56 ) ;

l_ = "书本序号:" ;

l_ = new ( 112, 20 ) ;

l_ = new ( "仿宋" , 10f ) ;

l_ign = Center ;

l_on = new ( 24 , 108 ) ;

l_ = "书 名:";

l_ = new ( 112 , 20 ) ;

l_ = new ( "仿宋" , 10f ) ;

l_ign = Center ;

l_on = new ( 24 , 212 ) ;

l_ = "价 格:" ;

l_ = new ( 112 , 20 ) ;

l_ = new ( "仿宋" , 10f ) ;

l_ign = Center ;

l_on = new ( 24 , 264 ) ;

l_ = "书 架 号:" ;

l_ = new ( 112 , 20 ) ;

l_ = new ( "仿宋" , 10f ) ;

l_ex = 16 ;

l_ign = Center ;

l_on = new ( 24 , 160 ) ;

l_ = "作 者:" ;

l_ = new ( 112 , 20 ) ;

l_ = new ( "仿宋" , 10f ) ;

l_ign = Center ;

on = new ( 32 , 16 ) ;

= "利用Vsiual C#来增加数据记录!" ;

= new ( 336 , 24 ) ;

lor = ;

= new ( "仿宋" , 14f , ) ;

//以下是对为显示数据记录而设定的标签和文本框进行初始化,并把记录绑定在不同的绑定

到文本框"Text"属性上

t_on = new ( 184 , 56 ) ;

t_ = new ( 80 , 20 ) ;

t_ ( "Text" , myDataSet , "" ) ;

t_on = new ( 184 , 264 ) ;

t_ = new ( 80 , 20 ) ;

t_ ( "Text" , myDataSet , "ock" ) ;

t_on = new ( 184 , 108 ) ;

t_ = new ( 176 , 20 ) ;

t_( "Text" , myDataSet , "tle" ) ;

t_on = new ( 184 , 212 ) ;

t_ = new ( 80 , 20 ) ;

t_ ( "Text" , myDataSet , "ice" ) ;

t_on = new ( 184 , 160 ) ;

t_ = new ( 128 , 20 ) ;

t_ ( "Text" , myDataSet , "thor" ) ;

t_on = new ( 62 , 354 ) ;

t_ = new ( 96 , 32 ) ;

t_ = "新建记录" ;

t_ += new andler ( t_newClick ) ;

on = new ( 222 , 354 ) ;

= new ( 96 , 32 ) ;

ex = 4 ;

= "保存记录" ;

+= new andler ( saveClick ) ;

= "利用Vsiual C#来增加数据记录的程序窗口!" ;

aleBaseSize = new ( 5 , 13 ) ;

rderStyle = 3D ;

Size = new ( 390 , 400 ) ;

//在窗体中加入下列组件

( lastrec ) ;

( nextrec ) ;

( previousrec ) ;

( firstrec ) ;

( title ) ;

( t_new ) ;

( save ) ;

( t_bookstock ) ;

( t_bookprice ) ;

( t_bookauthor ) ;

( t_booktitle ) ;

( t_bookid ) ;

( l_bookstock ) ;

( l_bookprice ) ;

( l_bookauthor ) ;

( l_booktitle ) ;

( l_bookid ) ;

//把对象DataSet和"books"数据表绑定到此myBind对象

myBind= gContext [ myDataSet , "books" ] ;

}

protected void saveClick ( object sender , rgs e )

{

try

{

//判断所有字段是否添完,添完则执行,反之弹出提示

if ( t_ != "" && t_ != "" && t_ != "" &&

t_ != "" && t_ != "" )

{

string strConn = " Provider = .4.0 ; Data Source = " ;

OleDbConnection myConn = new OleDbConnection ( strConn ) ;

( ) ;

string strInsert = " INSERT INTO books ( bookid , booktitle , bookauthor , bookprice , bookstock )

VALUES ( " ;

strInsert += t_ + ", '" ;

strInsert += t_ + "', '" ;

strInsert += t_ + "', " ;

strInsert += t_ + ", " ;

strInsert += t_ + ")" ;

OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ;

eNonQuery ( ) ;

( ) ;

}

else

{

( "必须填满所有字段值!" , "错误!" ) ;

}

}

catch ( Exception ed )

{

( "保存数据记录发生 " + ng ( ) , "错误!" ) ;

}

}

protected void t_newClick ( object sender , rgs e )

{

t_ = "" ;

t_ = "" ;

t_ = "" ;

t_ = "" ;

t_ = "" ;

}

//按钮"尾记录"对象事件程序

protected void GoLast ( object sender , rgs e )

{

on = - 1 ;

}

//按钮"下一条"对象事件程序

protected void GoNext ( object sender , rgs e )

{

if ( on == -1 )

( "已经到了最后一条记录!" ) ;

else

on += 1 ;

}

//按钮"上一条"对象事件程序

protected void GoPrevious ( object sender , rgs e )

{

if ( on == 0 )

( "已经到了第一条记录!" ) ;

else

on -= 1 ;

}

//按钮"首记录"对象事件程序

protected void GoFirst ( object sender , rgs e )

{

on = 0 ;

}

}

成功编译上面代码,就可以往Access 2000数据库里面插入记录了。

二.用Visual C#往SQL SERVER数据库中插入记录:

(1)用Visual C#往Access 2000和SQL SERVER添加记录的主要区别在于使用了不同的数

据库引擎。在编写程序之前,首先假设数据库服务器名称为:server1,要访问的数据库名称

为:data1,数据表名称为:books。用户名为:sa。其中数据表的数据结构和Access 2000

的表的结构相同。下面是程序中打开SQL SERVER的数据引擎程序代码:

// 设定数据连接字符串,此字符串的意思是打开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 ) ;

( ) ;

(2).用Visual C#往SQL SERVER 数据库中插入记录的源程序代码( ):

using System ;

using g ;

using entModel ;

using ;

using ;

using ;

//导入程序中使用到的名称空间

public class DataAdd : Form {

private Button lastrec ;

private Button nextrec ;

private Button previousrec ;

private Button firstrec ;

private Container components ;

private Label title ;

private Button t_new ;

private Button save ;

private TextBox t_bookstock ;

private TextBox t_bookprice ;

private TextBox t_bookauthor ;

private TextBox t_booktitle ;

private TextBox t_bookid ;

private Label l_bookstock ;

private Label l_bookprice ;

private Label l_bookauthor ;

private Label l_booktitle ;

private Label l_bookid ;

private DataSet myDataSet ;

private BindingManagerBase myBind ;

//定义在程序中要使用的组件

public DataAdd ( ) {

//连接到一个数据库

GetConnected ( ) ;

// 对窗体中所需要的内容进行初始化

InitializeComponent ( );

}

//释放程序使用过的所以资源

public override void Dispose ( ) {

e ( ) ;

e ( ) ;

}

public static void Main ( ) {

( new DataAdd ( ) ) ;

}

public void GetConnected ( )

{

try{

// 设定数据连接字符串,此字符串的意思是打开Sql server数据库,服务器名称为server1,

数据库为data1,用户名为sa。

string strCon = "Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID = sa ; Initial

Catalog = data1 ; Data Source = server1 " ;

OleDbConnection myConn = new OleDbConnection ( strCon ) ;

( ) ;

string strCom = " SELECT * FROM books " ;

//创建一个 DataSet

myDataSet = new DataSet ( ) ;

//用 OleDbDataAdapter 得到一个数据集

OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;

//把Dataset绑定books数据表

( myDataSet , "books" ) ;

//关闭此OleDbConnection

( ) ;

}

catch ( Exception e )

{

( "连接错误! " + ng ( ) , "错误" ) ;

}

}

private void InitializeComponent ( )

{

components = new ner ( ) ;

nextrec = new Button ( ) ;

lastrec = new Button ( ) ;

previousrec = new Button ( ) ;

firstrec = new Button ( ) ;

t_bookprice = new TextBox ( ) ;

l_booktitle = new Label ( ) ;

l_bookprice = new Label ( ) ;

l_bookauthor = new Label ( ) ;

t_bookid = new TextBox ( ) ;

save = new Button ( ) ;

title = new Label ( ) ;

t_bookauthor = new TextBox ( ) ;

t_booktitle = new TextBox ( ) ;

t_new = new Button ( ) ;

l_bookstock = new Label ( ) ;

t_bookstock = new TextBox ( ) ;

l_bookid = new Label ( ) ;

//以下是对数据浏览的四个按钮进行初始化

on = new ( 65 , 312 ) ;

lor = ;

= new ( 40 , 24 ) ;

= new ("仿宋", 8f );

= "首记录";

+= new andler(GoFirst);

on = new ( 135 , 312 ) ;

lor = ;

= new (40, 24) ;

= new ( "仿宋" , 8f ) ;

= "上一条" ;

+= new andler ( GoPrevious ) ;

on = new ( 205 , 312 );

lor = ;

= new ( 40 , 24 ) ;

= new ( "仿宋" , 8f ) ;

= "下一条" ;

+= new andler ( GoNext );

on = new ( 275 , 312 ) ;

lor = ;

= new ( 40 , 24 ) ;

= new ( "仿宋" , 8f ) ;

= "尾记录" ;

+= new andler ( GoLast ) ;

//以下是对显示标签进行初始化

l_on = new ( 24 , 56 ) ;

l_ = "书本序号:" ;

l_ = new ( 112, 20 ) ;

l_ = new ( "仿宋" , 10f ) ;

l_ign = Center ;

l_on = new ( 24 , 108 ) ;

l_ = "书 名:";

l_ = new ( 112 , 20 ) ;

l_ = new ( "仿宋" , 10f ) ;

l_ign = Center ;

l_on = new ( 24 , 212 ) ;

l_ = "价 格:" ;

l_ = new ( 112 , 20 ) ;

l_ = new ( "仿宋" , 10f ) ;

l_ign = Center ;

l_on = new ( 24 , 264 ) ;

l_ = "书 架 号:" ;

l_ = new ( 112 , 20 ) ;

l_ = new ( "仿宋" , 10f ) ;

l_ex = 16 ;

l_ign = Center ;

l_on = new ( 24 , 160 ) ;

l_ = "作 者:" ;

l_ = new ( 112 , 20 ) ;

l_ = new ( "仿宋" , 10f ) ;

l_ign = Center ;

on = new ( 32 , 16 ) ;

= "利用Vsiual C#来增加数据记录!" ;

= new ( 336 , 24 ) ;

lor = ;

= new ( "仿宋" , 14f , ) ;

//以下是对为显示数据记录而设定的标签和文本框进行初始化,并把记录绑定在不同的绑定

到文本框"Text"属性上

t_on = new ( 184 , 56 ) ;

t_ = new ( 80 , 20 ) ;

t_ ( "Text" , myDataSet , "" ) ;

t_on = new ( 184 , 264 ) ;

t_ = new ( 80 , 20 ) ;

t_ ( "Text" , myDataSet , "ock" ) ;

t_on = new ( 184 , 108 ) ;

t_ = new ( 176 , 20 ) ;

t_( "Text" , myDataSet , "tle" ) ;

t_on = new ( 184 , 212 ) ;

t_ = new ( 80 , 20 ) ;

t_ ( "Text" , myDataSet , "ice" ) ;

t_on = new ( 184 , 160 ) ;

t_ = new ( 128 , 20 ) ;

t_ ( "Text" , myDataSet , "thor" ) ;

t_on = new ( 62 , 354 ) ;

t_ = new ( 96 , 32 ) ;

t_ = "新建记录" ;

t_ += new andler ( t_newClick ) ;

on = new ( 222 , 354 ) ;

= new ( 96 , 32 ) ;

ex = 4 ;

= "保存记录" ;

+= new andler ( saveClick ) ;

= "利用Vsiual C#来增加数据记录的程序窗口!" ;

aleBaseSize = new ( 5 , 13 ) ;

rderStyle = 3D ;

Size = new ( 390 , 400 ) ;

//在窗体中加入下列组件

( lastrec ) ;

( nextrec ) ;

( previousrec ) ;

( firstrec ) ;

( title ) ;

( t_new ) ;

( save ) ;

( t_bookstock ) ;

( t_bookprice ) ;

( t_bookauthor ) ;

( t_booktitle ) ;

( t_bookid ) ;

( l_bookstock ) ;

( l_bookprice ) ;

( l_bookauthor ) ;

( l_booktitle ) ;

( l_bookid ) ;

//把对象DataSet和"books"数据表绑定到此myBind对象

myBind= gContext [ myDataSet , "books" ] ;

}

protected void saveClick ( object sender , rgs e )

{

try

{

//判断所有字段是否添完,添完则执行,反之弹出提示

if ( t_ != "" && t_ != "" && t_ != "" &&

t_ != "" && t_ != "" )

{

// 设定数据连接字符串,此字符串的意思是打开Sql server数据库,服务器名称为server1,

数据库为data1,用户名为sa。

string strConn = "Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID = sa ; Initial

Catalog = datal ; Data Source = server1 " ;

OleDbConnection myConn = new OleDbConnection ( strConn ) ;

( ) ;

string strInsert = " INSERT INTO books ( bookid , booktitle , bookauthor , bookprice , bookstock )

VALUES ( " ;

strInsert += t_ + ", '" ;

strInsert += t_ + "', '" ;

strInsert += t_ + "', " ;

strInsert += t_ + ", " ;

strInsert += t_ + ")" ;

OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ;

eNonQuery ( ) ;

( ) ;

}

else

{

( "必须填满所有字段值!" , "错误!" ) ;

}

}

catch ( Exception ed )

{

( "保存数据记录发生 " + ng ( ) , "错误!" ) ;

}

}

protected void t_newClick ( object sender , rgs e )

{

t_ = "" ;

t_ = "" ;

t_ = "" ;

t_ = "" ;

t_ = "" ;

}

//按钮"尾记录"对象事件程序

protected void GoLast ( object sender , rgs e )

{

on = - 1 ;

}

//按钮"下一条"对象事件程序

protected void GoNext ( object sender , rgs e )

{

if ( on == -1 )

( "已经到了最后一条记录!" ) ;

else

on += 1 ;

}

//按钮"上一条"对象事件程序

protected void GoPrevious ( object sender , rgs e )

{

if ( on == 0 )

( "已经到了第一条记录!" ) ;

else

on -= 1 ;

}

//按钮"首记录"对象事件程序

protected void GoFirst ( object sender , rgs e )

{

on = 0 ;

}

}

三.总结:

本文主要是通过二个程序的例子来具体说明用Visual C#如何往远程数据库--SQL SERVER

和本地数据库-- Access 2000中插入记录。对于其他类型的数据库也可以比照这二个这二个

数据库来处理,一般来说,用Visual C#处理数据库只是在选用数据库引擎上有较大的差别,

在程序中的具体设计和处理上,还是很类似的。最后希望此篇文章能对你的数据库编程有所

帮助。

本文标签: 记录数据库程序数据