Showing posts with label SQL Server CE. Show all posts
Showing posts with label SQL Server CE. Show all posts

Saturday, August 23, 2008

Using Stored Procs on SQL CE and Windows Mobile

A cool pattern as described in the Windows Mobile Line of Business Solution Accelerator 2008 show how to implement Stored Procedures in SQL CE on Windows Mobile with the Compact Framework.

OK they are not really Stored Procedures as known in the full version of SQL Server as this wouldn't make sense on devices as SQL CE is embedded and runs inproc in the callers context. But this pattern shows a simple example of extracting that ugly SQL code from our lovely OO C#/VB code.

NOTE: We *have* to use SQL and hand cranked data models etc on devices due to the lack of support for ORM's on devices.

Take the following code (using Mobile Client Software Factory Data Access Block) which embeds SQL code in C#:
//Bad code
DbCommand cmd = database.DbProviderFactory.CreateCommand();
DbParameter id = cmd.CreateParameter();
id.DbType = System.Data.DbType.Int32;
id.ParameterName = "@CustomerId";
id.Value = guid;

cmd.Parameters.Add(id);

int i = 0;
try
{
//Now for the SQL.
cmd.CommandText = "delete from Customer where " +
"customerId = @CustomerId";
i = database.ExecuteNonQuery(cmd, null);
}
catch (SqlCeException)
{
throw;
}
finally
{
cmd.Dispose();
}
The above is quite ugly you probably agree or should agree :) Imagine though that there are loads of methods doing all sorts of complex stuff. It would be a nightmare to maintain that SQL. In addition how would you run that SQL on the desktop if you wanted to script stuff. Imagine also that you might have a method that creates the database from scratch, now that would be ugly in code.

The solution is to abstract the SQL out of the code into resource files containing *.sql scripts that your code calls. The added bonus with this pattern is it allows you to localize the SQL too if you wanted.

Take the good code:
//Good code
DbCommand cmd = connection.DbProviderFactory.CreateCommand();
DbParameter id = cmd.CreateParameter();
id.DbType = System.Data.DbType.Int32;
id.ParameterName = "@CustomerId";
id.Value = customerId;

cmd.Parameters.Add(id);

try
{
//Now for the SQL.
cmd.CommandText = Properties.StoredProcs.Customer_Del_ByCustomerId;
connection.ExecuteNonQuery(cmd, null);
}
catch (SqlCeException)
{
throw;
}
finally
{
cmd.Dispose();
}
Now the SQL looks like the following:
DELETE FROM Customer
WHERE CustomerId = @CustomerId
If you wanted to localize it, we simply add a satalite resource with another locale, easy. Of course depending on what you are doing the scripts will port over to the desktop.

Thursday, May 15, 2008

Dropping Defaults (Column Constraint) in SQL Compact 3.5

This interesting post talks about the DEFAULT SQL statement is no longer a database constraint in SQL Compact 3.5. You merely use just DEFAULT.

To remove a default simply use SQL syntax: ALTER TABLE table ALTER COLUMN col DROP DEFAULT. This has always been the recomended way to remove defaults.

See here for more info: http://blogs.msdn.com/sqlservercompact/archive/2008/04/03/dropping-defaults.aspx

Wednesday, April 30, 2008

SQL Server CE 3.0 version inconsistencies

Many people in the community are reporting inconsistencies when viewing the version of SQL Server CE 3.0 through File Explorer file properties and Visual Studio Add Reference dialog.

I use SQL Server Compact 3.5 (note the naming differences) now which seems to be OK in terms of versioning. But earlier versions (3.0, 3.1) don't. See the screen shots attached.


As per Visual Studio.


As per the file system.

This diference is related to the build process between Mobile and Desktop assemblies. Microsoft has partially fixed SQL Server CE 3.1 but fully fixed SQL Server Compact 3.5.

Tuesday, April 29, 2008

Develop SQL Server Compact 3.5 apps with VS 2005

UPDATE 01 June 08: This is quite confusing but you can use VS 2008 RTM to manipulate a SQL Server Compact 3.5 database but not SQL Server 2008 database unless you install the newly released VS 2008 Beta Service Pack 1: http://simonrhart.blogspot.com/2008/05/visual-studio-2008-and-net-framework-35.html

I see this question asked all the time in the community, "Can I use SQL Server Compact 3.5 with Visual Studio 2005" the answer is Yes.

Although it is a little bit ugly, but does work. You have to explicitly add the SQL Server Compact manually by clicking on the Browse tab in Add Reference dialog if it doesn't exist in the .NET tab. Of course the limitations are not being able to use Server Explorer in VS 2005 for v3.5 databases and any UI tools that help with data binding etc (as if anyone actually uses these for corporate LOB apps!).

You can use SQL Server Management Studio 2008 to manipulate the SQL Compact 3.5 database or if you haven't got SQL Server 2008 as SQL Server 2008 is not in RTM yet then you can use Visual Studio 2008 Server Explorer to do this.

You can find SQL Server 3.5 engine here(or similar): C:\Program Files (x86)\Microsoft SQL Server Compact Edition\v3.5\Devices.

See here for the reasons why you might want to move to SQL Server Compact 3.5 and how to get it.

Sunday, December 02, 2007

Microsoft SQL Server Compact 3.5 Available

As released with Visual Studio 2008, SQL Server Compact 3.5 is now available in RTM. It is free to use as long as you're not using any type of replication or RDA (remote data access) with a back office SQL Server. Get it here.

SQL Server Compact 3.5 runs side-by-side with SQL Server 2005 Compact Edition (3.0/3.1).

Notice the name change, again ;)

SQL Server 3.5 contains some enhancements such as:

  1. Timestamp datatype - at last! ;)
  2. Transact-SQL enhancements such as:
    1. Nested query in FROM clause
    2. CROSS APPLY and OUTTER APPLY
    3. CASE and DECIMAL
    4. SET IDENTITY INSERT
    5. TOP


SQL Server Compact 3.5 runs under the .NET Compact Framework 2.0. So although it was released at the same time as Visual Studio 2008 and with Visual Studio 2008, you don't actually need VS 2008 or the .NET CF 3.5 to use it. You can use it within your Visual Studio 2005 projects today if you want to.

Read about all the new features here.

This release is supported on Pocket PC 2003 as well as the later devices, WM5, WM6.

Thursday, November 15, 2007

SQL Server CE Performance Tips

UPDATE: This is a very good article: http://www.sql-server-performance.com/tips/sql_server_ce_p1.aspx

As there have been a few questions recently in the communities regarding Microsoft SQL Server CE performance, I'd thought I'd post the following link which contains some useful information regarding SQL Server CE performance tips: http://support.microsoft.com/default.aspx/kb/274112

Also see here for information of changing the default temp table directory that SQL Server CE uses when processing transactions or large queries from RAM to a storage card which will also improve performance and reduce those annoying OutOfMemoryException exception's.

Also try using the DataReader class over the DataSet and ResultSet wherever possible if you don't need the scrollability, updateable and a bindable cursor as the DataReader will always be more efficient. I myself always use DataReader which populates a strongly typed collection which I can then use to bind to my controls where required.

You might also find this link interesting, written by Chris Tacke of OpenNETCF. It talks about data caching in SQL Server CE to improve performance.