使用 Unity(DI)+Oracle.ManagedDataAccess+Unity.config 连接DB (Oracle版)

记录,学习用
参考来源:

1.Unity(DI)でDBに接続してみよう    http://mk3008net.hatenablog.com/entry/2017/01/09/001325

2.WebアプリからOracleに接続するconnectionStrings(tnsname.ora不要)    https://qiita.com/lainzero/items/32f424c98544c7a1b2fe


使用工具
Visual Studio 安装以下套件*(NuGet)

==== Unity ====
Install-Package CommonServiceLocator -Version 1.3.0
Install-Package Unity -Version 4.0.1
Install-Package Unity.Interception -Version 4.0.1
==== Oracle DB接続 ====
Install-Package Oracle.ManagedDataAccess -Version 12.1.24160719

Step1:安装完成后将App.config修改成



  
    
    

Step2:建立Unity.config档




  
  
  
  
  
  

  
    
    
    

    
      
        
      
      
      
    

    
      
        
        
      
      
      
      
    

  

Step3:修改Module1.vb档

Imports Microsoft.Practices.ServiceLocation
Imports Microsoft.Practices.Unity.Configuration
Imports Microsoft.Practices.Unity

Module Module1

    Sub Main()

        Dim container As New UnityContainer()
        container.LoadConfiguration()
        Dim service As New UnityServiceLocator(container)
        ServiceLocator.SetLocatorProvider(Function() service)

        Using cmd = ServiceLocator.Current.GetInstance(Of IDbCommand)("cmdExhibit")
            cmd.CommandText = "SELECT CURRENT_TIMESTAMP FROM DUAL"
            Dim d = cmd.ExecuteScalar()
            Console.WriteLine(d)
        End Using

        Console.WriteLine("何かキーを押してください")
        Console.ReadKey()

    End Sub

End Module