Just a quick note the let you know, that SQL Server Compact will be exposed to Windows Phone developers as announced at the MIX11 conference. It is already on the device, as I blogged about earlier. The Windows Phone Developer blog has an overview of what is included for developers in the update, expected to be available this year. The developer tools update will appear next month, and I will blog more details about the API and other findings. The database will not be exposed via ADO.NET, as System.Data is not available in Silverlight, but the database will be accessible via LINQ. More details to follow when the API is published, and I have had a play with the developer tools. This MIX11 presentation demonstrates some of the details of working with SQL Server Compact on Windows Phone via Linq and DataContext only.
SQL Server Compact will be available for developers in upcoming Windows Phone “Mango” update
Saving images to SQL Server Compact with Entity Framework 4.1 Code First
This StackOverflow question points out an issue with EntityFramework 4.1, when used with a SQL Server Compact table with image columns. The “image” type is the predecessor to varbinary(MAX), and is used for storing large binary values, sometimes referred to as BLOBs.
I have created a sample Console application with fixes for the related issues.
To re-create the application, install EntityFramwork 4.1, create a C# Windows Console application in Visual Studio 2010, and add .NET references to:
System.Data.Entity
EntityFramework
System.ComponentModel.DataAnnotations
Then add code similar to this:
using System;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
//Also added reference to EntityFramework.dll (EF 4.1)
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace CfTest
{
class Program
{
static void Main(string[] args)
{
using (var db = new StudentContext())
{
var student = new Student { Name = "Erik", Photo = ConvertImageToByteArray(@"C:\Users\Erik\Pictures\erik.jpg"), StudentId = 1 };
student.LongText = new string('x', 6666);
db.Students.Add(student);
int recordsAffected = db.SaveChanges();
Console.WriteLine(
"Saved {0} entities to the database, press any key to exit.",
recordsAffected);
Console.ReadKey();
}
}
private static byte[] ConvertImageToByteArray(string fileName)
{
Bitmap bitMap = new Bitmap(fileName);
ImageFormat bmpFormat = bitMap.RawFormat;
var imageToConvert = Image.FromFile(fileName);
using (MemoryStream ms = new MemoryStream())
{
imageToConvert.Save(ms, bmpFormat);
return ms.ToArray();
}
}
}
public class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
// Required to force Code First to create a ntext column, not a nvarchar(n)
[Column(TypeName = "ntext")]
public string LongText { get; set; }
// Required to force Code First to create an image column, not a binary(n)
[Column(TypeName = "image")]
public byte[] Photo { get; set; }
}
public class StudentContext : DbContext
{
protected override bool ShouldValidateEntity(System.Data.Entity.Infrastructure.DbEntityEntry entityEntry)
{
// Required to prevent bug - http://stackoverflow.com/questions/5737733
if (entityEntry.Entity is Student)
{
return false;
}
return base.ShouldValidateEntity(entityEntry);
}
public DbSet<Student> Students { get; set; }
}
}
And add an app.config with contents like this. The connection string name matches the DbContext name and this causes Code First to magically create a database file in the specified location, with a Students table:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="StudentContext"
providerName="System.Data.SqlServerCe.4.0"
connectionString="Data Source=C:\Users\Erik\Documents\visual studio 2010\Projects\CfTest\Students.sdf"/>
</connectionStrings>
</configuration>
This is the resulting Students table:
-- Script Date: 11-07-2011 09:43 - Generated by ExportSqlCe version 3.5.1.3
CREATE TABLE [Students] (
[StudentId] int NOT NULL IDENTITY (1,1)
, [Name] nvarchar(4000) NULL
, [LongText] ntext NULL
, [Photo] image NULL
);
GO
ALTER TABLE [Students] ADD CONSTRAINT [PK__Students__000000000000000A] PRIMARY KEY ([StudentId]);
GO
This code demonstrates how to convert an image to a byte array, and also highlights the fixes required to work with image columns:
// Required to force Code First to create an image column, not a binary(n)
[Column(TypeName = "image")]
public byte[] Photo { get; set; }
The Photo column must be decorated with the Column attribute specifying a typeName of “Image.
// Required to force Code First to create a ntext column, not a nvarchar(n)
[Column(TypeName = "ntext")]
public string LongText { get; set; }
Likewise the LongText column must specify “ntext” as typename.
public class StudentContext : DbContext
{
protected override bool ShouldValidateEntity(System.Data.Entity.Infrastructure.DbEntityEntry entityEntry)
{
// Required to prevent bug - http://stackoverflow.com/questions/5737733
if (entityEntry.Entity is Student)
{
return false;
}
return base.ShouldValidateEntity(entityEntry);
}
public DbSet<Student> Students { get; set; }
}
And validation must be disabled to prevent an error message saying:
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. This issue is described by the ADO.NET Team here.
New release of Scripting Library and command line utilities–with Schema Diff and DGML from command line
The latest release of my ExportSqlCe SQL Server Compact scripting library and related command line utilities is now available on CodePlex.
This latest version of the command line utilities adds the capability to generate Schema Diff and DGML database graph files.
The schema diff option allows you to compare a SQL Server Compact database file with another SQL Server Compact database file or even a SQL Server database, and creates a script with the required ALTER TABLE etc. statements to synchronize the 2 database schemas.
The DGML option allows you to create a graphical view of the database tables and fields, the resulting .dgml file requires Visual Studio 2010 Premium or higher to be viewed. I blogged about DGML files earlier:
http://erikej.blogspot.com/2010/04/diagram-database-table-relationships.html
http://erikej.blogspot.com/2010/08/sql-server-compact-35-toolbox-updated.html
The latest documentation for the command line utilities is available here.
And both these file types can of course be generated from your own application, using the scripting library. I have some code samples available here.
SQL Server Compact Toolbox standalone - including all managed DLLs in a single .exe
The latest release of the standalone version of my SQL Server Compact Toolbox, mainly for users that do not have Visual Studio 2010 Pro or higher, is available as a single .exe. It was actually a Tweet from @scottgal, that pointed me towards this excerpt from Jeffery Richters’ CLR via C#, Third Edition.
In order to implement in the WPF application, that is the standalone Toolbox, I added the following code to App.xaml.cs (and a Startup handler to App.xaml):
private void Application_Startup(object sender, StartupEventArgs e)
{
AppDomain.CurrentDomain.AssemblyResolve += (ssender, args) =>
{
//string[] names = this.GetType().Assembly.GetManifestResourceNames();
String resourceName = "ErikEJ.SqlCeToolbox." +
new AssemblyName(args.Name).Name + ".dll";
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
};
}
I also added all the Managed libraries that the Toolbox uses as Embedded Resources.
I use the following libraries, all from CodePlex:
WPF Property Grid
http://wpg.codeplex.com/ (for the SqlCeConnectionStringBuilder)
Sample usage
KBCsv
http://kbcsv.codeplex.com/ (for .csv file import)
Sample usage
SQL Server Compact Scripting Library (for database scripting)
http://exportsqlce.codeplex.com/
Sample usage Sample usage
FabTab WPF Tab Control (the SQL Editor tabs)
http://fabtab.codeplex.com/
Sample usage
And the Microsoft Data Connection Dialog (to prompt fro a SQL Server Connection) from http://archive.msdn.microsoft.com/Connection
Sample usage
Hope you find this tip useful.
Scripting image column data for INSERT statements
I earlier blogged about scripting datetime values for INSERT statements, or for example by use in the SubmitSQL method of the SQL Server Compact RemoteDataAccess API. In this post, I will show how to serialize byte arrays as a hexadecimal string. Notice that for the SubmitSQL method, there is a limit to the size of the SQL statement (not sure what is is, but 64 K is a good guess, I think). So if you have large images, that you want to send to your server, you are out of luck with this method.
Below is the code I currently have implemented in my ExportSqlCE INSERT statement generator.
if (dt.Columns[iColumn].DataType == typeof(Byte[]))
{
Byte[] buffer = (Byte[])dt.Rows[iRow][iColumn];
_sbScript.Append("0x");
for (int i = 0; i < buffer.Length; i++)
{
_sbScript.Append(buffer[i].ToString("X2", System.Globalization.CultureInfo.InvariantCulture));
}
}
Notice the special “X2” string format, this is what performs the magic of creating the hex string. Prepending the string with 0x is required by SQL Server. (_sbScript is a StringBuilder)
There is a long discussion on Stackoverflow on how to do this faster, the code below seems to be the fastest:
private static string ByteArrayToHex(byte[] barray)
{
char[] c = new char[barray.Length * 2];
byte b;
for (int i = 0; i < barray.Length; ++i)
{
b = ((byte)(barray[i] >> 4));
c[i * 2] = (char)(b > 9 ? b + 0x37 : b + 0x30);
b = ((byte)(barray[i] & 0xF));
c[i * 2 + 1] = (char)(b > 9 ? b + 0x37 : b + 0x30);
}
return new string(c);
}
Maybe I should update my ExportSqlCe code? - Happy scripting!
SQL Server Compact ASP.NET Membership, Role and Profile Provider version 2.1 now available
My ASP.NET membership provider is now available in version 2.1, that contains many improvements and some new features based on excellent community feedback – keep it coming!
The ASP.NET membership provider project was prompted last July by the comments to Scott Gu’s blog post about the upcoming version 4.0 of SQL Server Compact, and it’s support for ASP.NET.
Basically the Gu said: “We are looking to potentially ship a set of providers that work with it (and do not use stored procedures). The first beta won't have this - but it is something we'll hopefully enable in the future.”
So it was time to start coding, since the absence of a Membership provider would make SQL Server Compact less of an attractive option for ASP.NET web sites.
Since then, the database schema used has been refactored to be in line with the ASP.NET 4.0 SQL Server based schema, which resulted in the first NuGet Package being released in January 2011.
Now version 2.1 is available, also via NuGet:
Or from the CodePlex site.
The new features in version 2.1 are:
Profile provider included (contrib davidsk)
Two new methods: UpdateUserName and MigrateMembershipDatabaseToAspNet40 (contrib nekno)
Bug fixes (by various contributors, thank you all):
UpdateUser() doesn't set LoweredEmail
GetUser w/ providerUserKey returns invalid information
Static salt leads to deterministic output, dynamic salt is better
Configuration error when using a provider
Populating a Windows Phone “Mango” SQL Server Compact database on desktop
For up to the minute news, outcries, complaints and cheers on SQL Server Compact, follow me on Twitter: @ErikEJ
If you want to prepopulate a Mango SQL Server Compact database with some data, you can use the following procedure to do this. (Notice, that the Mango tools are currently in beta)
First, define your data context, and run code to create the database on the device/emulator, using the CreateDatabase method of the DataContext. See a sample here. This will create the database structure on your device/emulator, but without any initial data.
Then use the Windows Phone 7 Isolated Storage Explorer to copy the database from the device to your desktop, as described here.
You can now use any tool, see the list of third party tools on this blog, to populate your tables with data as required. The file format is version 3.5. (not 4.0)
Finally, include the pre-populated database in your WP application as an Embedded Resource.
UPDATE: I was made aware, that for read only data, you can just include the database file as Content (not Embedded Resource), and it will be available from the special appdata: URI, with a connection string like the following:
“Data Source=appdata:/Chinook.sdf;Mode=Read Only”
So no need to run code to extract from an Embedded resource as below in that case.
You can then use code like the following to write out the database file to Isolated Storage on first run:
public class Chinook : System.Data.Linq.DataContext
{
public static string ConnectionString = "Data Source=isostore:/Chinook.sdf";
public static string FileName = "Chinook.sdf";
public Chinook(string connectionString) : base(connectionString) { }
public void CreateIfNotExists()
{
using (var db = new Chinook(Chinook.ConnectionString))
{
if (!db.DatabaseExists())
{
string[] names = this.GetType().Assembly.GetManifestResourceNames();
string name = names.Where(n => n.EndsWith(FileName)).FirstOrDefault();
if (name != null)
{
using (Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name))
{
if (resourceStream != null)
{
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(FileName, FileMode.Create, myIsolatedStorage))
{
using (BinaryWriter writer = new BinaryWriter(fileStream))
{
long length = resourceStream.Length;
byte[] buffer = new byte[32];
int readCount = 0;
using (BinaryReader reader = new BinaryReader(resourceStream))
{
// read file in chunks in order to reduce memory consumption and increase performance
while (readCount < length)
{
int actual = reader.Read(buffer, 0, buffer.Length);
readCount += actual;
writer.Write(buffer, 0, actual);
}
}
}
}
}
}
else
{
db.CreateDatabase();
}
}
}
else
{
db.CreateDatabase();
}
}
}
}
}
SQL Server Compact Toolbox 2.2–Visual Guide of new features
After more that 32.000 downloads, version 2.2 of my SQL Server Compact Toolbox extension for Visual Studio 2010 is now available for download. This blog post is a visual guide to the new features included in this release, many suggested by users of the tool via the CodePlex issue tracker
Generate an Windows Phone DataContext (.cs) in the current project (beta feature)
This feature allows you to create a DataContext for use with Windows Phone “Mango” projects, based on an existing SQL Server Compact 3.5 database.
This will be a huge timesaver, and allows you to reuse the effort you may already have put in creating your database schema, including foreign keys and the required indexes. It will also save you much repetitive code, as the generated classes reflect your tables 1:1.
This technique even works with Northwind.sdf, despite object names with spaces. None of this is (of course) supported in any way by Microsoft.
Under the covers, I am using SQLMetal to generate a desktop DataContext class, and then this class is enhanced to work well in a Windows Phone project.
Advantages of this approach:
- Use desktop database tools for data population and schema design
- Saves time doing 1:1 mapping between database tables and DataContext classes
- DataContext classe and entity classes are partial and can be expanded
- Invalid DataContext constructors removed
- The generated DataContext contains Index definitions (which SqlMetal does not support, as this is a Windpows Phone extension)
- The generated DataContext contains the CreateIfNotExists method, that optionally extracts an included database (prepopulated with data) to Isolated Storage
If you also would like to include your desktop database with your XAP, the generated DataContext contains the code from this blog post and supports the procedure described in the blog post.
Please provide any feedback for this beta feature to the CodePlex issue tracker.
Add Description to tables and columns
On Database, Table and Column level, there is a new menu item: Edit Description, that allows you to enter a object description. The object description are stored in a table named __ExtendedProperties in the current database.
The description is then shown as a tooltip for the object:
In addition, you can reuse the data for documentation etc. (I am planning a documentation feature in a future release)
Options dialog for saving options from session to session
A new options dialog is available from the main toolbar:
SQL Editor improvements
The SQL Editor (again) allows you to display the results in a Grid, rather than text. This can be selected via the Options above. (Using grid will be slower and requires more memory). Also, a Save Script button has been added.
Other minor improvements
Latest scripting libraries included, with improvements to Db Diff and primary keys with multiple columns
Thanks to all Toolbox users for your continued encouragement and feedback!
SQL Server Compact Private Deployment tweaks
As a follow up to my previous post about Private Deployment (the concept that you can simply include the SQL Server Compact DLL files with your application as content, so to speak), I will show you a couple of tweaks that the .NET Framework enables.
Forcing an existing application to use the private DLL files
If you have an existing application, that is compiled against the centrally deployed DLL files, for example assembly version 3.5.1.0 or 4.0.0.0, you can force the application to use the private assembly version files instead (3.5.1.50 and 4.0.0.1), via an application configuration file. Lets take ExportSqlCe40.exe as an example. This application is complied against assembly version 4.0.0.0, so it will not work unless SQL Server Compact 4.0 runtime is centrally installed.
To force this application to use Private Deployment only, create a .config file named ExportSqlCe40.exe.config, with the following contents:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Data.SqlServerCe" publicKeyToken="89845dcd8080cc91" />
<bindingRedirect oldVersion="4.0.0.0" newVersion="4.0.0.1" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
If you run the application now, you will get this error:
Now copy all files from the C:\Program Files\Microsoft SQL Server Compact Edition\v4.0\Private folder to the folder where the Exportsqlce40.exe file resides:
Now the application runs, and uses only the private DLL files.
Isolating the SQL Server Compact runtime files in a separate folder
Continuing the sample above, to be neater, it would be nice to have the SQL Server Compact DLL files in a subfolder below the .exe file location. This can be done by moving the files to a separate folder, for example named SqlCe4. Now I have moved all the SQL Server Compact files and folders to that folder:
Now modify the .config file as follows:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="SqlCe4"/>
<dependentAssembly>
<assemblyIdentity name="System.Data.SqlServerCe" publicKeyToken="89845dcd8080cc91" />
<bindingRedirect oldVersion="4.0.0.0" newVersion="4.0.0.1" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Notice the Probing setting above, that has been added. Also notice that this comes before the bindingRedirect.
Hop you find this useful.
Windows Phone / SQL Server Compact resources
This blog post collects links to relevant blog posts and articles about Windows Phone support for SQL Server Compact. You can also follow me on Twitter (@ErikEJ) to get notified of any SQL Server Compact related news.
MSDN
Local Database Overview for Windows Phone
How to: Create a Basic Local Database Application for Windows Phone
How to: Create a Local Database Application with MVVM for Windows Phone
How to: Deploy a Reference Database with a Windows Phone Application
Local Database Best Practices for Windows Phone
Local Database Connection Strings for Windows Phone
Local Database Migration Overview for Windows Phone
LINQ to SQL Support for Windows Phone
Video: SQL Server Compact and User Data Access in Mango
PowerPoint slides: SQL Server Compact and User Data Access in Mango
Video + PPT: New Data Access Features Coming to Windows Phone
Windows Phone Mango Application Storage Jumpstart PDF
Jesse Liberty
Coming In Mango–Local DB Part 2- Relationships
Best Practices For Local Databases
Yet Another Podcast #43–Sean McKenna and Windows Phone Data
Sean McKenna and Windows Phone "Mango" Database Support
Rob Tiffany
New Windows Phone Mango Data Access Features @ Tech Ed North America 2011
Alex Golesh
Windows Phone Mango–What’s New? (“Local Database” - Part 1 of 8)
Windows Phone Geek
Windows Phone Mango Local Database- mapping and database operations
Using SqlMetal to generate Windows Phone Mango Local Database classes
Performance Best Practices: Windows Phone Mango Local Database
Windows Phone Mango Local Database(SQL CE): Introduction
Windows Phone Mango Local Database(SQL CE): Linq to SQL
Windows Phone Mango Local Database(SQL CE): [Table] attribute
Windows Phone Mango Local Database(SQL CE): [Column] attribute
Windows Phone Mango Local Database(SQL CE): [Association] attribute
Windows Phone Mango Local Database(SQL CE): Database mapping
Windows Phone Mango Local Database(SQL CE): DataContext
Windows Phone Mango Local Database(SQL CE): Connection Strings
Windows Phone Mango Local Database(SQL CE): Creating the Database
Windows Phone Mango Local Database(SQL CE): Database Queries with LINQ
Windows Phone Mango Local Database(SQL CE): How to Insert data
Arsahnt
Distributing a SQL CE database in a WP7 Mango application
Windows Phone 7 SQL CE – Column inheritance
Windows Phone 7 SQL CE – DataContext Tables
Working with pre-populated SQL CE databases in WP7
LINQ to SQL CE performance tips for WP7
How To: Log LINQ to SQL activity on WP7
Kunal Chowdhury
Windows Phone 7 (Mango) Tutorial - 22 - Local Database Support, Create DataContext
Windows Phone 7 (Mango) Tutorial - 23 - Local Database Support, Configuring Project
Windows Phone 7 (Mango) Tutorial - 24 - Local Database Support, CRUD operation with Demo
Windows Phone 7 (Mango) Tutorial - 25 - Learn about Database Connection String
ErikEJ
Populating a Windows Phone “Mango” SQL Server Compact database on desktop
SQL Server Compact Toolbox 2.2–Visual Guide of new features
Sergey Barskiy
SQL CE on Windows Phone 7.1 (Mango)
SQL CE in Mango–Updating the Schema
Derik Whittaker
Using SQL CE on WP7 Mango–Getting Started
Using SQL CE On WP7 Mango–Working with Associations
Using SQL CE On WP7 Mango–Working with Indexes
Mark Artega
Windows Phone and Database Support
Rabeb
Mango- Baby Steps: Creating an application with a local Database
JeffCren
Matt Lacey
Simplifying use of SQL CE in Mango
Corrado
Using Local Database in WP7-Mango
Max Paulousky
Windows Phone (Mango) DB Engines Performance Testing
Nick Randolph
Change Tracking with SQL Server Compact (LINQ to SQL) on Windows Phone
Windows Phone LINQ to SQL and the INotifyPropertyChanged and INotifyPropertyChanging Interfaces
Chris Sainty
Rafa Serna
Performance Improvements in SQL CE environments – I
Debug mode
Local Database application for windows Phone
Shazaml Design, LLC
More Complete WP7 Mango Database Update Walkthrough
Jerry Nixon
Mango Sample: Database Part 1:2
Mango Sample: Database Part 2:2
Using PowerShell to manage SQL Server Compact database files
I noticed that someone asked if you could manage SQL Server Compact from PowerShell, and yes you can, based on the reply here I put together the following sample:
[Reflection.Assembly]::LoadFile(“C:\Program Files\Microsoft SQL Server Compact Edition\v3.5\Desktop\System.Data.SqlServerCe.dll”)
$connString = "Data Source=C:\data\sqlce\test\roads.sdf"
$cn = new-object "System.Data.SqlServerCe.SqlCeConnection" $connString
# create the command
$cmd = new-object "System.Data.SqlServerCe.SqlCeCommand"
$cmd.CommandType = [System.Data.CommandType]"Text"
$cmd.CommandText = "SELECT TOP (100) * FROM Road"
$cmd.Connection = $cn
#get the data
$dt = new-object "System.Data.DataTable"
$cn.Open()
$rdr = $cmd.ExecuteReader()
$dt.Load($rdr)
$cn.Close()
$dt | Out-Default | Format-Table
SQL Server Compact Toolbox 2.3–Visual Guide of new features
After more than 44.000 downloads, version 2.3 of my SQL Server Compact Toolbox extension for Visual Studio 2010 is now available for download. This blog post is a visual guide to the new features included in this release
Generate database documentation
This feature allows you to create documentation of all tables and columns in your database, in HTML or XML (raw) format, for use with product documentation etc. If you have added descriptions to database, table or column, these will also be included.
From the database context menu, select Create Database Documentation…
You will be prompted for a filename and can choose between HTML and XML format. The generated document will then open in the associated application (for example your browser).
The format of the HTML and XML file comes from the excellent DB>doc for Microsoft SQL Server CodePlex project. You can use the XML file as the data in your own documentation format.
By default, tables beginning with __ are not included in the documentation (this includes the table with object descriptions). They can optionally be included via a new option:
Please provide any feedback for this new feature to the CodePlex issue tracker
Handle password protected files better
When trying to open a password protected file, where the password is not saved with the connection string, you are now prompted to enter the database password, instead of being faced with an error.
Show result count in status bar
The query editor status bar now displays the number of rows returned.
Other fixes
Improvements to Windows Phone DataContext generation, improved error handling to prevent Visual Studio crashes, and the latest scripting libraries included.
Viewing SQL statements created by Entity Framework with SQL Server Compact
Sometimes it can be useful to be able to inspect the SQL statements generated by Entity Framework against your SQL Server Compact database. This can easily be done for SELECT statements as noted here. But for INSERT/UPDATE/DELETE this method will not work. This is usually not a problem for SQL Server based applications, as you can use SQL Server Profiler to log all SQL statements executed by an application, but this is not possible with SQL Server Compact.
This forum thread contains an extension method, that allows you to log INSERT/UPDATE/DELETE statements before SaveChanges is called on the ObjectContext. I have updated and fixed the code to work with SQL Server Compact 4.0, and it is available in the updated Chinook sample available below in the ObjectQueryExtensions class in the Chinook.Data project.
You can now use code like the following to inspect an INSERT statement:
using (var context = new Chinook.Model.ChinookEntities())
{
context.Artists.AddObject(new Chinook.Model.Artist { ArtistId = Int32.MaxValue, Name = "ErikEJ" });
string sql = context.ToTraceString();
}
The “sql” string variable now contains the following text:
--=============== BEGIN COMMAND ===============
declare @0 NVarChar set @0 = 'ErikEJ'
insert [Artist]([Name])
values (@0)
; select [ArtistId]
from [Artist]
where [ArtistId] = @@IDENTITY
go
--=============== END COMMAND ===============
This statement reveals some of the magic behind the new support for “server generated” keys with SQL Server Compact 4.0 when used with Entity Framework 4.0. SQL Server Compact is “tricked” into executing multiple statements in a single call.
Major update to SQL Server Compact 3.5 SP2 available
A major update to SQL Server Compact 3.5 SP2 has just been released, disguised as a “Cumulative Update Package”. Microsoft knowledgebase article 2553608 describes the update. The update contains the following product enhancements:
Support for Windows Embedded CE 7.0
The update contains updated device components. This expand the supported device platforms to this impressive list: Pocket PC 2003 Software, Windows CE, Windows Mobile 5.0, Windows Mobile 6, Windows Mobile 6.1 , Windows Mobile 6.5 Professional, Windows Mobile 6.5 Standard, Windows Embedded CE 7.0
Support for Merge Replication with SQL Server “Denali” CTP3
The update contains new Server Tools, that support Merge Replication with the next version of SQL Server, codename “Denali”. The replication components also work with Windows Embedded CE 7.0.
For a list of fixes in the Cumulative Updates released for SQL Server Compact 3.5 SP2, see my blog post here.
It is nice to see that the 3.5 SP2 product, with it’s full range of device support and synchronization technologies is kept alive and kicking.
NOTE: Currently, the only download available is the desktop runtime, I will update this blog post and tweet (@ErikEJ) when the other downloads are available.
SqlCeBulkCopy, a library for fast SQL Server Compact INSERTS released
Version 2.1 of my SQL Server Compact Bulk Insert Library has now been released. This library exposes an API similar to the SqlBulkCopy API implemented for the SqlClient (working against SQL Server). The library allows you to quickly load data inot a SQL Server Compact database.
New features in this release include:
3 editions of the library:
One for .NET Compact Framework for version 3.5 databases - ErikEJ.SqlCe.NetCF.dll
One for full .NET Framework for version 3.5 databases - ErikEJ.SqlCe.dll
One for full .NET Framework for version 4.0 databases - ErikEJ.SqlCe40.dll
- New overloads of the WriteToServer method allows you to load any .NET list that implements IEnumerable or IEnumerable<T>
- API Documentation is now available in HTML format here. The API documentation was created very easily using the open source ImmDoc.NET utility. All this command line utility requires is XML comments file and the DLL file(s).
- NuGet package available
A NuGet package, that includes the SQL Server Compact 4.0 library is now available via the NuGet Package Manager.
If you need to load data fast from a DataTable, DataReader or .NET List to a SQL Server Compact database file, this is the library for you.
Useful Windows Phone advice from Nick Randolph
Fellow MVP Nick Randolph (@BTRoam) publishes an excellent blog, Nick’s .NET Travels, often with articles that relate to SQL Server Compact and synchronization technologies, but also very useful articles for any Windows Phone developer, with a practical, hands-on approach. Highly recommended.
He has recently published the following articles:
Windows Phone LINQ to SQL and the INotifyPropertyChanged and INotifyPropertyChanging Interfaces
This article demonstrates the importance of implementing the INotifyPropertyChanging interface on your DataContext classes to improve memory management. As he points out, you should use SQLMetal to generate your DataContext, as this will avoid missing to implement these interfaces. Or even better use the SQL Server Compact Toolbox Visual Studio add-in, as it adds the following features on top of SQLMetal:
1. Removes unneeded/unsupported constructors
2. Adds any [Index] attributes to each table
3. Adds the CreateDatabaseIfExists method
4. Optionally splits the generated files into a file per table (in next version (2.4), currently available in beta)
Change Tracking with SQL Server Compact (LINQ to SQL) on Windows Phone
This article demonstrates how to get started using SQL Server Compact Change Tracking with a Windows Phone SQL Server Compact database, despite the fact that the Change Tracking APIs are not available on Windows Phone. He also gets thrown in how to use the Windows Phone SDK ISETool to move the database from the Phone (Emulator) to your local disk. Good stuff.
SQL Server Compact Toolbox 2.4–Visual Guide of new features
After more than 50.000 downloads, version 2.4 of my SQL Server Compact Toolbox extension for Visual Studio 2010 is now available for download. This blog post is a visual guide to the new features included in this release.
Edit Table Data (beta)
The tools that are included with Server Explorer for SQL Server Compact 3.5 and 4.0 already include a feature to edit table data, called Show Table Data:
But the grid has some limitations that I have lifted on the new “Edit Table Data” feature:
- Ability to sort data by clicking a column heading
- Ability to Import/Export/Delete content of image columns
- Ability to locate data in a column (QuickSearch)
Split Windows Phone DataContext into multiple files
You can now select to have the Windows Phone DataContext generated as a DataContext class file, and a class file per table in your database.
Select tables to include in Entity Data Model
You can now select which tables to include in the Entity Data Model created from your SQL Server Compact database. (Thanks to the Extended WPF Toolkit Project)
Explore and script primary and foreign keys
The tree view now lists the primary and foreign keys belonging to a table, and it is also possible to script these individually.
Other fixes
Improved Add-in Update detection (for users behind proxies)
Improved handling of password protected files for non-English SQL Compact runtime
Some icons were not transparent
CreateDataIfExists (Windows Phone DataContext) now returns bool if database was created
Latest scripting library, with fix for missing SET IDENTITY INSERT with multiple files (from large tables)
SQL Server Compact Toolbox available for Visual Studio 11
Visual Studio 11 Developer Preview is now available for testing. As one of the first third party add-ins, a build of the SQL Server Compact Toolbox version 2.4 that supports this Visual Studio Preview version is available via Extension Manager or in the Visual Studio Gallery.
In order to add support for Visual Studio version 11 in an existing add-in, all you need to do is modify the source.extension.vsixmanifest file as shown below:
<SupportedProducts>
<VisualStudio Version="10.0">
<Edition>Pro</Edition>
</VisualStudio>
<VisualStudio Version="11.0">
<Edition>Pro</Edition>
</VisualStudio>
</SupportedProducts>
The result of this change is that the add-in can now be installed for several versions of Visual Studio.
I have had to make some changes, as the Toolbox currently depends on SQL Server Compact 3.5 SP2 to store it’s connections, and only SQL Server Compact 4.0 is included with Visual Studio 11. In the Developer Preview the version of SQL Server Compact included is the 4.0 RTM version, so no changes there for now.
To detect which version of Visual Studio you are running, you can use the following code in your Package.cs class:
public Version VisualStudioVersion
{
get
{
var dte = this.GetServiceHelper(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
string root = dte.RegistryRoot;
if (root.Contains("10.0"))
{
return new Version(10, 0);
}
else if (root.Contains("11.0"))
{
return new Version(11, 0);
}
else
{
return new Version(0, 0);
}
}
set
{
this.VisualStudioVersion = value;
}
}
I am currently not bringing forward any 4.0 connections defined in the VS 2010 edition of the add-in. Please let me know if a feature to import these connections to the VS 11 Server Explorer would be useful.
Also, would it be of interest to be able to manage 3.5 databases in VS 11, even though they are not supported in Server Explorer?
As always, please provide any feedback in the comments or via the Codeplex issue tracker.
Analyzing SQL Server Compact queries using Visual Studio 2010 Premium/Ultimate
f you are the happy owner of Visual Studio 2010 Premium or Ultimate, there is a hidden tool that allows you to run and analyze queries against SQL Server Compact 3.5 and 4.0 databases. (Support for 4.0 requires Visual Studio 2010 SP1 + the SQL Server Compact Tools update). This blog post will walk through how to access and use this “hidden” tool.
NOTE: If you only have Visual Studio Professional, you can use my SQL Server Compact Toolbox in combination with the free SQL Server 2008 R2 Management Studio Express to perform similar query analysis.
To access the tool, go to the Data menu, and select Transact-SQL Editor, New Query Connection… (The tool is part of the so-called “Data Dude” features)
In the Connect to Server dialog, select SQL Server Compact:
You can select an existing database, or even create a new one. This dialog will automatically detect if the specified file is a version 3.5 or 4.0 file.
Once connected, you can perform functions similar to what you may know from SQL Server Management Studio:
Comparison of SQL Server Compact, SQL Server Express 2008 R2 and LocalDB
Now that SQL Server Compact 4 has been released, some developers are curious about the differences between SQL Server Compact 4.0 and SQL Server Express 2008 R2.
I have updated the comparison table from the excellent discussion of the differences between Compact 3.5 and Express 2005 here to reflect the changes in the newer versions of each product.
Information about LocalDB comes from here and “Denali” CTP3 Books Online. LocalDB is the full SQL Server Express engine, but invoked directly from the client provider. It does not support Fulltext Search, and I doubt it support Merge Replication. It is a replacement of the current “User Instance” feature in SQL Server Express.
UPDATE: Updated data on LocalDB, based on additional information and feedback from the LocalDB Program Manager.
Feature | SQL Server Compact 3.5 SP2 | SQL Server Compact 4.0 | SQL Server | SQL Server ”Denali” LocalDB |
Deployment/Installation Features | ||||
Installation size | 2.5 MB download size | 2.5 MB download size | 74 MB download size | 32 MB download size |
ClickOnce deployment | Yes | Yes | Yes | Yes |
Privately installed, embedded, with the application | Yes | Yes | No | No |
Non-admin installation option | Yes | Yes | No | No |
Runs under ASP.NET | No | Yes | Yes | Yes |
Runs on Windows Mobile / Windows Phone platform | Yes | No | No | No |
Installed centrally with an MSI | Yes | Yes | Yes | Yes |
Runs in-process with application | Yes | Yes | No | No (as process started by app) |
64-bit support | Yes | Yes | Yes | Yes |
Runs as a service | No – In process with application | No - In process with application | Yes | No – as launched process |
Data file features | ||||
File format | Single file | Single file | Multiple files | Multiple files |
Data file storage on a network share | No | No | No | No |
Support for different file extensions | Yes | Yes | No | No |
Database size support | 4 GB | 4 GB | 10 GB | 10 GB |
XML storage | Yes – stored as ntext | Yes - stored as ntext | Yes | Yes |
Binary (BLOB) storage | Yes – stored as image | Yes - stored as image | Yes | Yes |
FILESTREAM support | No | No | Yes | No |
Code free, document safe, file format | Yes | Yes | No | No |
Programmability | ||||
Transact-SQL - Common Query Features | Yes | Yes | Yes | Yes |
Procedural T-SQL - Select Case, If, features | No | No | Yes | Yes |
Remote Data Access (RDA) | Yes | No (not supported) | No | No |
ADO.NET Sync Framework | Yes | No | Yes | Yes |
LINQ to SQL | Yes | No | Yes | Yes |
ADO.NET Entity Framework 4.1 | Yes (no Code First) | Yes | Yes | Yes |
Subscriber for merge replication | Yes | No | Yes | No |
Simple transactions | Yes | Yes | Yes | Yes |
Distributed transactions | No | No | Yes | Yes |
Native XML, XQuery/XPath | No | No | Yes | Yes |
Stored procedures, views, triggers | No | No | Yes | Yes |
Role-based security | No | No | Yes | Yes |
Number of concurrent connections | 256 (100) | 256 | Unlimited | Unlimited (but only local) |
There is also a table here that allows you to determine which Transact-SQL commands, features, and data types are supported by SQL Server Compact 3.5 (which are the same a 4.0 with very few exceptions), compared with SQL Server 2005 and 2008.