Monday, June 7, 2010

“SQL Compact Toolbox” Visual Studio 2010 add-in beta available

I have just published the beta of a new Visual Studio (Professional or higher) add-in, that provides a number of useful tools for SQL Compact developers, directly from the Visual Studio environment:

Scripting of schema and data, ad-hoc query editing and execution, database relationship diagrams, migration/import/export and more features to come in subsequent releases.

After investigating the difficulty of creating a Visual Studio add-in, I stumbled upon the SQL Azure Explorer add-in on CodePlex. This gave me hope that creating a Visual Studio add-in could actually be done within a finite and limited amount of effort and time. This project showcased how to create a Toolwindow, and hook this up with a treeview complete with context menus and even a SQL free-form editor. The add-in uses the new VSIX package format, which greatly simplifies end-user management of extensions to the Visual Studio IDE

To start creating your own add-ins, download the Visual Studio 2010 SDK, and go through the walkthrough on MSDN.

The add-in gets it’s data repository and script generation code on the “API” available in my ExportSqlCe CodePlex project. In the latest source for this project, the “scripting framework” is available a a single DLL file, SqlCeScripting.dll.

The add-in bases it’s SQL Server Compact database connections on the Data Connections that you have defined in the Server Explorer. Below is the code to return a list of SQL Server Compact Data Connection names and related connection strings, based on the connections in Server Explorer:

using Microsoft.VisualStudio.Data;
using Microsoft.VisualStudio.Data.AdoDotNet;
using Microsoft.VisualStudio.Data.Interop;



internal static Dictionary<string, string> GetDataConnections(SqlCe35ToolboxPackage package)
{
// http://www.mztools.com/articles/2007/MZ2007018.aspx
Dictionary<string, string> databaseList = new Dictionary<string, string>();

var objIVsDataConnectionsService = package.GetServiceHelper(typeof(IVsDataConnectionsService)) as IVsDataConnectionsService;
var objIVsDataConnectionManager = package.GetServiceHelper(typeof(IVsDataConnectionManager)) as IVsDataConnectionManager;
if (objIVsDataConnectionsService != null)
{
for (int i = 0; i < objIVsDataConnectionsService.Count; i++)
{
var objProviderGuid = objIVsDataConnectionsService.GetProvider(i);
if (objProviderGuid == package.SqlCompact35Provider)
{
var sConnectionName = objIVsDataConnectionsService.GetConnectionName(i);
var sConnectionString = objIVsDataConnectionsService.GetConnectionString(i);
var objIVsDataConnection = objIVsDataConnectionManager.GetDataConnection(objProviderGuid, sConnectionString, true);
var conn = (DataConnection)objIVsDataConnection;
var adoConn = (AdoDotNetConnectionSupport)conn.ConnectionSupport;
//TODO test for Mobile Connection (using Microsoft.SqlServerCe.Client)
//Hack to get the underlying connection string including password
var ceconn = (System.Data.Common.DbConnection)adoConn.ProviderObject;
databaseList.Add(ceconn.ConnectionString, sConnectionName);
}
}
}
return databaseList;
}

//In the package class:


public object GetServiceHelper(Type type)
{
return GetService(type);
}



In addition to the script related features, the add-in allows you to create DGML documents, that visualize the table relationships of your database (see sample in my earlier blog post).



The following code shows how to open the DGML file from add-in code:



var dte = package.GetServiceHelper(typeof(EnvDTE.DTE)) as EnvDTE.DTE;


using (IRepository repository = new DBRepository(databaseInfo.Connectionstring))
{
Generator generator = new Generator(repository, fileName);
generator.GenerateSchemaGraph();
dte.ItemOperations.OpenFile(fileName);
dte.ActiveDocument.Activate();
}


Here are a few screenshots:



 



toolbox2



The table context menu, and the simple query editor.



 



toolbox1



The database context menu.



 



TOOLBOX3



The root context menu, with SQL Server related menu items.



Please try out the beta, and provide feedback of any kind.

Wednesday, June 2, 2010

SQL Compact Table Copy/Export samples published

João Paulo Figueira has published a couple of useful sample apps (this time in managed code!) on his new “SmilingSmith” blog:

SQL Compact table copy

How do you make a copy of a SQL Compact table? Version 3.5 does not support the SELECT INTO command that allows you to easily do just that. Instead, it provides the INSERT INTO SELECT FROM command that allows you to copy existing table data into another existing table. So we are only missing one part: how to create a replica of the existing table structure.

SQL Compact table export

This is a very simple variation on my last blog post: instead of copying the table to the same database, you can now copy it to another database (although with the same table name, but you can easily change that).

Enjoy!

Sunday, May 30, 2010

How to detect if the x64 SQL Compact 3.5 SP2 desktop runtime is installed

For x64 system with SQL Compact, it is required that both the x86 and x64 runtimes are installed, see this KB for more info.

So how can your installer detect if any of these versions are installed: by looking in the registry.

 

x64 system with only x86 runtime installed:

    [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server Compact Edition\v3.5\ENU]
    "DesktopRuntimeVersion"="3.5.8080.0"
    "DesktopRuntimeServicePackLevel"="2"

 

x64 system with both x86 and x64 installed:

    [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server Compact Edition\v3.5\ENU]
    "DesktopRuntimeVersion"="3.5.8080.0"
    "DesktopRuntimeServicePackLevel"="2"
    "DesktopRuntimeVersion_x64"="3.5.8080.0"

and

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server Compact Edition\v3.5\ENU]
"DesktopRuntimeVersion"="3.5.8080.0"
"DesktopRuntimeServicePackLevel"="2"

 

x64 system with only x64 runtime installed:

This is not possible with SQL Compact 3.5 SP2, attempting this will result in the following blocking error:

x64sqlce

 

Detection

So depending on the target platform (“x86”, “x64” or “AnyCPU”) of your installer, you can detect as follows:

- x86 based installer: 

Both these keys must be present and have proper values in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server Compact Edition\v3.5\ENU

(x86 platform reads the Wow6432Node as if it was the “normal” HKLM\Software node).

    "DesktopRuntimeVersion"="3.5.8080.0"  (x86 runtime installed)
    "DesktopRuntimeVersion_x64"="3.5.8080.0" (x64 runtime installed)

- x64 and AnyCPU based installer:

If the key below is present and have proper values, all is well:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server Compact Edition\v3.5\ENU]
"DesktopRuntimeVersion"="3.5.8080.0" (x64 and x86 runtimes installed)

If not, you must check the key below in HKEY_LOCAL_MACHINE\Wow6432Node\SOFTWARE\Microsoft\Microsoft SQL Server Compact Edition\v3.5\ENU

(to avoid reinstalling the x86 runtime);

    "DesktopRuntimeVersion"="3.5.8080.0" (x86 runtime installed) 

Friday, May 28, 2010

How to clear the list of recently opened SQL Server Compact database files

If you are working with many different SQL Compact files in SQL Server Management Studio, the list of files just grows and grows.

 

image

To clear the list, remove the following file (not sure what other important setting are in the file, so keep a copy):

%appdata%\Microsoft\Microsoft SQL Server\100\Tools\Shell\SqlStudio.bin

Monday, May 24, 2010

FAQ: Why does my changes not get saved, SQL CE does not commit etc.

This is a very frequently asked question in the SQL Compact forum, which I attempted to answer here: http://social.msdn.microsoft.com/forums/en-US/sqlce/thread/e7f7a713-b0fd-4716-a19d-4421e8be4c88/

An excellent approach to dealing with this Visual Studio issue is described by Dean Willson here: http://dean-o.blogspot.com/2010/03/mind-your-copy-to-output-directory.html

What’s new in SQL Server Compact 3.5 SP2 beta

Disclaimer: I am guessing some of this, as no documentation is currently available!

SQL Server Compact 3.5 SP2 beta is available for download for x86 and x64 (no Windows Mobile yet) here: http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=411ba1c5-ba57-45b6-9148-91bed6e7a9f1#tm and is also included with Visual Studio 2010 beta 2. I will try to update this blog entry as more information becomes available.

Notice that the installation of 3.5 SP2 beta upgrades a 3.5 and 3.5 SP1 installation. Also, this is beta software, so use at your own risk!

Firstly, 3.5 SP2 beta includes all the hotfixes, that has been released for 3.5 SP1, as listed by me here.

The SQL Compact 3.5 folder look like this after installation:

clip_image002

Notice the “Private” folder, which contains a version of System.Data.SqlServerCe.Dll which should be used for private deployments, to prevent the “Assembly probing” issue described in this forum thread. The problem is, that even if you are using a hotfixed version of SQL Compact in your application folder, assembly probing will load the GAC deployed version, which may or may not be hotfixed. This means that the deployment of a hotfix always must be done by an administrator, which may not always be possible.

Ravi Tandon from the product team add the following comments about EF related fixes in the beta (from same thread):

“In context of this thread this will alleviate these issues for you:
1. Fixes the ntext error, or any EF queries that use String Parameters. (Hotifx - http://support.microsoft.com/kb/958478/EN-US, earlier released has been rolled up)
2. Fixes the "AS" error. (Same Hotifx had this fix too and has been rolled up)
3. This MSI will do the seamless upgrades. You just need to use this MSI for ClickOnce and any other scenarios. No Uninstalls anymore.
4. We also got some reports of bad performance with EF designers if the Schema get's a bit complex and big. We have made changes for that, which should make the performance better.
As always, we are open for feedback, suggestions, comments.
Thanks a lot for using our product and the continued support!
Cheers
Ravi”

The 3.5 SP2 beta adds a few new APIs in the ADO.NET provider:

clip_image002[5]

 

1: A new property has been added to the SqlCeReplication object (merge replication agent): PostSyncCleanup – it is “documented” here.

2: The SqlCeEngine.Repair methods RepairOptions have changed (documented here):

DeleteCorruptedRows –> DeleteCorruptedRows

RecoverCorruptedRows –> RecoverAllPossibleRows (RecoverCorruptedRows is obsolete)

New Enum value: RecoverAllOrFail

 

clip_image002[8]

An entirely new class is now available, called SqlCeChangeTracking. It exposes methods to programmatically enable, query and disable change tracking, something that was only available to the Visual Studio “Local Data Cache” designer (for Sync Framework applications.) I am looking forward to more documentation on this API!

Hope this helps, and looking forward to the final release of SQL Server Compact 3.5 SP2.

UPDATE: Ravi Tandon has posted even more info here

UPDATE 2: The VS 2010 RC contains a newer build (3.5.8078.0), and includes a readme file, that documents a couple of issues:

1: A ClickOnce application that uses DbProviderFactory to load the SQL Server Compact 3.5 SP2 provider will not work in the following cases:

· A computer already has SQL Server Compact 3.5 SP2 and .NET FX 2.0, 3.0, or 3.5 installed, and the ClickOnce application installs .NET FX 4 and SQL Server Compact 3.5 SP2 on the computer.

· A computer already has SQL Server Compact 3.5 SP2 and .NET FX 4 and installed and the ClickOnce application installs .NET FX 2.0, 3.0, or 3.5 on the computer.

The workaround is to repair the SQL Server Compact 3.5 SP2 installation from Add/Remove Programs.

2: Ensure that if a database is created with SQL Server Compact 3.5 SP2 change tracking is also enabled with SQL Server Compact 3.5 SP2.

3: The SQL Server Compact 3.5 SP2 Query Tools Windows installer file (MSI) is uninstalled when SQL Server 2008 and SQL Server 2008 R2 are installed side-by-side and then SQL Server 2008 is uninstalled.

For more information, see the ReadmeSSCE35_ENU.htm in the C:\Program Files\Microsoft SQL Server Compact Edition\v3.5 folder, when build 3.5.8078.0 is installed.

 

UPDATE 3:

The final build released with VS 2010 is 3.5.8080.0.

The Desktop installer is a self-extracting .exe, that includes the following warning – On a 64-bit system both the 64-bit and 32-bit must be installed!

image

 

UPDATE 4: A workaround for a private deployment bug with SP2 (when using Entity Framework) is described here:

http://blogs.msdn.com/sqlservercompact/archive/2010/05/12/troubleshooting-problem-with-private-deployment-of-sql-server-compact-3-5sp2-entity-dll.aspx

Sunday, May 9, 2010

Recent Synchronization and Replication advice from Microsoft bloggers

This is a round-up of some recent blog entries from Microsoft relating to Sync and Replication.

Merge Replication

The Microsoft SQL Server Replication Support Team has started a blog which includes excellent Merge Replication advice. Start at this overview blog entry.

In addition, Rob Tiffany has made a list of SQL Compact Merge Replication performance tips available here. This list is a summary of some of the advice in Robs excellent Merge Replication book. It even includes some of the improvements in SQL Compact 3.5 SP2, including the new PostSyncCleanup property of the SqlCeReplication object, and highlights the new support for Windows 2008 R2 with IIS 7.5 included with the SP2 server tools.

Sync Services

The Sync Services team has published a useful FAQ. And the SQL Support team blog has a long walkthrough of setting up N-tier Sync Services for ADO.NET.