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

首頁(yè) > 開(kāi)發(fā) > 綜合 > 正文

用Visual C#來(lái)增加數(shù)據(jù)記錄(轉(zhuǎn))----------1

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

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

程序中使用的數(shù)據(jù)庫(kù)名稱(chēng)為sample.mdb,在此數(shù)據(jù)庫(kù)中有一張數(shù)據(jù)表books。此數(shù)據(jù)表的結(jié)構(gòu)如下:
字段名稱(chēng) 字段類(lèi)型 代表意思
bookid 數(shù)字 序號(hào)
booktitle 文本 書(shū)籍名稱(chēng)
bookauthor 文本 書(shū)籍作者
bookprice 數(shù)字 價(jià)格
bookstock 數(shù)字 書(shū)架號(hào)

(二).程序設(shè)計(jì)難點(diǎn)和應(yīng)該注意的問(wèn)題:
如何正確的往數(shù)據(jù)庫(kù)中添加記錄是本文要討論的一個(gè)重點(diǎn)和難點(diǎn),下面就是解決這一問(wèn)題的具體思路:
(1)創(chuàng)建并打開(kāi)一個(gè) oledbconnection對(duì)象。
(2)創(chuàng)建一個(gè)插入一條記錄的sql語(yǔ)句。
(3)創(chuàng)建一個(gè)oledbcommand對(duì)象。
(4)通過(guò)此oledbcommand對(duì)象完成對(duì)插入一條記錄到數(shù)據(jù)庫(kù)的操作。
以下是在程序中實(shí)現(xiàn)的具體語(yǔ)句:
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#來(lái)插入記錄的程序源代碼( 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)入程序中使用到的名稱(chēng)空間
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 ( ) {
//連接到一個(gè)數(shù)據(jù)庫(kù)
getconnected ( ) ;
// 對(duì)窗體中所需要的內(nèi)容進(jìn)行初始化
initializecomponent ( );
}
//釋放程序使用過(guò)的所以資源
public override void dispose ( ) {
base.dispose ( ) ;
components.dispose ( ) ;
}
public static void main ( ) {
application.run ( new dataadd ( ) ) ;
}
public void getconnected ( )
{
try{
//創(chuàng)建一個(gè) oledbconnection對(duì)象
string strcon = " provider = microsoft.jet.oledb.4.0 ; data source = sample.mdb" ;
oledbconnection myconn = new oledbconnection ( strcon ) ;
string strcom = " select * from books " ;
//創(chuàng)建一個(gè) dataset
mydataset = new dataset ( ) ;
myconn.open ( ) ;
//用 oledbdataadapter 得到一個(gè)數(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 ( "連接錯(cuò)誤! " + e.tostring ( ) , "錯(cuò)誤" ) ;
}
}
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 ( ) ;
//以下是對(duì)數(shù)據(jù)瀏覽的四個(gè)按鈕進(jìn)行初始化
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 ) ;
//以下是對(duì)顯示標(biāo)簽進(jìn)行初始化
l_bookid.location = new system.drawing.point ( 24 , 56 ) ;
l_bookid.text = "書(shū)本序號(hào):" ;
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 = "書(shū) 名:";
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 = "價(jià) 格:" ;
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 = "書(shū) 架 號(hào):" ;
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#來(lái)增加數(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 ) ;
//以下是對(duì)為顯示數(shù)據(jù)記錄而設(shè)定的標(biāo)簽和文本框進(jìn)行初始化,并把記錄綁定在不同的綁定到文本框"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#來(lái)增加數(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 ) ;
//把對(duì)象dataset和"books"數(shù)據(jù)表綁定到此mybind對(duì)象
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 ( "必須填滿(mǎn)所有字段值!" , "錯(cuò)誤!" ) ;
}
}
catch ( exception ed )
{
messagebox.show ( "保存數(shù)據(jù)記錄發(fā)生 " + ed.tostring ( ) , "錯(cuò)誤!" ) ;
}
}
protected void t_newclick ( object sender , system.eventargs e )
{
t_bookid.text = "" ;
t_booktitle.text = "" ;
t_bookauthor.text = "" ;
t_bookprice.text = "" ;
t_bookstock.text = "" ;
}
//按鈕"尾記錄"對(duì)象事件程序
protected void golast ( object sender , system.eventargs e )
{
mybind.position = mybind.count - 1 ;
}

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


成功編譯上面代碼,就可以往access 2000數(shù)據(jù)庫(kù)里面插入記錄了。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 定西市| 延川县| 吉林省| 锦州市| 蒲江县| 阳信县| 昌吉市| 丹巴县| 崇明县| 滦南县| 中超| 鞍山市| 上饶市| 石河子市| 卫辉市| 临洮县| 灯塔市| 奉化市| 芜湖市| 织金县| 湛江市| 鄂州市| 淮滨县| 光泽县| 图木舒克市| 四会市| 中西区| 张家界市| 资兴市| 古蔺县| 淮北市| 揭西县| 喀什市| 慈溪市| 上蔡县| 新河县| 玛沁县| 永德县| 宿迁市| 白水县| 通江县|