自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

精通C#數(shù)據(jù)庫編程及相關(guān)實例

開發(fā) 后端
這里將介紹精通C#數(shù)據(jù)庫編程及相關(guān)實例。本文就來著重探討一下如何精通C#數(shù)據(jù)庫編程,即:如何瀏覽記錄、修改記錄、刪除記錄和插入記錄。

針對數(shù)據(jù)庫編程始終是程序設(shè)計語言的一個重要方面的內(nèi)容,也是一個難點。數(shù)據(jù)庫編程的內(nèi)容十分豐富,但最為基本編程的也就是那么幾點,譬如:連接數(shù)據(jù)庫、得到需要的數(shù)據(jù)和針對數(shù)據(jù)記錄的瀏覽、刪除、修改、插入等操作。其中又以后面針對數(shù)據(jù)記錄的數(shù)據(jù)操作為重點。本文就來著重探討一下如何精通C#數(shù)據(jù)庫編程,即:如何瀏覽記錄、修改記錄、刪除記錄和插入記錄。

一.程序設(shè)計和運(yùn)行的環(huán)境設(shè)置:

(1).視窗2000服務(wù)器版

(2).MicrosoftDataAcessComponent2.6以上版本(MDAC2.6)

(3)..NetFrameWorkSDKBeta2

為了更清楚的說明問題,在數(shù)據(jù)庫的選用上,采用了當(dāng)前比較典型的數(shù)據(jù)庫,一個是本地數(shù)據(jù)庫Access2000,另外一個是遠(yuǎn)程數(shù)據(jù)庫SqlServer2000。其中本地數(shù)據(jù)庫名稱為"db.mdb",在其中定義了一張數(shù)據(jù)表"person","person"表的數(shù)據(jù)結(jié)構(gòu)如下表:

字段名稱字段類型字段意思

id數(shù)字序號

xm文本姓名

<xb文本性別

nl文本年齡

zip文本郵政編碼

遠(yuǎn)程數(shù)據(jù)庫SqlServer2000的數(shù)據(jù)庫服務(wù)器名稱為"Server1",數(shù)據(jù)庫名稱為"Data1",登陸的ID為"sa",口令為空,在數(shù)據(jù)庫也定義了一張"person"表,數(shù)據(jù)結(jié)構(gòu)如上表。

二.精通C#數(shù)據(jù)庫編程之如何瀏覽數(shù)據(jù):

在《VisualC#的數(shù)據(jù)綁定》中,已經(jīng)了解了如何把數(shù)據(jù)集中的某些字段綁定到WinForm組件的某個屬性上,這樣程序員就可以根據(jù)以WinForm組件的來定制數(shù)據(jù)顯示的形式,并且此時的WinForm組件顯示內(nèi)容就可以隨著記錄指針的變化而改變。至此可見,瀏覽數(shù)據(jù)記錄的關(guān)鍵就是如何改變記錄指針。要實現(xiàn)這種操作,就要使用到BindingManagerBase類,此類的主要作用是管理對于那些實現(xiàn)了對同一個數(shù)據(jù)源進(jìn)行綁定的對象。說的具體些,就是能夠使得Windows窗體上的已經(jīng)對同一數(shù)據(jù)源進(jìn)行數(shù)據(jù)綁定的組件保持同步。在BindingManagerBase類中定義了一個屬性"Position",通過這個屬性就可以改變BindingManagerBase對象中的數(shù)據(jù)指針。創(chuàng)建BindingManagerBase對象必須要使用到BindingContext類,其實每一個由Control類中繼承而得到的對象,都有單一的BindingContext對象,在大多數(shù)創(chuàng)建窗體中實現(xiàn)數(shù)據(jù)綁定組件的BindingManagerBase對象是使用Form類的BindingContext來得到。下列代碼是以Access2000數(shù)據(jù)庫為模型,創(chuàng)建的一個名稱為"myBind"的BindingManagerBase對象。

  1. //創(chuàng)建一個OleDbConnectionstringstrCon="Provider=Microsoft.Jet.OLEDB.4.0;
  2. DataSource=db.mdb";OleDbConnectionmyConn=newOleDbConnection(strCon);
  3. stringstrCom="SELECT*FROMperson";file://創(chuàng)建一個DataSetmyDataSet=newDataSet();
  4. myConn.Open();file://用OleDbDataAdapter得到一個數(shù)據(jù)集OleDbDataAdaptermyCommand
  5. =newOleDbDataAdapter(strCom,myConn);file:
  6. //把Dataset綁定books數(shù)據(jù)表myCommand.Fill(myDataSet,"person");file:
  7. //關(guān)閉此OleDbConnectionmyConn.Close();myBind=this.BindingContext[myDataSet,"person"]; 

下列代碼是以SqlServer2000數(shù)據(jù)庫為模型,創(chuàng)建一個名稱為"myBind"的BindingManagerBase對象。

  1. //設(shè)定數(shù)據(jù)連接字符串,此字符串的意思是打開Sqlserver數(shù)據(jù)庫,//服務(wù)器名稱為server1,數(shù)據(jù)庫為data1stringstrCon="Provider=SQLOLEDB.1;PersistSecurityInfo=False;  
  2. UserID=sa;InitialCatalog=data1;DataSource=server1";  
  3. OleDbConnectionmyConn=newOleDbConnection(strCon);myConn.Open();stringstrCom="SELECT*FROMperson";  
  4. file://創(chuàng)建一個DataSetmyDataSet=newDataSet();  
  5. file://用OleDbDataAdapter得到一個數(shù)據(jù)集OleDbDataAdaptermyCommand=newOleDbDataAdapter(strCom,myConn);  
  6. file://把Dataset綁定person數(shù)據(jù)表myCommand.Fill(myDataSet,"person");  
  7. file://關(guān)閉此OleDbConnectionmyConn.Close();  
  8. myBind=this.BindingContext[myDataSet,"person"]; 

得到了是同一數(shù)據(jù)源的BindingManagerBase對象,通過改變此對象的"Position"屬性值,這樣綁定數(shù)據(jù)的組件顯示的數(shù)據(jù)就隨之變化,從而實現(xiàn)導(dǎo)航數(shù)據(jù)記錄。

<I>.導(dǎo)航按鈕"上一條"實現(xiàn)方法:

  1. protectedvoidGoPrevious(objectsender,System.EventArgse)  
  2. {if(myBind.Position==0)MessageBox.Show("已經(jīng)到了第一條記錄!","信息提示!",  
  3. MessageBoxButtons.OK,MessageBoxIcon.Information);elsemyBind.Position-=1;}  
  4. <  

<II>.導(dǎo)航按鈕"下一條"實現(xiàn)方法:

  1. protectedvoidGoNext(objectsender,System.EventArgse)  
  2. {if(myBind.Position==myBind.Count-1)MessageBox.Show("已經(jīng)到了最后一條記錄!","信息提示!",  
  3. MessageBoxButtons.OK,MessageBoxIcon.Information);elsemyBind.Position+=1;} 

<III>.導(dǎo)航按鈕"至尾"實現(xiàn)方法:

  1. protectedvoidGoLast(objectsender,System.EventArgse){myBind.Position=myBind.Count-1;}  
  2. <IV>.導(dǎo)航按鈕"至首"實現(xiàn)方法:protectedvoidGoFirst(objectsender,System.EventArgse){myBind.Position=0;} 

注釋:"Count"是BindingManagerBase對象的另外一個重要的屬性,是數(shù)據(jù)集記錄的總數(shù)。

三.精通C#數(shù)據(jù)庫編程之實現(xiàn)刪除記錄:

在對數(shù)據(jù)記錄進(jìn)行操作的時候,有二點必須十分清晰:

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

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

  1. protectedvoidDelete_record(objectsender,System.EventArgse){DialogResultr=MessageBox.Show("是否刪除當(dāng)前記錄!","刪除當(dāng)前記錄!",  
  2. MessageBoxButtons.YesNo,MessageBoxIcon.Question);  
  3. intss=(int)r;  
  4. if(ss==6)//按動"確定"按鈕{try{file://連接到一個數(shù)據(jù)庫stringstrCon="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=db.mdb";  
  5. OleDbConnectionmyConn=newOleDbConnection(strCon);  
  6. myConn.Open();stringstrDele="DELETEFROMpersonWHEREid="+t_id.Text;OleDbCommandmyCommand=newOleDbCommand(strDele,myConn);  
  7. file://從數(shù)據(jù)庫中刪除指定記錄myCommand.ExecuteNonQuery();  
  8. file://從DataSet中刪除指定記錄myDataSet.Tables["person"].Rows[myBind.Position].Delete();myDataSet.Tables["person"].AcceptChanges();myConn.Close();}  
  9. catch(Exceptioned){MessageBox.Show("刪除記錄錯誤信息:"+ed.ToString(),"錯誤!");}}} 

四.精通C#數(shù)據(jù)庫編程之插入數(shù)據(jù)記錄:

對數(shù)據(jù)庫進(jìn)行插入記錄操作和刪除記錄操作基本的思路是一致的,就是通過ADO.NET首先插入數(shù)據(jù)記錄到數(shù)據(jù)庫,然后對"DataSet"對象進(jìn)行必要的修改。下列代碼就是以Access2000數(shù)據(jù)庫為模型修改當(dāng)前記錄的代碼:

<

  1. protectedvoidUpdate_record(objectsender,System.EventArgse){inti=myBind.Position;try{file://連接到一個數(shù)據(jù)庫stringstrCon="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=db.mdb";  
  2. OleDbConnectionmyConn=newOleDbConnection(strCon);myConn.Open();myDataSet.Tables["person"].Rows[myBind.Position].BeginEdit();  
  3. file://從數(shù)據(jù)庫中修改指定記錄stringstrUpdt="UPDATEpersonSETxm='"+t_xm.Text+"',xb='"+t_xb.Text+"',nl="+t_nl.Text+",zip="+t_books.Text+"WHEREid="+t_id.Text;OleDbCommandmyCommand=newOleDbCommand(strUpdt,myConn);myCommand.ExecuteNonQuery();  
  4. myDataSet.Tables["person"].Rows[myBind.Position].EndEdit();myDataSet.Tables["person"].AcceptChanges();myConn.Close();}  
  5. catch(Exceptioned){MessageBox.Show("修改指定記錄錯誤:"+ed.ToString(),"錯誤!");}myBind.Position=i;} 

由于對SqlServer2000數(shù)據(jù)記錄修改操作和Access2000數(shù)據(jù)記錄修改操作的差異只在于不同的數(shù)據(jù)鏈接,具體的代碼可以參考"刪除數(shù)據(jù)記錄"中的代碼,在這里就不提供了。

五.精通C#數(shù)據(jù)庫編程之插入數(shù)據(jù)記錄

和前面二種操作在思路是一致的,就是通過ADO.NET首先插入數(shù)據(jù)記錄到數(shù)據(jù)庫,然后對"DataSet"對象進(jìn)行必要的修改。下列代碼就是以Access2000數(shù)據(jù)庫為模型插入一條數(shù)據(jù)記錄的代碼

  1. protectedvoidInsert_record(objectsender,System.EventArgse){try{file://判斷所有字段是否添完,添完則執(zhí)行,反之彈出提示  
  2. if(t_id.Text!=""&&t_xm.Text!=""&&t_xb.Text!=""&&t_nl.Text!=""&&t_books.Text!="")  
  3. {stringmyConn1="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=db.mdb";OleDbConnectionmyConn  
  4. =newOleDbConnection(myConn1);  
  5. myConn.Open();stringstrInsert="INSERTINTOperson(id,xm,xb,nl,zip)VALUES(";strInsert+=t_id.Text+",'";strInsert+=t_xm.Text+"','";strInsert+=t_xb.Text+"',";  
  6. strInsert+=t_nl.Text+",";strInsert+=t_books.Text+")";OleDbCommandinst=newOleDbCommand(strInsert,myConn);  
  7. inst.ExecuteNonQuery();myConn.Close();myDataSet.Tables["person"].Rows[myBind.Position].BeginEdit();  
  8. myDataSet.Tables["person"].Rows[myBind.Position].EndEdit();myDataSet.Tables["person"].AcceptChanges();}  
  9. else 
  10. {MessageBox.Show("必須填滿所有字段值!","錯誤!");}}  
  11. catch(Exceptioned){MessageBox.Show("保存數(shù)據(jù)記錄發(fā)生"+ed.ToString(),"錯誤!");}} 

同樣對SqlServer2000數(shù)據(jù)庫進(jìn)行插入記錄操作和Access2000數(shù)據(jù)庫插入記錄操作的差異也只在于不同的數(shù)據(jù)鏈接,具體的代碼可以參考"刪除數(shù)據(jù)記錄"中的代碼,在這里就不提供了。

六.VisualC#數(shù)據(jù)庫編程的完成源代碼和程序運(yùn)行的主界面:

掌握了上面要點,編寫一個完整的數(shù)據(jù)庫編程的程序就顯得非常容易了,下面是VisualC#進(jìn)行數(shù)據(jù)庫編程的完整代碼(Data01.cs),此代碼是以Access2000數(shù)據(jù)庫為模型設(shè)計的,具體如下:

  1. using System ;  
  2.  using System.Drawing ;  
  3.  using System.ComponentModel ;  
  4.  using System.Windows.Forms ;  
  5.  using System.Data.OleDb ;  
  6.  using System.Data ;   
  7.  public class Data : Form  
  8.  {  
  9.  private System.ComponentModel.Container components = null ;  
  10.  private Button lastrec ;  
  11.  private Button nextrec ;  
  12.  private Button previousrec ;  
  13.  private Button firstrec ;  
  14.  private TextBox t_books ;  
  15.  private TextBox t_nl ;  
  16.  private ComboBox t_xb ;  
  17.  private TextBox t_xm ;  
  18.  private TextBox t_id ;  
  19.  private Label l_books ;  
  20.  private Label l_nl ;  
  21.  private Label l_xb ;  
  22.  private Label l_xm ;  
  23.  private Label l_id ;  
  24.  private Label label1 ;  
  25.  private DataSet myDataSet ;  
  26.  private Button button1 ;  
  27.  private Button button2 ;  
  28.  private Button button3 ;  
  29.  private Button button4 ;  
  30.  private BindingManagerBase myBind ;  
  31.  public Data ( )  
  32.  {  
  33.  file://連接到一個數(shù)據(jù)庫  
  34.  GetConnected ( ) ;  
  35.  // 對窗體中所需要的內(nèi)容進(jìn)行初始化  
  36.  InitializeComponent ( ) ;  
  37.  }  
  38.  file://清除在程序中使用過的資源  
  39.  protected override void Dispose( bool disposing )  
  40.  {  
  41.  if( disposing )  
  42.  {  
  43.  if ( components != null )  
  44.  {  
  45.  components.Dispose ( ) ;  
  46.  }  
  47.  }  
  48.  base.Dispose( disposing ) ;  
  49.  }  
  50.  public static void Main ( )  
  51.  {  
  52.  Application.Run ( new Data ( ) ) ;  
  53.  }  
  54.  public void GetConnected ( )  
  55.  {  
  56.  try 
  57.  {  
  58.  file://創(chuàng)建一個 OleDbConnection  
  59.  string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;  
  60.  OleDbConnection myConn = new OleDbConnection ( strCon ) ;  
  61.  string strCom = " SELECT * FROM person " ;  
  62.  file://創(chuàng)建一個 DataSet  
  63.  myDataSet = new DataSet ( ) ;  
  64.  myConn.Open ( ) ;  
  65.  file://用 OleDbDataAdapter 得到一個數(shù)據(jù)集  
  66.  OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;  
  67.  file://把Dataset綁定books數(shù)據(jù)表  
  68.  myCommand.Fill ( myDataSet , "person" ) ;  
  69.  file://關(guān)閉此OleDbConnection  
  70.  myConn.Close ( ) ;  
  71.  }  
  72.  catch ( Exception e )  
  73.  {  
  74.  MessageBox.Show ( "連接錯誤! " + e.ToString ( ) , "錯誤" ) ;   
  75.  }  
  76.  }  
  77.      private void InitializeComponent ( )  
  78.      {  
  79.      file://添加控件,略  
  80.      this.Name = "Data" ;  
  81.      this.Text = "Visual C#的數(shù)據(jù)庫編程!" ;  
  82.      this.ResumeLayout(false) ;  
  83.      myBind = this.BindingContext [ myDataSet , "person" ] ;  
  84.      }  
  85.      protected void New_record ( object sender , System.EventArgs e )  
  86.      {  
  87.  
  88.      t_id.Text = ( myBind.Count + 1 ).ToString ( ) ;  
  89.      t_xm.Text = "" ;  
  90.      t_xb.Text = "" ;  
  91.      t_nl.Text = "" ;  
  92.      t_books.Text = "" ;  
  93.      }  
  94.      protected void Insert_record ( object sender , System.EventArgs e )  
  95.      {  
  96.      try 
  97.      {  
  98.      file://判斷所有字段是否添完,添完則執(zhí)行,反之彈出提示   
  99.      if ( t_id.Text != "" && t_xm.Text != "" && t_xb.Text != "" &&  
  100.      t_nl.Text != "" && t_books.Text != "" )  
  101.      {  
  102.      string myConn1 = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;  
  103.      OleDbConnection myConn = new OleDbConnection ( myConn1 ) ;  
  104.      myConn.Open ( ) ;  
  105.      string strInsert = " INSERT INTO person ( id , xm , xb , nl , zip ) VALUES ( " ;  
  106.      strInsert += t_id.Text + ", '" ;  
  107.      strInsert += t_xm.Text + "', '" ;  
  108.      strInsert += t_xb.Text + "', " ;  
  109.      strInsert += t_nl.Text + ", " ;  
  110.      strInsert += t_books.Text + ")" ;  
  111.      OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ;  
  112.      inst.ExecuteNonQuery ( ) ;  
  113.      myConn.Close ( ) ;  
  114.      myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . BeginEdit ( ) ;  
  115.      myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . EndEdit ( ) ;  
  116.      myDataSet.Tables [ "person" ] . AcceptChanges ( ) ;  
  117.      }  
  118.      else 
  119.      {  
  120.      MessageBox.Show ( "必須填滿所有字段值!" , "錯誤!" ) ;  
  121.      }  
  122.      }  
  123.      catch ( Exception ed )  
  124.      {  
  125.      MessageBox.Show ( "保存數(shù)據(jù)記錄發(fā)生 " + ed.ToString ( ) , "錯誤!" ) ;  
  126.      }       
  127.      }  
  128.      protected void Update_record ( object sender , System.EventArgs e )  
  129.      {  
  130.      int i = myBind.Position ;  
  131.      try{  
  132.      file://連接到一個數(shù)據(jù)庫  
  133.      string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb " ;  
  134.      OleDbConnection myConn = new OleDbConnection ( strCon ) ;  
  135.      myConn.Open ( ) ;  
  136.      myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . BeginEdit ( ) ;  
  137.      file://從數(shù)據(jù)庫中修改指定記錄  
  138.      string strUpdt = " UPDATE person SET xm = '" 
  139.      + t_xm.Text + "' , xb = '" 
  140.      + t_xb.Text + "' , nl = " 
  141.      + t_nl.Text + " , zip = " 
  142.      + t_books.Text + " WHERE id = " + t_id.Text ;  
  143.      OleDbCommand myCommand = new OleDbCommand ( strUpdt , myConn ) ;  
  144.      myCommand.ExecuteNonQuery ( ) ;   
  145.      myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . EndEdit ( ) ;  
  146.      myDataSet.Tables [ "person" ] . AcceptChanges ( ) ;  
  147.      myConn.Close ( ) ;  
  148.      }  
  149.      catch ( Exception ed )  
  150.      {  
  151.      MessageBox.Show ( "修改指定記錄錯誤: " + ed.ToString ( ) , "錯誤!" ) ;   
  152.      }  
  153.      myBind.Position = i ;  
  154.      }  
  155.  
  156.      protected void Delete_record ( object sender , System.EventArgs e )  
  157.      {  
  158.      DialogResult r = MessageBox.Show ( "是否刪除當(dāng)前記錄!" , "刪除當(dāng)前記錄!" ,   
  159.               MessageBoxButtons.YesNo, MessageBoxIcon.Question ) ;  
  160.      int ss = ( int ) r ;  
  161.      if ( ss == 6 ) // 按動"確定"按鈕  
  162.      {  
  163.      try{  
  164.      file://連接到一個數(shù)據(jù)庫  
  165.      string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb " ;  
  166.      OleDbConnection myConn = new OleDbConnection ( strCon ) ;  
  167.      myConn.Open ( ) ;  
  168.      string strDele = "DELETE FROM person WHERE id= " + t_id.Text ;  
  169.      OleDbCommand myCommand = new OleDbCommand ( strDele , myConn ) ;  
  170.      file://從數(shù)據(jù)庫中刪除指定記錄  
  171.      myCommand.ExecuteNonQuery ( ) ;  
  172.      file://從DataSet中刪除指定記錄  
  173.      myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . Delete ( ) ;  
  174.      myDataSet.Tables [ "person" ] . AcceptChanges ( ) ;  
  175.      myConn.Close ( ) ;  
  176.      }  
  177.      catch ( Exception ed )  
  178.      {  
  179.      MessageBox.Show ( "刪除記錄錯誤信息: " + ed.ToString ( ) , "錯誤!" ) ;  
  180.      }  
  181.      }  
  182.      }  
  183.      file://按鈕"尾記錄"對象事件程序   
  184.      protected void GoLast ( object sender , System.EventArgs e )  
  185.      {  
  186.      myBind.Position = myBind.Count - 1 ;  
  187.      }  
  188.      file://按鈕"下一條"對象事件程序  
  189.      protected void GoNext ( object sender , System.EventArgs e )  
  190.      {  
  191.      if ( myBind.Position == myBind.Count -1 )  
  192.      MessageBox.Show ( "已經(jīng)到了最后一條記錄!""信息提示!" ,  
  193.                     MessageBoxButtons.OK , MessageBoxIcon.Information  
  194.      ) ;  
  195.      else 
  196.      myBind.Position += 1 ;  
  197.      }  
  198.      file://按鈕"上一條"對象事件程序   
  199.      protected void GoPrevious ( object sender , System.EventArgs e )  
  200.      {  
  201.      if ( myBind.Position == 0 )  
  202.      MessageBox.Show ( "已經(jīng)到了第一條記錄!" , "信息提示!" ,  
  203.               MessageBoxButtons.OK , MessageBoxIcon.Information  
  204.      ) ;  
  205.      else 
  206.      myBind.Position -= 1 ;  
  207.      }  
  208.      file://按鈕"首記錄"對象事件程序  
  209.      protected void GoFirst ( object sender , System.EventArgs e )  
  210.      {  
  211.      myBind.Position = 0 ;  
  212.      }  
  213.      } 

對于以SqlServer2000數(shù)據(jù)庫為模型的程序代碼,只要把Data01.cs中的數(shù)據(jù)鏈接,即:

stringmyConn1="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=db.mdb";

改換成:

stringstrCon="Provider=SQLOLEDB.1;PersistSecurityInfo=False;UserID=sa;InitialCatalog=data1;DataSource=server1";

注釋:此數(shù)據(jù)鏈接代表的意思是:打開Sqlserver數(shù)據(jù)庫,服務(wù)器名稱為server1,數(shù)據(jù)庫為data1就可以得到VisualC#針對SqlServer2000數(shù)據(jù)庫為模板編程的完成源程序代碼了。所以本文就不再提供了。

七.精通C#數(shù)據(jù)庫編程總結(jié):

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

【編輯推薦】

  1. 淺析C#正則表達(dá)式
  2. 實現(xiàn)DB2功能的C#數(shù)據(jù)庫編程實例
  3. C#語言操縱數(shù)據(jù)庫事務(wù)
  4. 概述C#語言異常處理
  5. 簡單介紹C#數(shù)組和函數(shù)
責(zé)任編輯:彭凡 來源: CSDN
相關(guān)推薦

2009-08-07 16:19:00

C#下數(shù)據(jù)庫編程

2009-08-07 16:19:00

C#下數(shù)據(jù)庫編程

2009-08-07 18:07:58

C#數(shù)據(jù)庫開發(fā)

2009-08-07 15:26:38

C#數(shù)據(jù)庫編程實例

2009-08-19 16:30:55

C#操作Access數(shù)

2009-12-24 09:16:11

C#泛型

2009-08-25 16:36:16

C#進(jìn)行數(shù)據(jù)庫編程

2009-08-25 15:50:13

C#連接遠(yuǎn)程數(shù)據(jù)庫

2011-03-03 09:45:25

DB2數(shù)據(jù)庫Visual C#

2009-08-07 17:04:41

C#數(shù)據(jù)庫

2009-08-14 13:52:18

C#判斷數(shù)據(jù)類型

2009-08-14 16:08:34

讀寫B(tài)inaryC#編程實例

2009-08-31 18:17:32

C#接口編程

2009-08-25 15:35:20

C#連接Oracle數(shù)

2010-04-23 09:32:39

Oracle數(shù)據(jù)庫實例

2010-09-13 09:03:49

Access數(shù)據(jù)庫

2024-02-28 08:06:17

2024-04-18 09:56:16

2009-08-11 13:35:13

C# Berkeley

2009-08-07 15:33:11

C#數(shù)據(jù)庫編程
點贊
收藏

51CTO技術(shù)棧公眾號