Thursday, June 2, 2011

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.

image

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();
}
}
}
}
}

Monday, May 23, 2011

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:

image

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

Monday, May 16, 2011

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!

Friday, May 6, 2011

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.

Tuesday, May 3, 2011

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.

Thursday, April 14, 2011

SQL Server Compact will be available for developers in upcoming Windows Phone “Mango” update

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.

Monday, April 4, 2011

Useful new topics in SQL Server Compact 4.0 Books Online

SQL Server Compact 4.0 Books Online contains a couple of useful new help topics, that relate some to the new features in version 4.0, the OFFSET FETCH clause, support for medium trust under ASP.NET, and a couple of missing ADO.NET APis, that have been implemented.

SQL Server Compact 4.0 Books Online is available for download, and also available online.

Some of the interesting new topics include:

What’s new in SQL Server Compact 4.0

Microsoft SQL Server Compact 4.0 has a group of new features, and enables a new scenario where SQL Server Compact 4.0 can be used as a database for ASP.NET Web applications and Web sites.

OFFSET FETCH Clause

The OFFSET-FETCH clause provides you with an option to fetch only a window or page of results from the result set. OFFSET-FETCH can be used only with the ORDER BY clause.

Deployment Considerations

SQL Server Compact 4.0 is optimized for use as a database for ASP.NET web applications. Web applications are required to run in Medium Trust or Partial Trust, SQL Server Compact 4.0 can also run in medium or partial trust level. (Includes steps required to make SQL Server Compact 4.0 run under medium trust with .NET Framework 3.5 SP1.

SqlCeConnection.GetSchema method

Returns schema information for the data source of this SqlCeConnection

SqlCeConnectionStringBuilder Class

Provides a simple way to create and manage the contents of connection strings used by the SqlCeConnection class.