Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1.         private SqlConnection connection = null;
  2.         private SqlCommand cmd;
  3.         private SqlDataReader reader;
  4.         private bool IsDatabaseOpen = false;
  5.         private string _DB = null;
  6.         private cp_ini _c;
  7.         public cp_db(string db)
  8.         {
  9.             _c = new cp_ini(Environment.CurrentDirectory + @"\Config\Config.cfg");
  10.  
  11.             string host = _c.Read("host", "Config");
  12.             string user = _c.Read("username", "Config");
  13.             string password = _c.Read("password", "Config");
  14.  
  15.             SqlConnectionStringBuilder SqlConnection = new SqlConnectionStringBuilder();
  16.             SqlConnection.DataSource = host;
  17.             SqlConnection.UserID = user;
  18.             SqlConnection.Password = password;
  19.             SqlConnection.InitialCatalog = db;
  20.             SqlConnection.MultipleActiveResultSets = true;
  21.  
  22.             string connection_string = SqlConnection.ToString();
  23.            
  24.             Console.WriteLine("Connecting to a SQL Server...");
  25.             Console.WriteLine(connection_string);
  26.  
  27.             connection = new SqlConnection(connection_string);
  28.  
  29.             _DB = db;
  30.  
  31.             this.IsDatabaseOpen = Open();
  32.         }
  33.  
  34.         private bool Open()
  35.         {
  36.             bool isOpen = false;
  37.  
  38.             if (connection != null)
  39.             {
  40.                 try
  41.                 {
  42.                     connection.Open();
  43.                 }
  44.                 catch (SqlException ex)
  45.                 {
  46.                     throw ex;
  47.                 }
  48.                 finally
  49.                 {
  50.                     if (connection.State == ConnectionState.Open) isOpen = true;
  51.                     Console.WriteLine("SQL Server -> Connection has been successfully established");
  52.                 }
  53.             }
  54.             return isOpen;
  55.         }
  56.  
  57.         public bool DBStatusOpen()
  58.         {
  59.             return this.IsDatabaseOpen;
  60.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement