Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
C# DB Connection Code Question
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01311457
Message ID:
01311589
Views:
17
Ahh I see. Yes you are correct the SqlConnectionBuilder is only for MS SQL.
I don't know how generic you can make it. You will for sure have to pass along information about what kind of database you are connecting to. Most database servers have their own connection string format. A good reference is : http://www.connectionstrings.com/



>This looks like a SQL Connection String builder - which would be fine if I was always going to connect to SQL.
>
>I want to create a generic class that will return an open SQL, ODBC or OleDb connection object. There must be a way
>to make this generically.
>
>
>
>>If you are using 2.0 (or newer) you should look into the SqlConnectionBuilder class.
>>
>>>How do I make this routine generic so that I can connect to any type of database?
>>>
>>>
>>>public SqlConnection GetConnection()
>>>{
>>>
>>>    // Define variables
>>>    bool bTrustedConnection = true;
>>>    string sDatabase = "test";
>>>    string sPassword = "";
>>>    string sServer = "PC108\\SQLEXPRESS";
>>>    string sUserName = "";
>>>
>>>    // Define variables
>>>    string sConnString = "";
>>>
>>>    // If the user is trusted...
>>>    if (bTrustedConnection)
>>>    {
>>>        // Create the connection string for a trusted connection
>>>        sConnString = "Server=" + sServer + ";Database= " + sDatabase + ";Trusted_Connection=yes";
>>>    }
>>>    else
>>>    {
>>>        // Create the connection string for a logged in user
>>>        sConnString = "Server=" + sServer + ";Database= " + sDatabase + ";Uid=" + sUserName + ";Pwd=" + sPassword;
>>>    }
>>>
>>>    // Create a connection object, passing the connection string
>>>    SqlConnection oConnection = new SqlConnection(sConnString);
>>>
>>>    try
>>>    {
>>>        // Attempt to open the connection
>>>        oConnection.Open();
>>>    }
>>>    catch (Exception oException)
>>>    {
>>>        // Handle exception here
>>>    }
>>>
>>>    return oConnection;
>>>}
>>>
Semper ubi sub ubi.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform