Csharp/C Sharp by API/System.Data.SqlClient/SqlException
SqlException.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();
}
}
}
}
SqlException.LineNumber
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();
}
}
}
}
SqlException.Number
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();
}
}
}
}