What happens if limit of Sql Server Compact Edition is reached? - c#

What happens if a database reaches the limit of 4GB of the SQL Server Compact Edition? Is there a special exception for this?
Can I safely catch this event or exception and, let's say, create a new database?

I have not experienced this myself, but it looks like a SqlCeException will be thrown and the NativeError property of the contained SqlCeError will have an error code of 25104 (SSCE_M_DATABASETOOBIG).
Here's a listing of SqlCeError Native Codes related to db engine errors -- the one about the db file being too big is about 2/3 of the way down. The listing is for SQL CE 3.5; you didn't specify what version you were using, but I'm guessing that it wouldn't change.
I don't see why you couldn't catch this exception and then create a new database in your catch section.
try {
//do something
} catch (SqlCeException cexc){
foeach (SqlCeError aError in cexc.Errors) {
if (aError.NativeError == 25104) { //this is the code for the TOO BIG error code
//handle too big error -- maybe create a new database
}
}
}
I hope this helps!

Related

C# Exception Include Event Details

I am new to C# and Windows development in general. I need to use it to build an integration between our data in MySQL to Microsoft Dynamics GP (using eConnect). That part is not really relevant, but adds a little context to the examples below.
Ok, so when I connect to the service:
eConnectClient client = new eConnectClient();
string newCustomerDocument = "SOME_XML_HERE";
string connectionString = "Data Source=localhost;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=GPVPM;";
try
{
client.Open();
bool result = client.CreateEntity(connectionString, newCustomerDocument);
}
catch (FaultException<eConnectFault> e)
{
Console.Write("ECONNECT FAULT: " + e.ToString() + "\n");
}
Now, if I have an error in my XML, it will cause a FaultException to be thrown, but the resulting exception message is useless:
ECONNECT FAULT: System.ServiceModel.FaultException`1[GPConnect.eConnect.eConnectFault]: The creator of this fault did not specify a Reason. (Fault Detail is equal to GPConnect.eConnect.eConnectFault).
I found that if I look in the Event Viewer for Windows, it paints an entirely different picture of what happened:
Specifically:
Error Number = 250 Stored Procedure= taUpdateCreateCustomerRcd Error Description = The Tax Schedule does not exist in the Tax Schedule Master Table
So that is something actionable that can help me identify the problem and fix it.
The question:
With C#, how can I get the same level of details from an Exception as is recorded by the Event Manager?
The server may and may not return the detailed exception to the client. You may want to check
e.Detail /* of type GPConnect.eConnect.eConnectFault */
and
e.InnerException
inside the catch block for potential details.

Converting MS Oracle client to Oracle Data Provider - OracleException

I'm converting a product from System.Data.OracleClient to Oracle.DataAccess.Client, and came across a question Here's a snippet of some code:
try
{
//some db code
}
catch (System.Data.OracleClient.OracleException ex)
{
if (ex.Code == 00904)
{
// log specific error
}
}
The problem is, Oracle.DataAccess.Client.OracleException has no Code property. It has a Number property. Is this the same thing? The docs say this about the property -
This error number can be the topmost level of error generated by
Oracle and can be a provider-specific error number.
The Code property contained the ORA- error code.
Yes, they are the same thing. Oracle.DataAccess.Client.OracleException.Number is the same info as System.Data.OracleClient.OracleException.Code.

throw new Exception statement not working in MVC3 application

I am trying to import details from an access database into my sql application database. I have a check to make sure that it has imported all the rows, and if it hasn't then I want it to stop the process and throw an error.
However, despite it going into the correct function, and hitting the "throw" line, it doesn't throw the error, and instead just carries on with the process.
Should this code block not work?
AccessRepository details = new AccessRepository();
if (numRows != details.GetDetailsRowCount(periodsInFreq, payDate))
{
throw new DataException("Some data is missing from the Details table (only " + numRows.ToString() + " rows) - Please try again. If this problem persists, please contact the system administrator");
}
Thanks
Thanks to Varun and Zenwalker.
It turned out I had some nested catch statements which were cancelling each other out. I've since taken out the nested try/catches out and the errors now make a lot more sense.
Thanks

SQL Server perform backup with C#

I've investigated the possibilities of creating database backups through SMO with C#.
The task is quite easy and code straightforward. I've got only one question: how can I check if the backup was really created?
SqlBackup.SqlBackup method returns no parameters and I don't even know if it throws any exceptions. (the only thing that I know is that it is blocking, because there's also SqlBackupAsync method)
I would appreciate any help.
you can and its very possible to do what you asked for,
but doing the backup it self using SMO its not very hard, but the hard part is managing the backup and the restore.
it would be hard to put all the code here, but its wont fit. so I will try my best to put the lines you need.
SqlBackup.SqlBackup doesn't return any value, its a void function.
but it takes one parameter which is "Server", try out the following code:
Server srvSql;
//Connect to Server using your authentication method and load the databases in srvSql
// THEN
Backup bkpDatabase = new Backup();
bkpDatabase.Action = BackupActionType.Database;
bkpDatabase.Incremental = true; // will take an incemental backup
bkpDatabase.Incremental = false; // will take a Full backup
bkpDatabase.Database = "your DB name";
BackupDeviceItem bDevice = new BackupDeviceItem("Backup.bak", DeviceType.File);
bkpDatabase.Devices.Add(bDevice );
bkpDatabase.PercentCompleteNotification = 1;// this for progress
bkpDatabase.SqlBackup(srvSql);
bkpDatabase.Devices.Clear();
I've investigated the problem using Reflector.NET (I suppose this is legal since RedGate is Ms Gold Certified Partner and Reflector.NET opens .NET libraries out of the box). As I found out the method throws two types of exceptions:
FailedOperationException - in most cases, other exceptions are "translated" (I suppose translating means creating new FailedOperationException and setting InnerException to what was actually thrown)
UnsupportedVersionException - in one case when log truncation is set to TruncateOnly and server major version is more or equal to 10 (which is sql server 2008?)
This solves my problem partially, because I'm not 100% sure that if something goes wrong those exceptions will actually be thrown.

Checking whether a database is available?

My problem involves checking if I have a valid database connection before reading from the database. If the database is down I'd like to write to a xml file instead. I have the location of the database (if it's up) at runtime so if the database was working I can create a new sqlConnection to it.
Use a typical try...catch...finally structure, and based on the specific exception type and message, decide whether you want to write to xml or not.
try
{
SqlConnection connection = new SqlConnection(DB("Your DB Name"));
connection.Open();
}
catch (Exception ex)
{
// check the exception message here, if it's telling you that the db is not available. then
//write to xml file.
WriteToXml();
}
finally
{
connection.Close();
}
I would just use something like:
using(SqlConnection conn = new SqlConnection(c)) {
conn.Open();
}
It will throw an exception if invalid. You could write to the xml in the exception.
An easy way would be to execute a simple query and see if an error occurs:
For Oracle:
SELECT * FROM DUAL
For SQL Server
SELECT 1
Basicly just some kind of relatively "free" query that will let you know that the database is up and running and responding to requests and your connection hasn't timed out.
You cannot really tell whether the DB is up and running without actually opening a connecting to it. But still, connection might be dropped while you're working with it, so this should be accounted for.

Categories