Csharp/CSharp Tutorial/ADO.Net/SqlException

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Catch SqlClient.SqlException

using System;
using System.Data;
using System.Data.SqlClient;
class MainClass
{
   static void Main()
   {
         SqlConnection conn = new SqlConnection(@"data source = .\sqlexpress;integrated security = true;database = northwind");
         SqlCommand cmd = conn.CreateCommand();
         cmd.rumandType = CommandType.StoredProcedure;
         cmd.rumandText = "sp_DbException_1";
         try
         {
            conn.Open();
            cmd.ExecuteNonQuery();
         }
         catch (System.Data.SqlClient.SqlException ex)
         {
            Console.WriteLine("Source: " + ex.Source);
            Console.WriteLine("Number: "+ ex.Number.ToString());
            Console.WriteLine("Message: "+ ex.Message);
            Console.WriteLine("Class: "+ ex.Class.ToString ());
            Console.WriteLine("Procedure: "+ ex.Procedure.ToString());
            Console.WriteLine("Line Number: "+ex.LineNumber.ToString());
            Console.WriteLine("Server: "+ ex.Server.ToString());
         }
         catch (System.Exception ex)
         {
            Console.WriteLine("Source: " + ex.Source);
            Console.WriteLine("Exception Message: " + ex.Message);
         }
         finally
         {
            if (conn.State == ConnectionState.Open)
            {
               Console.WriteLine("Finally block closing the connection");
               conn.Close();
            }
         }
   }
}

Catch SqlException when opening connection

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
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); 
         }
  }
}

Loop through all errors

using System;
using System.Data;
using System.Data.SqlClient;
class MainClass
{
   static void Main()
   {
         SqlConnection conn = new SqlConnection(@"data source = .\sqlexpress;integrated security = true;database = northwnd");
         SqlCommand cmd = conn.CreateCommand();
         cmd.rumandType = CommandType.StoredProcedure;
         cmd.rumandText = "error command";
         try
         {
            conn.Open();
            cmd.ExecuteNonQuery();
         }
         catch (System.Data.SqlClient.SqlException ex)
         {
            for (int i = 0; i < ex.Errors.Count; i++)
            {
                 Console.WriteLine("Index #" + i);
                 Console.WriteLine("Exception: " + ex.Errors[i].ToString() );
                 Console.WriteLine("Number: " + ex.Errors[i].Number.ToString() );
            }
         }
         catch (System.Exception ex)
         {
            Console.WriteLine("Source: " + ex.Source);
            Console.WriteLine("Exception Message: " + ex.Message);
         }
         finally
         {
            if (conn.State == ConnectionState.Open)
            {
               Console.WriteLine("Finally block closing the connection");
               conn.Close();
            }
         }
   }
}
Index #0
Exception: System.Data.SqlClient.SqlError: Cannot open database "northwnd" requested by the login. T
he login failed.
Number: 4060
Index #1
Exception: System.Data.SqlClient.SqlError: Login failed for user "nfex\Joe".
Number: 18456