Advertisement
Guest User

Untitled

a guest
Jun 4th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Web;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6.  
  7. public static class SQL
  8. {
  9.     // Connexion à la base de données
  10.     public static void connexion_sql(string databaseServer, string databaseName, string databaseUsername = null, string databasePassword = null)
  11.     {
  12.         string ConnectionString = "Data Source=" + databaseServer + ";";
  13.                ConnectionString += "User ID=" + databaseUsername + ";";
  14.                ConnectionString += "Password=" + databasePassword + ";";
  15.                ConnectionString += "Initial Catalog=" + databaseName;
  16.        
  17.         SqlConnection SQLConnection = new SqlConnection();
  18.        
  19.         try
  20.         {
  21.             SQLConnection.ConnectionString = ConnectionString;
  22.             SQLConnection.Open();
  23.         }
  24.         catch (Exception Ex)
  25.         {
  26.             // Tente de fermer la connexion
  27.             if (SQLConnection != null)
  28.             {
  29.                 SQLConnection.Dispose();
  30.             }
  31.        
  32.             // Crée un message d'erreur (utile)
  33.             string ErrorMessage = "A error occurred while trying to connect to the server.";
  34.                    ErrorMessage += Environment.NewLine;
  35.                    ErrorMessage += Environment.NewLine;
  36.                    ErrorMessage += Ex.Message;
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement