用visual c#来增加数据记录(1)_c#应用

2008-02-23 05:46:37来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

在本篇文章中,我们将介绍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)本文程式使用的数据库的介绍:

程式中使用的数据库名称为sample.mdb,在此数据库中有一张数据表books。此数据表的结构如下:
字段名称 字段类型 代表意思
Bookid 数字 序号
booktitle 文本 书籍名称
bookauthor 文本 书籍作者
bookprice 数字 价格
bookstock 数字 书架号

(二).程式设计难点和应该注意的问题:
如何正确的往数据库中添加记录是本文要讨论的一个重点和难点,下面就是解决这一问题的具体思路:
(1)创建并打开一个 OleDbConnection对象。
(2)创建一个插入一条记录的SQL语句。
(3)创建一个OleDbCommand对象。
(4)通过此OleDbCommand对象完成对插入一条记录到数据库的操作。
以下是在程式中实现的具体语句:
string strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = sample.mdb " ;
OleDbConnection myConn = new OleDbConnection ( strConn ) ;
myConn.Open ( ) ;
string strInsert = " INSERT INTO books ( bookid , booktitle , bookauthor , bookprice , bookstock ) VALUES ( " ;
strInsert = t_bookid.Text ", " ;
strInsert = t_booktitle.Text ", " ;
strInsert = t_bookauthor.Text ", " ;
strInsert = t_bookprice.Text ", " ;
strInsert = t_bookstock.Text ")" ;
OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ;
inst.ExecuteNonQuery ( ) ;
myConn.Close ( ) ;


(三).用Visual C#来插入记录的程式源代码( add.cs )和执行后的界面:
下图是add.cs编译后的执行界面:

add.cs源程式代码:
using System ;
using System.Drawing ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data.OleDb ;
using System.Data ;
//导入程式中使用到的名称空间
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 ( ) {
base.Dispose ( ) ;
components.Dispose ( ) ;
}
public static void Main ( ) {
Application.Run ( new DataAdd ( ) ) ;
}
public void GetConnected ( )
{
try{
//创建一个 OleDbConnection对象
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = sample.mdb" ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom = " SELECT * FROM books " ;
//创建一个 DataSet
myDataSet = new DataSet ( ) ;
myConn.Open ( ) ;
//用 OleDbDataAdapter 得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
//把Dataset绑定books数据表
myCommand.Fill ( myDataSet , "books" ) ;
//关闭此OleDbConnection
myConn.Close ( ) ;
}
catch ( Exception e )
{
MessageBox.Show ( "连接错误! " e.ToString ( ) , "错误" ) ;
}
}
private void InitializeComponent ( )
{
components = new System.ComponentModel.Container ( ) ;
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 ( ) ;
//以下是对数据浏览的四个按钮进行初始化
firstrec.Location = new System.Drawing.Point ( 65 , 312 ) ;
firstrec.ForeColor = System.Drawing.Color.Black ;
firstrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
firstrec.Font = new System.Drawing.Font("仿宋", 8f );
firstrec.Text = "首记录";
firstrec.Click = new System.EventHandler(GoFirst);
previousrec.Location = new System.Drawing.Point ( 135 , 312 ) ;
previousrec.ForeColor = System.Drawing.Color.Black ;
previousrec.Size = new System.Drawing.Size(40, 24) ;

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇: vsiaul c#如何读取注册信息_c#应用

下一篇: 用visual c#中轻松浏览数据库记录_c#应用