Correct sqlceexception Handling Method

Source: Internet
Author: User
If you have been engaged in the development of. NET Compact Framework 1.0 and SQL Server ce 2.0 on PPC, You should have already handed in with sqlceexception. This article describes how to correctly capture and effectively handle sqlceexception.

1. Capture sqlceexception
In. in NET Compact Framework 1.0, sqlceexception is an exception class that requires special processing. If you do not capture it separately, the obtained exception information will be "sqlceexception" or an empty string, instead of the real exception information you want to obtain. Next I will analyze several paragraphs for you.Code:
[Code 1]

Try
{
// Create a database connection. The database in the connection string does not exist.
Sqlceconnection Conn =   New Sqlceconnection ( " Data Source = nonexistsource. SDF; " );

// open a database connection, A sqlceexception
Conn. open ();
}< br>
catch (exception ex)
{< br>
// the obtained exception information is a blank string
MessageBox. show (ex. message);
}


[Code 1]MediumThe exception handling code of captures an exception instance of sqlceexception. If the type conversion is not performed on the instance in the Catch Block, the message value is obtained directly, the error message is a blank string, not the original exception information. Let's take a look.[Code 2]:
[Code 2]
Try
{
// Create a database connection. The database in the connection string does not exist.
Sqlceconnection Conn =   New Sqlceconnection ( " Data Source = nonexistsource. SDF; " );

// Opening the database connection will throw a sqlceexception
Conn. open ();
}
Catch (Sqlceexception ssex)
{
// The obtained exception information is as follows:
// "The database file cannot be found. Check the path to the database. [, file name,]"
MessageBox. Show (ssex. Message );
}
Catch (Exception ex)
{
// Obtain other exception information
MessageBox. Show (ex. Message );
}


In[Code 2]Sqlceexception is captured separately, and the original error message "the database file cannot be found. check the path to the database. [, file name,] ".

Ii. Handling sqlceexception
If you have a class and method for unified Exception Handling, such as exceptionmanager. Publish (exception ). According to the above analysis, You have to convert the sqlceexception type in the pulibsh method. For example[Code 3]As shown in:
[Code 3]

Stringbuilder sb =   New Stringbuilder ();
SB. append (
" Message: " );
If (Exception Is Sqlceexception)
{
SB. append (sqlceexception) exception). Message );
}
Else
{
SB. append (exception. Message );
}

The error information obtained from the sqlceexception. Message attribute is often not detailed enough because it is pre-defined in SQL Server ce 2.0. We hope to get more detailed error information and help us quickly find the specific location of the error code. For example[Code 2], We want to know what the database file name cannot be found. In fact, in addition to the Message attribute, more error information can be obtained from sqlceexception. Please refer[Code 4]:
[Code 4]

///   <Summary>
/// Sqlceexception exception handling that generates a detailed error messageProgram.
///   </Summary>
///   <Param name = "exception"> Sqlceexception exception object. </Param>
Public   Static   Void Showerrors (sqlceexception exception)
{
// Obtains a set of one or more sqlceerror objects.
// SQL Server Ce. NET Framework lite data provider exception details.
Sqlceerrorcollection errs = Exception. errors;

// Obtain the exception instance that causes the current exception.
Exception inner = Exception. innerexception;
If ( Null   ! = Inner)
{
// Displays internal exception information.
MessageBox. Show ( " Inner exception: "   + Inner. tostring ());
}

Stringbuilder sb= NewStringbuilder ();

// Display details of each error in the message box
Foreach (Sqlceerror err In Errs)
{
// Identifies the hresult value of the error type. These errors are not inherent in SQL Server ce.
SB. append ( " Error code: " ). Append (ERR. hresult. tostring ( " X " ));

//The text that describes the error.
SB. append ("\ Nmessage:"). Append (ERR. Message );

// obtain the local error code of sqlceerror.
Sb. append ( " \ nminor err.: " ). append (err. nativeerror);

//Generate the name of the wrong provider.
SB. append ("\ Nsource:"). Append (ERR. source );

// Traverse the first three error parameters. SQL Server ce uses error parameters to provide other details about errors.
Foreach ( Int Numpara In Err. numericerrorparameters)
{
// Although errors may have parameters, not all errors return parameters.
// If no parameter is returned when an error occurs, the value of this array is 0.
If ( Numpara ! = 0)
{
SB. append (
" \ Nnum. par .: " ). Append (numpara );
}
}

// Traverse the last three error parameters. SQL Server ce uses error parameters to provide other details about errors.
Foreach ( String Errpara In Err. errorparameters)
{
// Although errors may have parameters, not all errors return parameters.
// If no parameter is returned when an error occurs, the value of this array is a null string.
If (Errpara ! = String. Empty)
{
SB. append (
" \ Nerr. par .: " ). Append (errpara );
}
}

MessageBox. Show (sb. tostring ());
SB. Remove (0, SB. Length );
}
}

We can use[Code 4]The showerrors method in.[Code 2]Sqlceexception in.
[Code 5]

Try
{
// Create a database connection. The database in the connection string does not exist.
Sqlceconnection Conn =   New Sqlceconnection ( " Data Source = nonexistsource. SDF; " );

// open a database connection, A sqlceexception
Conn. open ();
}< br>
catch (sqlceexception ssex)
{< br>
// obtain detailed exception information
showerrors (ssex );
}< br>
catch (exception ex)
{< br>
// obtain other exception information
MessageBox. show (ex. message);
}

 

 

Shows the exception information:

Iii. Summary
The content of this article is my experience gained during project development. I hope to help those who have just started SQL Server ce development with sqlceexceptionHelp.

Sample Code

[Reference]
SQL Server ce 2.0 books online: Error Handling in C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.