Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
C# DB Connection Code Question
Message
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01311457
Message ID:
01311491
Vues:
21
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.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform