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

首頁 > 開發 > 綜合 > 正文

C#數據庫編程2

2024-07-21 02:23:40
字體:
來源:轉載
供稿:網友
四.插入數據記錄: </p><p>  對數據庫進行插入記錄操作和刪除記錄操作基本的思路是一致的,就是通過ado.net首先插入數據記錄到數據庫,然后對"dataset"對象進行必要的修改。下列代碼就是以access 2000數據庫為模型修改當前記錄的代碼: </p><p>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 ;

} </p><p>  由于對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數據庫為模板編程的完成源程序代碼了。所以本文就不再提供了。



  七.總結:



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






  • 網站運營seo文章大全
  • 提供全面的站長運營經驗及seo技術!
  • 發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 孟连| 合肥市| 大方县| 通道| 台江县| 吴忠市| 布拖县| 中卫市| 东乡县| 顺义区| 乌拉特前旗| 德江县| 禄丰县| 孟津县| 简阳市| 广州市| 平果县| 淮滨县| 清水河县| 南川市| 馆陶县| 嘉禾县| 海阳市| 汽车| 鹤岗市| 北安市| 团风县| 平昌县| 德保县| 井冈山市| 海盐县| 迁西县| 阳春市| 广水市| 沿河| 泰宁县| 太保市| 新巴尔虎左旗| 固安县| 高阳县| 师宗县|