Advertisement
OKIEWARDOYO

VISUAL C# No.39 1

Feb 17th, 2014
4,090
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.42 KB | None | 0 0
  1.  
  2.  
  3.         private void makeConnection(){
  4.             Console.WriteLine("\nBegin connecting...");
  5.  
  6.             mdfFilename = "Database1.mdf";
  7.             outputFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  8.             attachDbFilename = Path.Combine(outputFolder, mdfFilename);
  9.  
  10.             connString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + attachDbFilename + ";Integrated Security=True";
  11.  
  12.             connection = new SqlConnection(connString);
  13.  
  14.             //kalo menggunakan using block,
  15.             //maka koneksi akan otomatis closed pada akhir using block,
  16.  
  17.             try
  18.             {
  19.  
  20.                 connection.Open();
  21.  
  22.                 Console.WriteLine("Connected..");
  23.                 Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
  24.                 Console.WriteLine("State: {0}", connection.State);
  25.                 Console.WriteLine("ConnectionString: {0}", connection.ConnectionString);
  26.  
  27.                 is_connecting = true;
  28.                
  29.             }
  30.             catch (Exception ex)
  31.             {
  32.                 Console.WriteLine("Failed to Connect::" + ex.Message);
  33.                
  34.             }
  35.         }
  36.  
  37.         private void closeConnection(){
  38.             if (is_connecting == true)
  39.             {
  40.                 Console.WriteLine("\nClosing connection...");
  41.                 connection.Close();
  42.             }
  43.  
  44.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement