);
// 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 #