asp+Access通用的自動替換資料庫中的字串_應用技巧

來源:互聯網
上載者:User

當初只是為了玩玩寫的,沒想到寫了之後不斷有人詢問,所以改寫了一下代碼,完善了一下,支援了正則替換,避開了會導致出錯的二進位(ole對象),並且做了一個EXE的程式。感謝虛擬幫忙。
附asp代碼:

複製代碼 代碼如下:

<%    
'####################################    
'批量替換資料庫內容2008-3-17     
'替換是無法復原的,所以操作前做好能備份    
'####################################    
Dim Db,conn,Rs1,Rs2,Str1,Str2,I,Re    
str1="admi[0-z]" '要替換的字串,支援正則    
str2="1234" '替換為的字串    
Db="db.mdb" '資料庫地址    
'以下無需改動    
'建立正則對象    
Set Re=new RegExp    
    Re.IgnoreCase =True   
    Re.Global=True   
Set Conn=Server.CreateObject("ADODB.Connection")    
    Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&server.mappath(Db)    
Set Rs1 = Conn.OpenSchema(20)    
    Do While Not Rs1.Eof    
        If UCase(Rs1("TABLE_TYPE"))="TABLE" Then '如果是使用者表,則進行操作    
          Set Rs2=Server.Createobject("adodb.recordset")    
              Rs2.Open "select * from ["&Rs1("TABLE_NAME")"]",conn,1,3    
              Do While Not Rs2.Eof    
                For I=0 to Rs2.fields.count-1 '遍曆所有欄位    
                    If Rs2(i).Properties("ISAUTOINCREMENT") = False And Rs2(i).Type<>128 And Rs2(i).Type<>204 And Rs2(i).Type<>205 Then '如果非自動編號並且非ole對象    
                        Rs2(Rs2(i).name)=Myreplace(Rs2(i).value&"")    
                    End If   
                Next   
             Rs2.Movenext    
             Loop   
        Rs2.Close    
        Set Rs2=nothing    
        End If   
    Rs1.MoveNext    
    Loop   
Rs1.Close    
Set Rs1=Nothing   
Conn.close    
Set Conn=Nothing   
Set Re=Nothing   
Response.Write "替換完成"   

'自訂的替換函數    
Function myreplace(byval Tstr)    
If Tstr="" Or isnull(Tstr) Then     
 Exit Function   
Else   
    Re.Pattern=Str1    
    myReplace =Re.Replace(Tstr,Str2)    
End If   
End Function   
%> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.