Connect To SQLServer

This simple snippet creates a connection to an SQLServer database.


  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. class Program
  5. {
  6.     private static SqlConnection _con = null;
  7.     static void Main(string[] args)
  8.     {
  9.         string host = ""; //Enter your host
  10.         string catalog = ""; // Enter your database name
  11.         string strProvider = "Data Source=" + host + ";Initial Catalog=" + catalog + ";Integrated Security=True";
  12.         try
  13.         {
  14.             _con = new SqlConnection(strProvider);
  15.             _con.Open();
  16.             if (_con.State == ConnectionState.Open)
  17.             {
  18.                 Console.WriteLine("Connection Open");
  19.             }
  20.         }
  21.         catch (Exception e)
  22.         {
  23.             Console.WriteLine(e.Message);
  24.         }
  25.         Console.ReadKey();
  26.     }
  27. }

No comments:

Post a Comment