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

首頁 > 開發 > 綜合 > 正文

祥解Visual C#數據庫編程

2024-07-21 02:20:53
字體:
來源:轉載
供稿:網友
關于數據庫編程,微軟提供了一個統一的數據對象訪問模型,在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 文本 郵政編碼

  遠程數據庫sql server 2000的數據庫服務器名稱為"server1",數據庫名稱為"data1",登陸的id為"sa",口令為空,在數據庫也定義了一張"person"表,數據結構如上表。
二.如何瀏覽數據:

  在《visual c#的數據綁定》中,已經了解了如何把數據集中的某些字段綁定到winform組件的某個屬性上,這樣程序員就可以根據以winform組件的來定制數據顯示的形式,并且此時的winform組件顯示內容就可以隨著記錄指針的變化而改變。至此可見,瀏覽數據記錄的關鍵就是如何改變記錄指針。要實現這種操作,就要使用到bindingmanagerbase類,此類的主要作用是管理對于那些實現了對同一個數據源進行綁定的對象。說的具體些,就是能夠使得windows窗體上的已經對同一數據源進行數據綁定的組件保持同步。在bindingmanagerbase類中定義了一個屬性"position",通過這個屬性就可以改變bindingmanagerbase對象中的數據指針。創建bindingmanagerbase對象必須要使用到bindingcontext類,其實每一個由control類中繼承而得到的對象,都有單一的bindingcontext對象,在大多數創建窗體中實現數據綁定組件的bindingmanagerbase對象是使用form類的bindingcontext來得到。下列代碼是以access 2000數據庫為模型,創建的一個名稱為"mybind"的bindingmanagerbase對象。

//創建一個 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 2000數據庫為模型,創建一個名稱為"mybind"的bindingmanagerbase對象。

// 設定數據連接字符串,此字符串的意思是打開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" ] ;

  得到了是同一數據源的bindingmanagerbase對象,通過改變此對象的"position"屬性值,這樣綁定數據的組件顯示的數據就隨之變化,從而實現導航數據記錄。

  < i > .導航按鈕"上一條"實現方法:

protected void goprevious ( object sender , system.eventargs e )
{
if ( mybind.position == 0 )
messagebox.show ( "已經到了第一條記錄!" , "信息提示!" , messageboxbuttons.ok , messageboxicon.information ) ;
else
mybind.position -= 1 ;
}

  < ii > . 導航按鈕"下一條"實現方法:

protected void gonext ( object sender , system.eventargs e )
{
if ( mybind.position == mybind.count -1 )
messagebox.show ( "已經到了最后一條記錄!", "信息提示!" , messageboxbuttons.ok , messageboxicon.information ) ;
else
mybind.position += 1 ;
}

  < iii > . 導航按鈕"至尾"實現方法:

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 ;
}

  注釋:"count"是bindingmanagerbase對象的另外一個重要的屬性,是數據集記錄的總數。
三.實現刪除記錄:

  在對數據記錄進行操作的時候,有二點必須十分清晰:

  其一:在對數據記錄進行操作的時候,我想有一些程序員一定有這樣一個疑惑,當對數據庫服務器請求數據集的時候,就會產生"dataset"對象,用以管理數據集,這樣如果這些對數據庫服務器的請求非常多,同樣也就會產生很多的"dataset"對象,達到一定時候必然會使得數據庫服務器崩潰。這種想法是自然的,但和實際并不相符,因為"dataset"對象并不是在服務器端產生的,而是在客戶端產生的。所以面對眾多的數據請求的時候對數據庫服務器的影響并不十分太大。

  其二:記得在用delphi編寫三層數據模型的時候的,每一次對數據庫的修改其實只是對第二層產生的數據集的修改,要真正修改數據庫,還必須調用一個另外的方法。在用ado.net處理數據庫的時候,雖然處理的直接對象是數據庫,但此時"dataset"對象中的內容并沒有隨之改變,而綁定的數據組件顯示的數據又來源于"dataset"對象,這樣就會產生一個錯覺,就是修改了的記錄并沒有修改掉,刪除的記錄并沒有刪除掉。所以對數據記錄進行操作的時候,在修改數據庫后,還要對"dataset"對象進行必要的修改,這樣才能保證"dataset"對象和數據庫內容一致、同步。下面代碼是刪除當前綁定組件顯示的記錄的程序代碼,此代碼是以access 2000數據庫為模板的:

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 ( ) , "錯誤!" ) ;
}
}
}

四.插入數據記錄:

  對數據庫進行插入記錄操作和刪除記錄操作基本的思路是一致的,就是通過ado.net首先插入數據記錄到數據庫,然后對"dataset"對象進行必要的修改。下列代碼就是以access 2000數據庫為模型修改當前記錄的代碼:

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 ;
}

  由于對sql server 2000數據記錄修改操作和access 2000數據記錄修改操作的差異只在于不同的數據鏈接,具體的代碼可以參考"刪除數據記錄"中的代碼,在這里就不提供了。
五.插入數據記錄:

  和前面二種操作在思路是一致的,就是通過ado.net首先插入數據記錄到數據庫,然后對"dataset"對象進行必要的修改。下列代碼就是以access 2000數據庫為模型插入一條數據記錄的代碼

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 ( ) , "錯誤!" ) ;
}
}

  同樣對sql server 2000數據庫進行插入記錄操作和access 2000數據庫插入記錄操作的差異也只在于不同的數據鏈接,具體的代碼可以參考"刪除數據記錄"中的代碼,在這里就不提供了。
六.visual c#數據庫編程的完成源代碼和程序運行的主界面:

  掌握了上面要點,編寫一個完整的數據庫編程的程序就顯得非常容易了,下面是visual c#進行數據庫編程的完整代碼(data01.cs),此代碼是以access 2000數據庫為模型設計的,具體如下:

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 ;
}
}

對于以sql server 2000數據庫為模型的程序代碼,只要把data01.cs中的數據鏈接,即:
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數據庫為模板編程的完成源程序代碼了。所以本文就不再提供了。

  七.總結:

  數據庫編程始終是程序編程內容中的一個重點和難點。而以上介紹的這些操作又是數據庫編程中最為基本,也是最為重要的內容。那些復雜的編程無非是以上這些處理的若干個疊加。



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 壤塘县| 昭觉县| 凌海市| 中牟县| 枞阳县| 华容县| 巴马| 仁布县| 自贡市| 牡丹江市| 拉孜县| 梨树县| 松原市| 霸州市| 汝南县| 彭州市| 肇庆市| 孝昌县| 乌恰县| 房产| 高唐县| 徐州市| 景宁| 旺苍县| 辛集市| 江孜县| 平武县| 合作市| 永春县| 通海县| 藁城市| 南汇区| 定兴县| 元氏县| 玉溪市| 宝山区| 双峰县| 历史| 锡林浩特市| 乐都县| 阿克|