编程之道

无论是批评的,赞扬的,指着的都请EMAIL给我,你的建议将是我前进的动力! 联系我

自己在一个项目使用到的DBUntil类


先看下INETA牛人Stephen Walther的比较测试结论.希望对大家有用

  • DataReadear比DataSet快15%
  • SqlDataReader比OleDbDataReader快50%
  • 用DataReader的ASP风格的表格显示比DataGrid绑定DataReader快60%
  • 用Ordinal的DataReader访问字段比用名字访问快15%
  • DataGrid中用AutoGenerateColumns=true比用显式绑定快24%
  • 尽量用缓存

测试由于比较局限,所以不一定很准确,但可以做个参考。

Imports  System.Data.OleDb


Public   Class  ComDataBase

    
Private  LsConn  As   String          
     Private  LoleConn  As  OleDbConnection         
     Private  LoleTrans  As  OleDbTransaction        

     Public   Sub   New ()
        
Dim  bOracle  As   Boolean   =   False
        
Dim  oIni  As   New  ComIniFile( " ..\INI\CCCC001.ini " )

        
If  (oIni.GetValue( " DB " " DBTYPE " =   " ORACLE " Then
            bOracle 
=   True
        
End   If

        
Dim  sServer  As   String   =  oIni.GetValue( " DB " " SERVERNAME "

         Dim  sDBName  As   String   =  oIni.GetValue( " DB " " DBNAME " )      
         Dim  sUser  As   String   =  oIni.GetValue( " DB " " USER " )          
         Dim  sPsw  As   String   =  oIni.GetValue( " DB " " PASSWORD " )       

        
If  (bOracle)  Then
            LsConn  =   " Provider=OraOLEDB.Oracle;Data Source= "   &  sDBName _
                    
&   " ;User Id= "   &  sUser  &   " ;Password= "   &  sPsw  &   " ;OLEDB.NET=true "
        
Else
            
LsConn  =   " Provider=sqloledb;Data Source= "   &  sServer  &   " ;Initial Catalog= "  _
                     
&  sDBName  &   " ;User Id= "   &  sUser  &   " ;Password= "   &  sPsw  &   " ; "
        
End   If

    
End Sub

'db open
     Public   Sub  Open()
        
Try
        

             If  ( Not  (LoleConn  Is   Nothing ))  Then
      
                 If  (LoleConn.State  =  ConnectionState.Open)  Then
                    
' '接続文字列は定義しない文字列
                     If  (LoleConn.ConnectionString  <>  LsConn)  Then
                        LoleConn.Close()
                        LoleConn.ConnectionString 
=  LsConn
                        LoleConn.Open()
                    
End   If
                
Else
                    
' '接続文字列は定義しない文字列
                     If  (LoleConn.ConnectionString  <>  LsConn)  Then
                        LoleConn.ConnectionString 
=  LsConn
                    
End   If
                    LoleConn.Open()
                
End   If
            
Else
                LoleConn 
=   New  OleDbConnection(LsConn)
                LoleConn.Open()
            
End   If
        
Catch  ex  As  Exception
            ComLog.SetErrLog(
" ComDataBase " " Open " " データベースの接続に失敗しました。 "   &  ex.Message)
            ComMsgBox.ErrMsg(
" E-0002 " )
        
End   Try
    
End Sub


     Public   Sub  Close()
        
Try
            
' 'データ ソースへの接続を閉じする
             If  ( Not  (LoleConn  Is   Nothing ))  Then
                LoleConn.Close()
            
End   If
        
Catch  ex  As  Exception
            ComLog.SetErrLog(
" ComDataBase " " Close " , ex.Message)
        
Finally
            
' '対象を解放する
             If  ( Not  (LoleConn  Is   Nothing ))  Then
                LoleConn.Dispose()
                LoleConn 
=   Nothing
            
End   If
        
End   Try
    
End Sub


     Public   Sub  BeginTrans()
        
' 'トランザクションを開始する
        LoleTrans  =  LoleConn.BeginTransaction()
    
End Sub


     Public   Sub  Commit()
        Execute(
" Delete システム管理 where 1=2 " )
        
' 'トランザクションの終点をマークする
        LoleTrans.Commit()
    
End Sub


     Public   Sub  RollBack()
        Execute(
" Delete システム管理 where 1=2 " )
        
' 'データ変更を消去する
        LoleTrans.Rollback()
    
End Sub

     Public   Function  GetDataSet( ByVal  sSQL  As   String As  DataSet
        
Dim  oleAdapter  As  OleDbDataAdapter
        
Dim  oDataSet  As  DataSet  =   New  DataSet

        
Try
            
Dim  oleCommand  As   New  OleDbCommand(sSQL, LoleConn)
            oleCommand.Transaction 
=  LoleTrans
            oleAdapter 
=   New  OleDbDataAdapter(oleCommand)
            oleAdapter.Fill(oDataSet)               
' 'SQL文を検索する
         Finally
            oleAdapter.Dispose()                    
' '対象を解放
         End   Try
        
Return  oDataSet
    
End Function


     Public   Function  Query( ByVal  sSQL  As   String As  OleDbDataReader
        
Dim  oleCommand  As   New  OleDbCommand(sSQL, LoleConn)
        
Try
            oleCommand.Transaction 
=  LoleTrans
            
Return  oleCommand.ExecuteReader()        ' 'SQL文を検索する
         Finally
            oleCommand.Dispose()                    
' '対象を解放
         End   Try
    
End Function


     Public   Function  Query( ByVal  sSQL  As   String ByRef  aryOleDbParameter  As  ArrayList)  As  OleDbDataReader
        
Dim  oleCommand  As   New  OleDbCommand(sSQL, LoleConn)
        
Dim  oleParam  As  OleDbParameter

        
Try
            oleCommand.Transaction 
=  LoleTrans

            
For   Each  oleParam  In  aryOleDbParameter
                oleCommand.Parameters.Add(oleParam)
            
Next
            
Return  oleCommand.ExecuteReader()        ' 'SQL文を検索する
         Finally
            oleCommand.Dispose()                    
' '対象を解放
         End   Try
    
End Function


     Public   Function  Execute( ByVal  sSQL  As   String As   Integer
        
Dim  oleCommand  As   New  OleDbCommand(sSQL, LoleConn)    ' 'OleDbCommandの新インスタンス
         Try
            oleCommand.Transaction 
=  LoleTrans
            
Return  oleCommand.ExecuteNonQuery()     
         Finally
            oleCommand.Dispose()                    
       
        
End   Try
    
End Function

 

     Public   Function  Execute( ByVal  sSQL  As   String ByRef  aryOleDbParameter  As  ArrayList)  As   Integer
        
Dim  oleCommand  As   New  OleDbCommand(sSQL, LoleConn)   
         Dim  oleParam  As  OleDbParameter
        
Try
            oleCommand.Transaction 
=  LoleTrans

            
For   Each  oleParam  In  aryOleDbParameter
                oleCommand.Parameters.Add(oleParam)
            
Next

            
Return  oleCommand.ExecuteNonQuery()     
         Finally
            oleCommand.Dispose()  

        
End   Try
    
End Function

基本功能都有了,对于小项目而言,这个类够用了。
 

posted on 2006-06-09 12:24 疯流成性 阅读(654) 评论(0)  编辑  收藏 所属分类: .NET


只有注册用户登录后才能发表评论。


网站导航: