Manually load the driver
How to load an ADO.NET driver but when a connection is needed I want to specify the connection string manually which means through my code and without having it in the config file ?
If there's no way to do this, what's the best approach to achieve this, knowing that I'm writing an app that开发者_如何学C manages different databases (so different DBMS) that are inputted by the user himself ?
You can find plenty of examples here:
http://www.connectionstrings.com/
Example (using http://www.connectionstrings.com/postgre-sql):
var connection = new PostgreSqlConnection("Server=127.0.0.1;Port=5432;Database=myDataBase;User Id=myUsername;Password=myPassword");
connection.Open();
You can use db provider factory for interface based programming instead if driver specific. http://msdn.microsoft.com/en-us/library/dd0w4a2z(VS.80).aspx
To create a factory: http://msdn.microsoft.com/en-us/library/dd0w4a2z.aspx
Edit
I think I understand what you are looking for. To load the actual driver assembly at runtime. You can do that using the DbProviderFactory
. You don't need to add any driver specific references to your project when using it. However, the drivers must be installed on the computer =)
精彩评论