Catch SqlException when opening connection : SqlException : ADO.Net C# Examples


C# Examples » ADO.Net » SqlException »

 

Catch SqlException when opening connection









    
using  System;
using  System.Drawing;
using  System.Collections;
using  System.ComponentModel;
using  System.Windows.Forms;
using  System.Data;
using  System.Data.OleDb;
using  System.Data.SqlClient;
using  System.IO;

public  class  MainClass{

        public  static  void  Main(){
                  string  ConnectionString  ="server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated  Security=SSPI;";
                  SqlConnection  conn  =  new  SqlConnection(ConnectionString);

                  try
                  {
                          conn.Open();
                  }
                  catch(SqlException  ae)
                  {
                          Console.WriteLine("{0}:  Threw  an  Error:  ***{1}***"+
                                  "with  SqlServer  Code  {2}",  
                                  ae.Errors[0].Server,  
                                  ae.Errors[0].Message,ae.Errors[0].Number);  
                  }

    }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo ADO.Net
» SqlException