国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

用Visual C#來增加數(shù)據(jù)記錄(1)

2024-07-21 02:22:33
字體:
供稿:網(wǎng)友
  在本篇文章中,我們將介紹visual c#對數(shù)據(jù)庫的一個基本操作,即:如何往數(shù)據(jù)庫中添加記錄。我們將通過一些數(shù)據(jù)庫操作的例子,來具體說明一下。為了更清楚的說明這個問題,在選用數(shù)據(jù)庫方面采用了二種當前比較典型的數(shù)據(jù)庫,其一是本地數(shù)據(jù)庫--access 2000,另外一個是遠程數(shù)據(jù)庫--sql server 7.0。首先介紹如何用visual c#來添加access 2000數(shù)據(jù)庫的記錄。

一.用visual c#來添加access 2000數(shù)據(jù)庫的記錄
(一).程序設(shè)計和運行的環(huán)境設(shè)置:
(1)視窗2000服務(wù)器版
(2)microsoft data acess component 2.6 以上版本 ( mdac 2.6 )
(3)本文程序使用的數(shù)據(jù)庫的介紹:

程序中使用的數(shù)據(jù)庫名稱為sample.mdb,在此數(shù)據(jù)庫中有一張數(shù)據(jù)表books。此數(shù)據(jù)表的結(jié)構(gòu)如下:
字段名稱字段類型代表意思bookid數(shù)字序號booktitle文本書籍名稱bookauthor文本書籍作者bookprice數(shù)字價格bookstock數(shù)字書架號
(二).程序設(shè)計難點和應(yīng)該注意的問題:
如何正確的往數(shù)據(jù)庫中添加記錄是本文要討論的一個重點和難點,下面就是解決這一問題的具體思路:
(1)創(chuàng)建并打開一個 oledbconnection對象。
(2)創(chuàng)建一個插入一條記錄的sql語句。
(3)創(chuàng)建一個oledbcommand對象。
(4)通過此oledbcommand對象完成對插入一條記錄到數(shù)據(jù)庫的操作。
以下是在程序中實現(xiàn)的具體語句:
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 )和執(zhí)行后的界面:
下圖是add.cs編譯后的執(zhí)行界面:

add.cs源程序代碼:
using system ;
using system.drawing ;
using system.componentmodel ;
using system.windows.forms ;
using system.data.oledb ;
using system.data ;
//導(dǎo)入程序中使用到的名稱空間
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 ( ) {
//連接到一個數(shù)據(jù)庫
getconnected ( ) ;
// 對窗體中所需要的內(nèi)容進行初始化
initializecomponent ( );
}
//釋放程序使用過的所以資源
public override void dispose ( ) {
base.dispose ( ) ;
components.dispose ( ) ;
}
public static void main ( ) {
application.run ( new dataadd ( ) ) ;
}
public void getconnected ( )
{
try{
//創(chuàng)建一個 oledbconnection對象
string strcon = " provider = microsoft.jet.oledb.4.0 ; data source = sample.mdb" ;
oledbconnection myconn = new oledbconnection ( strcon ) ;
string strcom = " select * from books " ;
//創(chuàng)建一個 dataset
mydataset = new dataset ( ) ;
myconn.open ( ) ;
//用 oledbdataadapter 得到一個數(shù)據(jù)集
oledbdataadapter mycommand = new oledbdataadapter ( strcom , myconn ) ;
//把dataset綁定books數(shù)據(jù)表
mycommand.fill ( mydataset , "books" ) ;
//關(guān)閉此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 ( ) ;
//以下是對數(shù)據(jù)瀏覽的四個按鈕進行初始化
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) ;
previousrec.font = new system.drawing.font ( "仿宋" , 8f ) ;
previousrec.text = "上一條" ;
previousrec.click += new system.eventhandler ( goprevious ) ;
nextrec.location = new system.drawing.point ( 205 , 312 );
nextrec.forecolor = system.drawing.color.black ;
nextrec.size = new system.drawing.size ( 40 , 24 ) ;
nextrec.font = new system.drawing.font ( "仿宋" , 8f ) ;
nextrec.text = "下一條" ;
nextrec.click += new system.eventhandler ( gonext );
lastrec.location = new system.drawing.point ( 275 , 312 ) ;
lastrec.forecolor = system.drawing.color.black ;
lastrec.size = new system.drawing.size ( 40 , 24 ) ;
lastrec.font = new system.drawing.font ( "仿宋" , 8f ) ;
lastrec.text = "尾記錄" ;
lastrec.click += new system.eventhandler ( golast ) ;
//以下是對顯示標簽進行初始化
l_bookid.location = new system.drawing.point ( 24 , 56 ) ;
l_bookid.text = "書本序號:" ;
l_bookid.size = new system.drawing.size ( 112, 20 ) ;
l_bookid.font = new system.drawing.font ( "仿宋" , 10f ) ;
l_bookid.textalign = system.drawing.contentalignment.middlecenter ;
l_booktitle.location = new system.drawing.point ( 24 , 108 ) ;
l_booktitle.text = "書 名:";
l_booktitle.size = new system.drawing.size ( 112 , 20 ) ;
l_booktitle.font = new system.drawing.font ( "仿宋" , 10f ) ;
l_booktitle.textalign = system.drawing.contentalignment.middlecenter ;
l_bookprice.location = new system.drawing.point ( 24 , 212 ) ;
l_bookprice.text = "價 格:" ;
l_bookprice.size = new system.drawing.size ( 112 , 20 ) ;
l_bookprice.font = new system.drawing.font ( "仿宋" , 10f ) ;
l_bookprice.textalign = system.drawing.contentalignment.middlecenter ;
l_bookstock.location = new system.drawing.point ( 24 , 264 ) ;
l_bookstock.text = "書 架 號:" ;
l_bookstock.size = new system.drawing.size ( 112 , 20 ) ;
l_bookstock.font = new system.drawing.font ( "仿宋" , 10f ) ;
l_bookstock.tabindex = 16 ;
l_bookstock.textalign = system.drawing.contentalignment.middlecenter ;
l_bookauthor.location = new system.drawing.point ( 24 , 160 ) ;
l_bookauthor.text = "作 者:" ;
l_bookauthor.size = new system.drawing.size ( 112 , 20 ) ;
l_bookauthor.font = new system.drawing.font ( "仿宋" , 10f ) ;
l_bookauthor.textalign = system.drawing.contentalignment.middlecenter ;
title.location = new system.drawing.point ( 32 , 16 ) ;
title.text = "利用vsiual c#來增加數(shù)據(jù)記錄!" ;
title.size = new system.drawing.size ( 336 , 24 ) ;
title.forecolor = system.drawing.color.green ;
title.font = new system.drawing.font ( "仿宋" , 14f , system.drawing.fontstyle.bold ) ;
//以下是對為顯示數(shù)據(jù)記錄而設(shè)定的標簽和文本框進行初始化,并把記錄綁定在不同的綁定到文本框"text"屬性上
t_bookid.location = new system.drawing.point ( 184 , 56 ) ;
t_bookid.size = new system.drawing.size ( 80 , 20 ) ;
t_bookid.databindings.add ( "text" , mydataset , "books.bookid" ) ;
t_bookstock.location = new system.drawing.point ( 184 , 264 ) ;
t_bookstock.size = new system.drawing.size ( 80 , 20 ) ;
t_bookstock.databindings.add ( "text" , mydataset , "books.bookstock" ) ;
t_booktitle.location = new system.drawing.point ( 184 , 108 ) ;
t_booktitle.size = new system.drawing.size ( 176 , 20 ) ;
t_booktitle.databindings.add( "text" , mydataset , "books.booktitle" ) ;
t_bookprice.location = new system.drawing.point ( 184 , 212 ) ;
t_bookprice.size = new system.drawing.size ( 80 , 20 ) ;
t_bookprice.databindings.add ( "text" , mydataset , "books.bookprice" ) ;
t_bookauthor.location = new system.drawing.point ( 184 , 160 ) ;
t_bookauthor.size = new system.drawing.size ( 128 , 20 ) ;
t_bookauthor.databindings.add ( "text" , mydataset , "books.bookauthor" ) ;
t_new.location = new system.drawing.point ( 62 , 354 ) ;
t_new.size = new system.drawing.size ( 96 , 32 ) ;
t_new.text = "新建記錄" ;
t_new.click += new system.eventhandler ( t_newclick ) ;
save.location = new system.drawing.point ( 222 , 354 ) ;
save.size = new system.drawing.size ( 96 , 32 ) ;
save.tabindex = 4 ;
save.text = "保存記錄" ;
save.click += new system.eventhandler ( saveclick ) ;
this.text = "利用vsiual c#來增加數(shù)據(jù)記錄的程序窗口!" ;
this.autoscalebasesize = new system.drawing.size ( 5 , 13 ) ;
this.formborderstyle = formborderstyle.fixed3d ;
this.clientsize = new system.drawing.size ( 390 , 400 ) ;
//在窗體中加入下列組件
this.controls.add ( lastrec ) ;
this.controls.add ( nextrec ) ;
this.controls.add ( previousrec ) ;
this.controls.add ( firstrec ) ;
this.controls.add ( title ) ;
this.controls.add ( t_new ) ;
this.controls.add ( save ) ;
this.controls.add ( t_bookstock ) ;
this.controls.add ( t_bookprice ) ;
this.controls.add ( t_bookauthor ) ;
this.controls.add ( t_booktitle ) ;
this.controls.add ( t_bookid ) ;
this.controls.add ( l_bookstock ) ;
this.controls.add ( l_bookprice ) ;
this.controls.add ( l_bookauthor ) ;
this.controls.add ( l_booktitle ) ;
this.controls.add ( l_bookid ) ;
//把對象dataset和"books"數(shù)據(jù)表綁定到此mybind對象
mybind= this.bindingcontext [ mydataset , "books" ] ;
}
protected void saveclick ( object sender , system.eventargs e )
{
try
{
//判斷所有字段是否添完,添完則執(zhí)行,反之彈出提示
if ( t_bookid.text != "" && t_booktitle.text != "" && t_bookauthor.text != "" && t_bookprice.text != "" && t_bookstock.text != "" )
{
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 ( ) ;
}
else
{
messagebox.show ( "必須填滿所有字段值!" , "錯誤!" ) ;
}
}
catch ( exception ed )
{
messagebox.show ( "保存數(shù)據(jù)記錄發(fā)生 " + ed.tostring ( ) , "錯誤!" ) ;
}
}
protected void t_newclick ( object sender , system.eventargs e )
{
t_bookid.text = "" ;
t_booktitle.text = "" ;
t_bookauthor.text = "" ;
t_bookprice.text = "" ;
t_bookstock.text = "" ;
}
//按鈕"尾記錄"對象事件程序
protected void golast ( object sender , system.eventargs e )
{
mybind.position = mybind.count - 1 ;
}

//按鈕"下一條"對象事件程序
protected void gonext ( object sender , system.eventargs e )
{
if ( mybind.position == mybind.count -1 )
messagebox.show ( "已經(jīng)到了最后一條記錄!" ) ;
else
mybind.position += 1 ;
}
//按鈕"上一條"對象事件程序
protected void goprevious ( object sender , system.eventargs e )
{
if ( mybind.position == 0 )
messagebox.show ( "已經(jīng)到了第一條記錄!" ) ;
else
mybind.position -= 1 ;
}
//按鈕"首記錄"對象事件程序
protected void gofirst ( object sender , system.eventargs e )
{
mybind.position = 0 ;
}
}

成功編譯上面代碼,就可以往access 2000數(shù)據(jù)庫里面插入記錄了。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: SHOW| 蓬溪县| 湟源县| 卓资县| 宜兴市| 井冈山市| 富源县| 忻城县| 临颍县| 新津县| 永昌县| 宕昌县| 千阳县| 贡嘎县| 丹东市| 怀安县| 区。| 大埔县| 铜陵市| 嘉定区| 宣恩县| 恭城| 开原市| 大石桥市| 漳浦县| 财经| 天等县| 屯昌县| 宁南县| 元谋县| 遵义市| 松阳县| 肃宁县| 扶风县| 调兵山市| 尚义县| 福州市| 汨罗市| 南平市| 高碑店市| 霞浦县|