Check MS SQL database connection - CSharp Database

CSharp examples for Database:SQL Server

Description

Check MS SQL database connection

Demo Code


using System;/*from  w  ww  .ja  va2 s  .com*/
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
    		try
            {
                using (SqlConnection conn = new SqlConnection("Server=YourServerName\\DBMITSPPS01;Database=Inventory;Trusted_Connection=True;"))
                {
                    try
                    {
                        conn.Open();
                        if (conn.State == ConnectionState.Open)
                        {
                            Console.Write("You have been successfully connected to the database!");
                        }
                        else
                        {
                            Console.Write("Connection failed.");
                        }
                    }
                    catch (SqlException) { }
                }
            }
            catch (Exception ex)
            {
                Console.Write("Error:" + ex);
            }
            finally { }
			Console.ReadLine();
        }
    }
}

Related Tutorials