Hola
Algo que aunque no siempre sea aplicable a la realidad, todos deseamos, es poder tener un programa al cual le podamos cambiar de BD y que todo siga funcionando de forma automática sin mayores traumas en el código fuente, sin mucha charla les comento que desde el framework 2.0 esto es posible con ado.net a través de las clases que se encuentran en el espacio de nombre System.Data.Common
Como es el procedimiento?
- Recuperar el proveedor de data and the connection string from somewhere and create a DbProviderFactory
- Use that to create DataAdapters provider through DbProviderFactory.CreateDataAdapter ()
- use DataAdapters
Creating the connection I'll be
too simplistic just to bring some order and structure code depends on where you put the strings that return the following methods
Public Shared GetProvider Function ( ) As String
Return "System.Data.SqlClient"
End Function
Public Shared Function GetCnnString() As String
Return "Data Source=MiSQLsrv;Initial Catalog=MiBD;Integrated Security = True "
End Function
above functions should be making access to app.config or a text file which the channels recovered up the show as constants
We'll use them, creating a connection from the supplier, so that the connection is indicated for the supplier whose description is obtained GetProvider function () Dim
sProv As String = GetProvider()
Dim sCnn As String = GetCnnString()
Dim
oCnn As DbConnection = nothingDim f As DbProviderFactory = DbProviderFactories.GetFactory(sProv)
oCnn= f.CreateConnection()
oCnn.ConnectionString = sCnn
Now that we have the connection we must generate the commands to run a select query.
Dim cmd As DbCommand = oCnn.CreateCommand ()
cmd.CommandText = "Select * from myTable where id = 1" cmd
. CommandType = CommandType.Text
oCnn.Open ()
Dim ID As Integer=0
Dim sNombres As String=""
dr.Read()
dim ID as integer = dr( "Id" )
sNombres = dr( "Nombres" )
End If
dr.Close()
oCnn.Close()
As you saw the connection used to generate a command, which will depend on the supplier, then I give the text of the query and then get a datareader from which to download the data to variables finally close the DataReader and connection
You see the issue of vendor independence is not as otherworldly as if not more it is really easy. In another article (which I guess will be this year) will show you how to use DataAdapters to record data in a structure similar to this.
I hope this helps someone you
soon and Happy Holidays! Salu2
Sergio
0 comments:
Post a Comment