Catch SqlException when opening connection : SqlException « ADO.Net « C# / CSharp Tutorial






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); 
         }

  }
}








32.27.SqlException
32.27.1.Catch SqlClient.SqlException
32.27.2.Catch SqlException when opening connection
32.27.3.Loop through all errors