乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      VB6.0操作SQL Server——增刪改查

       昵稱32057794 2016-11-13

      http://www.cnblogs.com/Miss-Lin/archive/2012/08/13/2635848.html

       

      一、數(shù)據(jù)錄入

      通過VBSQL Server數(shù)據(jù)庫中錄入數(shù)據(jù),可以使用數(shù)據(jù)綁定控件錄入數(shù)據(jù)與使用SQL語句錄入

      1.利用數(shù)據(jù)綁定控件錄入數(shù)據(jù)

      使用數(shù)據(jù)綁定控件錄入數(shù)據(jù)可以運行較少的代碼,實現(xiàn)向數(shù)據(jù)庫中錄入數(shù)據(jù),數(shù)據(jù)綁定后,由于數(shù)據(jù)綁定控件已經(jīng)與數(shù)據(jù)表相連接,所以只需通過ADO控件的AddNew方法添加一條新的空白記錄,再通過Update方法保存當(dāng)前的記錄,即可完成向數(shù)據(jù)庫中錄入數(shù)據(jù)。

      例:向數(shù)據(jù)庫中添加一條記錄

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      Private Sub cmdAdd_click()
        
            Adodc1.Recordset.AddNew
        
      End Sub
        
      Private Sub cmdSave_click()
        
           Dim I as Long
        
           for I =0 to 6
        
                Adodc.Recordset.Fields(i).value=Text(i+1).text
        
           Next I
        
           Adodc1.Recoedset.Update
        
           MsgBox'添加信息成功!'
        
      End Sub

        

      2.使用Insert語句錄入數(shù)據(jù)

         使用SQL語句錄入數(shù)據(jù),通過connection對象的execute方法執(zhí)行SQL語句,語法如下:

          connection.execute 字符串,記錄數(shù),字符串參數(shù)

      例:向student數(shù)據(jù)庫中添加一條學(xué)生信息

      Dim cnn as ADODB.conection
        
      Dim rst as ADODB.recordset
        
      Private sub Form_Load()
        
          set cnn=New ADODB.connection
        
          set rst=new ADODB.recordset
        
          cnn.open'provider=SQLOLEDB;Persist security Info=True;User ID=sa;Initial Catalog=student;Data Source=.'
        
          rst.open'select * from student_info',cnn
        
          set MSHFlexGrid.DataSource=rst
        
      End Sub
        
      Private sub cmdAdd_click()
        
         cnn.execute'insert into student_info(txtSID,txtName,txtSex;txtBornData,txtRuDate,txtClassNo)' & _
        
                'value('' & txtSID & '','' & txtName & '','' & txtSex & '','' & txtBornDate & '','' & txtRuDate & '','' & txtClassNo & '')'
        
      End Sub

        

      二、數(shù)據(jù)刪除

      通過VB刪除數(shù)據(jù)時,不僅可以使用SQL語句中的Delete語句來實現(xiàn),也可以使用ADO控件Delete方法來刪除數(shù)據(jù)。

      1.使用ADO控件Delete方法刪除數(shù)據(jù)

      使用ADO控件Delete方法刪除數(shù)據(jù)時,被刪除的數(shù)據(jù)信息表面上是被刪除了,實質(zhì)上數(shù)據(jù)還存在與內(nèi)存中,這時還需要使用ADO控件的Update方法才能夠?qū)?shù)據(jù)徹底地刪除

      例:刪除表中的指定記錄:

      Adodc1.Recordset.Delete

      Adodc1.Recordset.Update

      例:通過ADO控件Delete方法刪除表中指定的數(shù)據(jù):

      Private Sub Form_Load()
        
        Adodc1.ConnectionString='Provider=SQLOLEDB;Persist Security Info=True;User ID=sa;Initail Catalog=student;Data Source=.'
        
        Adodc1.Commandtype=adCmdText
        
        Adodc.Recordsource='select * from student_info'
        
        Set DataGrid1.DataSource=Adodc1
        
      End Sub
        
      Private Sub Command1_Click()
        
        Adodc1.Recordset.Delete
        
        If Msgbox('確認(rèn)刪除指定數(shù)據(jù)嗎?',vbYesNo,'提示')=vbYes then
        
           Adodc1.Recordset.Refresh
        
        End If
        
      End Sub

        

      2.執(zhí)行Delete語句進(jìn)行數(shù)據(jù)刪除

      使用Delete語句刪除數(shù)據(jù),不僅可以刪除指定的一條或多條語句,也可以將數(shù)據(jù)表中的數(shù)據(jù)全部刪除

      例:在VB中執(zhí)行Delete語句刪除表student中的全部數(shù)據(jù)

      Cnn.Execute'delete from student'

      可以在Delete語句后使用where來限定條件,將條件符合的數(shù)據(jù)刪除

      例:刪除表student中年齡大于25歲的數(shù)據(jù)

      Cnn.Execute 'delete from student where 年齡>'25''

        

      3.刪除列中的數(shù)據(jù)

      無論是通過ADO控件Delete方法還是delete語句刪除數(shù)據(jù),都是將一行或多行數(shù)據(jù)全部刪除,如果表中一些行的數(shù)據(jù)需要保留某些列中的信息,那么使用ADO控件Delete方法或Delete語句都是比較麻煩的。

      對于刪除表中的數(shù)據(jù),可以采取數(shù)據(jù)的方式實現(xiàn),即將刪除列中的數(shù)據(jù)直接更新為Null

      例:通過ADO控件Update方法刪除表中列的數(shù)據(jù)

      Private Sub Command1_Click()
        
        Adodc1.Recordset.Update 1,Nul
        
      End Sub

        

       

      三、數(shù)據(jù)修改

      VB中修改數(shù)據(jù),不僅僅可以采用執(zhí)行SQL語句實現(xiàn),也可以通過數(shù)據(jù)綁定控件進(jìn)行數(shù)據(jù)的修改。數(shù)據(jù)綁定控件進(jìn)行數(shù)據(jù)的修改采用的代碼較少,特別是一些表格類數(shù)據(jù)綁定控件,不需要代碼即可對數(shù)據(jù)進(jìn)行修改,如DataGrid控件。

      1.使用Update語句修改數(shù)據(jù)

      Update語句通過ADO對象的Execute方法執(zhí)行,達(dá)到對數(shù)據(jù)表中的數(shù)據(jù)進(jìn)行修改

      Cnn.Execute'Update 數(shù)據(jù)表名稱 set 字段1=表達(dá)式1,字段2=表達(dá)式2……where子句'

      Update語句中可以使用where子句,還有符合條件的數(shù)據(jù)可執(zhí)行修改操作,如果使用where指定條件,則Update語句會修改表中的全部數(shù)據(jù)

      通常都是在批量更改時采用該語句,這種方法要比ADOUpdate方法逐條地修改數(shù)據(jù)方便,但VB中一些表格控件可以不需要代碼而直觀實現(xiàn)數(shù)據(jù)的批量修改

      2.表格數(shù)據(jù)綁定控件修改數(shù)據(jù)

      VB中采用表格數(shù)據(jù)綁定修改控件修改數(shù)據(jù),基本上不需要代碼,只需要設(shè)置表格數(shù)據(jù)綁定控件的一些屬性即可實現(xiàn),例如DataGrid控件,將其綁定到相應(yīng)的數(shù)據(jù)源上,再設(shè)置其AllowUpdate屬性為True,即可實現(xiàn)表格中修改的數(shù)據(jù)及時更新到數(shù)據(jù)庫中

      該屬性可返回或設(shè)置一個值,指示用戶能否修改DataGrid控件中的數(shù)據(jù),語法:

      DataGrid.AllowUpdate=布爾表達(dá)式

      例:使用DataGrid控件修改數(shù)據(jù)

      Private Sub Form_Load()
        
         '設(shè)置連接字符串
        
       Adodc1.ConnectionString='Provider=SQLOLEDB;Persist Security Info=True;User ID=sa;Initial Catalog=student;Data Source=.'
        
        '設(shè)定數(shù)據(jù)源
        
      Adodc1.CommandType=adcmdText
        
      Adodc1.RecordSource='select * from student_Info order by student_ID'
        
        '為DataGrid控件綁定數(shù)據(jù)源
        
      Set DataGrid1.DataSouce=Adodc1
        
        '允許DataGrid1控件更新表中的數(shù)據(jù)
        
      DataGrid1.AllowUpdate=True
        
      End Sub

        

      3.使用文本數(shù)據(jù)綁定控件修改數(shù)據(jù)

      DataGrid控件不同,文本類控件綁定數(shù)據(jù)后在修改數(shù)據(jù)時,由于所修改的數(shù)據(jù)只是寫入在緩存當(dāng)中,所以需要通過ADOUpdate方法提交修改,另外,ADO還提供CancelUpdate方法,用此方法來撤銷在使用Update方法前對數(shù)據(jù)所作的修改,在程序中使用CancelUpdate方法的代碼如下:Adodc1.Recordset.CancelUpdate

      例:使用文本控件綁定數(shù)據(jù)并進(jìn)行修改數(shù)據(jù)

      '設(shè)置連接字符串與數(shù)據(jù)源,并綁定數(shù)據(jù)源
        
      Private Sub Form_Load()
        
       Adodc1.ConnectiongString='Provider=SQLOLEDB;Persist Security Info=True;User ID=sa;Initial Catalog=student;Data Source=.'
        
      Adodc1.RecordSource='select * from Course_Info'
        
      Set Text1.DataSource=Adodc1
        
      Text1.DataField='課程名稱'
        
      End Sub
        
      '提交保存數(shù)據(jù)
        
      Private Sub Commad1_Click()
        
       Adodc1.Recordset.Update
        
       MsgBox'保存成功!',vbOKOnly,'提示'
        
      End Sub
        
        '撤銷數(shù)據(jù)修改
        
      Private Sub Command2_Click()
        
       Adodc1.Recordset.CancelUpdate
        
      End Sub

        

       

      四、數(shù)據(jù)查詢

      VB中查詢數(shù)據(jù)庫中的數(shù)據(jù)不僅可以通過執(zhí)行SQL語句實現(xiàn),也可以通過控件或?qū)Γㄈ?/span>ADO)限定檢索數(shù)據(jù)的條件來實現(xiàn)。

      1.簡單查詢

      簡單查詢可以通過Select語句限定數(shù)據(jù)源來實現(xiàn)。在Select語句中使用關(guān)鍵字、子句等實現(xiàn)。

      下面對幾種常用的簡單查詢進(jìn)行介紹:

      1)使用AS改變查詢數(shù)據(jù)的列名

      多數(shù)情況下,在SQL Server中創(chuàng)建表的字段都是以英文命名的,這些字段名在顯示時給一般用戶帶來不便,那么可以在查詢中采用AS來改變字段在顯示時的名稱。

      例:使用AS改變查詢數(shù)據(jù)的列名

      Private Sub cmdModify_Click()
        
           Adodc.RecordSource='select ID as 編號,Mailbox as 電子郵箱 from Mail_Info'
        
           Adodc.Refresh
        
      End Sub

        

       

      2)計算查詢的結(jié)果

      VB編寫的程序中,往往需要將數(shù)據(jù)庫中的數(shù)據(jù)進(jìn)行計算,以便獲得所需的數(shù)值。

      通過在查詢語句中使用算術(shù)運算符,在配合AS為計算出的字段設(shè)置名稱,即可在查詢結(jié)果中顯示計算得到的數(shù)據(jù)。

      例:計算查詢的結(jié)果

      Private Sub cmdAcount_Click()
        
          Adodc.RecordSource='select 卡號,每小時上機費用,上機時間(每小時上機費用*上機時間)as 收費金額 from 學(xué)生上機收費金額表'
        
          Adodc.Refresh
        
      End Sub

        

      3)比較條件查詢

      在查詢語句中可以包含比較運算,常用的比較運算符有=、、><>、!>、!、>=、<>!=。使用這些運算符連接表達(dá)式形成一個比較條件,系統(tǒng)將根絕該查詢條件返回的布爾值來判斷數(shù)據(jù)是否滿足該查詢條件,只有滿足條件的數(shù)據(jù)才會出現(xiàn)在查詢的結(jié)果集中。

      這些比較條件在查詢語句中需要與where子句連用,但是VB提供Filter屬性為Recordset中的數(shù)據(jù)指定篩選條件。該屬性根據(jù)條件可選擇性地屏蔽Recordset對象中的記錄,該屬性語法如下:Recordset.Filter=字符串

      例:通過Filter屬性實現(xiàn)比較條件查詢

      '設(shè)置連接、數(shù)據(jù)源與初始化程序
        
      Private Sub Form_Load()
        
           Adodc.connectionString='provider=SQLOLEDB;Data Source=.;Initail CataLog=student;UID=sa;PWD=123456'
        
           Adodc.Recordset='select stuID as 學(xué)號,stuname as 姓名,stuage as 年齡 from student_info'
        
           set DataGrid.DataSource=Adodc
        
           Combo1.AddItem'學(xué)號'
        
           Combo1.AddItem'姓名'
        
           Combo1.AddItem'年齡'
        
           Combo2.AddItem'>'
        
           Combo2.AddItem'<>
        
           Combo2.AddItem'>='
        
           Combo2.AddItem'<>
        
           Combo2.AddItem'='
        
      End Sub
        
         
        
      '查詢信息以及錯誤處
        
      Private Sub cmdQuery_Click()
        
            On Error Resume Next
        
            If Combo1.text<>'' and Combo2.text<>'' then
        
                   Adodc.Recordset.Filter=Adodc.Recordset.Fields(Combo1.ListIndex).Name & Combo2.Text & '' & Trim(txtQuery.text) & ''
        
            End If
        
            If Err then Msgbox'請輸入正確信息!',vbOKOnly,'警告'
        
      End Sub

        

       

       

      2.模糊查詢

      模糊查詢是通過在VB中查詢與所給查詢內(nèi)容相似的信息。

      實現(xiàn)模糊查詢可以通過執(zhí)行SQL語句中的Like語句實現(xiàn)。Like語句用來確定給定的數(shù)據(jù)信息是否與指定的模式匹配。(模式可以包含常規(guī)字符和通配符字符。)Like也可以在ADOFilter屬性中使用。

      下面介紹在Filter屬性中使用Like與通配符的幾種方法:

       

       

      3.日期和時間查詢

      對日期和時間的數(shù)據(jù)查詢,可以通過VB中的ADOFilter屬性限定篩選條件,或執(zhí)行查詢SQL語句實現(xiàn)。

      1)查詢指定日期時間的數(shù)據(jù)

      SQL語句中,可以使用運算符(>=、Like等)查詢?nèi)掌?/span>/時間類型的數(shù)據(jù)。日期時間字符串的書寫要符合一定的格式,例如“2012-08-13 0:00:01”。

      例:查詢指定日期時間的數(shù)據(jù)

      Private Sub cmdQuery_Click()
        
             On Error Resume Next
        
             Adodc.Recordset.Filter='出生日期='' & Trim(txtYear.Text) & '-' & Trim(txtMonth.Text) & '-' & Trim(txtDay.Text) & '0:00:00''
        
             If Err then Msgbox'請輸入正確信息!',vbOKOnly,'錯誤'
        
      End sub
        
         
        
      Private Sub Form_Load()
        
             Adodc.ConnectonString='Provider=SQLOLEDB;Data Source=.;Initail Catalog=student;UID=sa;PWD=123456'
        
             Adodc.RecordSource='select stuID as 學(xué)號,stuName as 姓名,stuAge as 年齡,stuBornDate as 出生日期 from Student_Info'
        
             set DataGrid.DataSource=Adodc
        
      End Sub

        

       

      2)分別按年、月、日等查詢數(shù)據(jù)

      在設(shè)計數(shù)據(jù)庫系統(tǒng)應(yīng)用程序時,往往需要查詢某一年、某個月或某一天的數(shù)據(jù)。如果采用指定日期時間的方式來查詢,雖然可以實現(xiàn),但是過程是很麻煩的。這時可以采用YearMonth、Day等函數(shù),對數(shù)據(jù)庫中日期時間數(shù)據(jù)提取年、月、日等相關(guān)信息;然后結(jié)合指定日期時間的查詢方式,即可方便地查詢到某一年、某個月或者某一天的數(shù)據(jù)。

      例:按年、月、日等查詢數(shù)據(jù)

      Private Sub cmdQuery_Click()
        
          Dim stc as string
        
          On Error Resume Next
        
          Select Case combo1.ListIndex
        
          Case 0
        
                stc='year'
          Case 1
        
                stc='month'
        
          Case 2
        
                stc='day'
        
          End Select
        
          Adodc.RecordSource='select stuID as 學(xué)號,stuName as 姓名,stuAge as 年齡,stuBornDate as 出生日期 from Student_Info where ' & stc & '(stuBornDate)='' & txtQuery.Text & '''
        
          Adodc.Refresh
        
          If Err then Msgbox'請輸入正確信息!',vbOKOnly,'錯誤'\
        
      End Sub
        
         
        
      Private Sub Form_Load()
        
             Adodc.ConnectonString='Provider=SQLOLEDB;Data Source=.;Initail Catalog=student;UID=sa;PWD=123456'
        
             Adodc.RecordSource='select stuID as 學(xué)號,stuName as 姓名,stuAge as 年齡,stuBornDate as 出生日期 from Student_Info'
        
             set DataGrid.DataSource=Adodc
        
             combo1.AddItem'按年'
        
             combo1.AddItem'按月'
        
             combo1.AddItem'按日'
        
      End Sub

        

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多