Advertisement
Androide28

SQL Connection

Apr 7th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. public class clsSQL
  2.     {
  3.         public string strConn()
  4.         {
  5.             string connString = @"Data Source=xxx.xxx.x.xx\SQLEXPRESS,1433;" +
  6.                                 "Network Library=DBMSSOCN;" +
  7.                                 "Initial Catalog=DB;" +
  8.                                 "User ID=sa;" +
  9.                                 "Password=xxxxxxxx;";
  10.  
  11.             return connString;
  12.         }
  13.  
  14.         public bool isConnected(string connectionString)
  15.         {
  16.             bool bState = true;
  17.             try
  18.             {
  19.                 using (SqlConnection conn = new SqlConnection(connectionString))
  20.                 {
  21.                     conn.Open();
  22.                     if (conn.State == ConnectionState.Closed)
  23.                         bState = false;
  24.                 }
  25.             }
  26.             catch(SqlException ex)
  27.             {
  28.                 Console.WriteLine(ex.Message);
  29.                 bState = false;
  30.             }
  31.             return bState;
  32.         }
  33.     }
  34.  
  35. class Program
  36.     {
  37.         static void Main(string[] args)
  38.         {
  39.             clsSQL sql = new clsSQL();
  40.             Console.WriteLine(sql.isConnected(sql.strConn()) ? "CONNECTED" : "NOT CONNECTED");
  41.             Console.ReadLine();
  42.         }
  43.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement