How we handle sql exceptions?

How we handle sql exceptions?

SqlExceptionclass is created whenever the .NET Framework  Data Provider for SQL Server encounters an error generated from the server. (Client side errors are thrown as standard common language runtime exceptions.) SqlException always contains at least one instance of SqlError.

...

try
{
myCommand.Connection.Open();
}
catch (SqlException e)
{
string errorMessages ;

for (int i 0; i < e.Errors.Count; i++)
{
errorMessages + Index # + i + \n +
Message: + e.Errors[i].Message + \n +
LineNumber: + e.Errors[i].LineNumber + \n +
Source: + e.Errors[i].Source + \n +
Procedure: + e.Errors[i].Procedure + \n ;
}

System.Diagnostics.EventLog log new System.Diagnostics.EventLog();
log.Source My Application ;
log.WriteEntry(errorMessages);
Console.WriteLine( An exception occurred. Please contact your system administrator. );
}

By:kamal
Date:2014-10-10 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related C Links

C interview questions and answers for experienced and fresher