Csharp/C Sharp/Database ADO.net/SqlException

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

Catch Sql command exceptions

<source lang="csharp">

using System; using System.Data; using System.Data.SqlClient;

  class SqlExceptionDemo {
     static void Main(){
        string connString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
        SqlConnection conn = new SqlConnection(connString);
        SqlCommand cmd = conn.CreateCommand();
     
        cmd.rumandType = CommandType.StoredProcedure; 
        cmd.rumandText = "something wrong here";
        try {
           conn.Open();

           SqlDataReader dr = cmd.ExecuteReader();
           string str = dr.GetValue(20).ToString();
           dr.Close();
        }
        catch (System.InvalidOperationException ex)
        {
           string str;
           str = "Source:" + ex.Source;
           str += "\n" + "Message:"+ ex.Message;
           str += "\n" + "\n";
           str += "\n" + "Stack Trace :" + ex.StackTrace;
           Console.WriteLine (str, "Specific Exception");
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
           string str;
           str = "Source:" + ex.Source;
           str += "\n" + "Message:" + ex.Message;
           Console.WriteLine (str, "Database Exception");
        }
        catch (System.Exception ex)
        {
           string str;
           str = "Source:" + ex.Source;
           str += "\n"+ "Message:" + ex.Message;
           Console.WriteLine (str, "Generic Exception");
        }
        finally
        {
           if ( conn.State == ConnectionState.Open)
           {
              Console.WriteLine ("Finally block closing the connection", "Finally");
              conn.Close();
           }
        }
     }
  }


      </source>


Deal with multiple Sql error in SqlException

<source lang="csharp"> using System; using System.Data; using System.Data.SqlClient;

  class SqlExceptionDemo {
     static void Main(){
        string connString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
        SqlConnection conn = new SqlConnection(connString);
        SqlCommand cmd = conn.CreateCommand();
        cmd.rumandType = CommandType.StoredProcedure;
        cmd.rumandText = "wrong";
        try
        {
           conn.Open();
           cmd.ExecuteNonQuery();
        }  
        catch (System.Data.SqlClient.SqlException ex)
        {
           string str ="";
           for (int i = 0; i < ex.Errors.Count; i++)
           {
              str += "\n" + "Index #" + i + "\n" +
                 "Exception : " + ex.Errors[i].ToString() + "\n" +
                 "Number:" + ex.Errors[i].Number.ToString() + "\n"
                 ;
           }
       
           Console.WriteLine(str);
        }
        catch (System.Exception ex)
        {
           string str;
           str = "Source:"+ ex.Source;
           str += "\n"+ "Error Message:"+ ex.Message;
           Console.WriteLine (str);
        }
        finally
        {
           if (conn.State == ConnectionState.Open)
           {
              Console.WriteLine ("Finally block closing the connection", "Finally");
              conn.Close();
           }
        }  
     }
  }


      </source>


SqlException detail info: line number, procedure, server

<source lang="csharp"> using System; using System.Data; using System.Data.SqlClient;

  class SqlExceptionDemo {
     static void Main(){
        string connString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
        SqlConnection conn = new SqlConnection(connString);
        SqlCommand cmd = conn.CreateCommand();
        cmd.rumandType = CommandType.StoredProcedure;
        cmd.rumandText = "wrong command";
        try {
           conn.Open();
           cmd.ExecuteNonQuery();
        }  
        catch (System.Data.SqlClient.SqlException ex)
        {
           string str;
           str = "Source:"+ ex.Source;        
           str += "\n"+ "Number:"+ ex.Number.ToString();
           str += "\n"+ "Message:"+ ex.Message;
           str += "\n"+ "Class:"+ ex.Class.ToString ();
           str += "\n"+ "Procedure:"+ ex.Procedure.ToString();
           str += "\n"+ "Line Number:"+ex.LineNumber.ToString();
           str += "\n"+ "Server:"+ ex.Server.ToString();
       
           Console.WriteLine (str, "Database Exception");
        }
        catch (System.Exception ex)
        {
           string str;
           str = "Source:"+ ex.Source;
           str += "\n"+ "Error Message:"+ ex.Message;
           Console.WriteLine (str, "General Exception");
        }
        finally
        {
           if (conn.State == ConnectionState.Open)
           {
              Console.WriteLine ("Finally block closing the connection", "Finally");
              conn.Close();
           }        
        } 
     }
  }


      </source>


SqlException message

<source lang="csharp">

using System; using System.Data; using System.Data.SqlClient;

  class SqlExceptionDemo {
     static void Main(){
        string connString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
        SqlConnection conn = new SqlConnection(connString);
        SqlCommand cmd = conn.CreateCommand(); 
 
        cmd.rumandType = CommandType.StoredProcedure;
 
        cmd.rumandText = "wrong sql";
        try{  
           conn.Open();
           SqlDataReader dr = cmd.ExecuteReader();    
           dr.Close();      
        } catch (System.Data.SqlClient.SqlException ex) {
           string str;
           str = "Source:" + ex.Source;
           str += "\n" + "Exception Message:" + ex.Message;
           Console.WriteLine(str);
        }
        catch (System.Exception ex) 
        {
           string str;
           str = "Source:" + ex.Source;
           str += "\n" + "Exception Message:" + ex.Message;
           Console.WriteLine(str);
        }
        finally
        {
           if (conn.State == ConnectionState.Open)
           {
              Console.WriteLine("Finally block closing the connection", "Finally");
              conn.Close();
           }   
        } 
     }
  }
          
      </source>