Pages

Friday, January 23, 2009

Inserting images from VBA

Inserting images into a SQL Compact from VB(A) does not appear to be possible using vanilla VBA code (please correct me if I am wrong). In order to work around this limitation, it is possible to create a function in C#, and call this function from VBA. This excellent post got me going. This is the VBA Code:

Sub InsertBlob()

Dim IMG_FILE As String
Dim intFile As Integer
Dim ImgBuff() As Byte
Dim ImgLen As Long
Dim ret As String

IMG_FILE = "C:\ErikEJ.jpg"
'Read/Store GIF file in ByteArray
intFile = FreeFile
Open IMG_FILE For Binary As #intFile
ImgLen = LOF(intFile)
ReDim ImgBuff(ImgLen) As Byte
Get #intFile, , ImgBuff()
Close #intFile

Dim sqlCeUtil As New SqlCompactUtility
ret = sqlCeUtil.SaveBlob(ImgBuff(), "Data Source=C:\Data\SQLCE\NorthWind2.sdf", "INSERT INTO Images (Picture) VALUES (@Blob)", "@Blob")

End Sub


First a Byte array with the image contents is created. Then the .NET based method (SaveBlob) is invoked.



The SaveBlob method takes 4 parameters:



1. The byte array



2. The SQL Compact connection string



3. The Insert statement



4. The parameter name



In order for this to work, add a reference to the .NET dll library from the VB(A) project, the code of which is shown below (created per the instructions in the "A Basic Walk Through" part of the post mentioned above). If you have followed the instruction in the blog, the DLL should appear in the Add Reference dialog once built.





using System;
using System.Data.SqlServerCe;
using System.Runtime.InteropServices;

namespace SqlCompactInterop
{
public class SqlCompactUtility
{
//SaveBlob(data, @"Data Source=C:\Data\SQLCE\NorthWind2.sdf", "INSERT INTO Images (Picture, Id) VALUES (@Blob, 2)", "@Blob");
public string SaveBlob(byte[] data, string connectionString, string insertStatement, string parameterName)
{
try
{
using (SqlCeConnection conn = new SqlCeConnection(connectionString))
{
SqlCeCommand cmd = new SqlCeCommand(insertStatement);
cmd.Parameters.Add(
new SqlCeParameter(parameterName, System.Data.SqlDbType.Image, data.Length));
cmd.Parameters[parameterName].Value = data;
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
cmd.Dispose();
return string.Empty;
}
}
catch (SqlCeException e)
{
return ShowErrors(e);
}
}

// Error handling routine that generates an error message
private string ShowErrors(SqlCeException e)
{
SqlCeErrorCollection errorCollection = e.Errors;

System.Text.
StringBuilder bld = new System.Text.StringBuilder();
Exception inner = e.InnerException;

if (null != inner)
{
bld.Append(inner.ToString());
}
// Enumerate the errors to a message box.
foreach (SqlCeError err in errorCollection)
{
bld.Append(
"\n Error Code: " + err.HResult.ToString("X"));
bld.Append(
"\n Message : " + err.Message);
bld.Append(
"\n Minor Err.: " + err.NativeError);
bld.Append(
"\n Source : " + err.Source);

// Enumerate each numeric parameter for the error.
foreach (int numPar in err.NumericErrorParameters)
{
if (0 != numPar) bld.Append("\n Num. Par. : " + numPar);
}

// Enumerate each string parameter for the error.
foreach (string errPar in err.ErrorParameters)
{
if (String.Empty != errPar) bld.Append("\n Err. Par. : " + errPar);
}


}
return bld.ToString();
}

}

}

 






"Hope this helps"


kick it on DotNetKicks.com
READ MORE - Inserting images from VBA

Wednesday, January 21, 2009

ExportSqlCE - new release with support for SQL Compact 3.0/3.1

The post title says it all! - ExportSqlCE release 1.6 is now available at CodePlex. The reason for the new release is support for exporting SQL Server 2005 Mobile Edition (3.0) and SQL Server Compact Edition (3.1) sdf files. Identity columns are not included in the CREATE TABLE statement, as "SET IDENTITY_INSERT" is not available with SQL Compact 3.0/3.1. Support for 3.1 is enabled by ading a new solution and project file to the source, which in turn generates a new exe: ExportSqlCe31.exe. This allows both versions to share a single codebase, but reference different versions of System.Data.SqlServerCe, with conditional compilation symbols. Please try it out and let me know what you think.

Mærker fra Technorati: ,,
READ MORE - ExportSqlCE - new release with support for SQL Compact 3.0/3.1

Monday, January 19, 2009

ExportSQLCe – support for SQL Compact 3.1

The source (not a release yet) for ExportSQLCe has been updated to support SQL Compact 3.1 – please feel free to try it out.

READ MORE - ExportSQLCe – support for SQL Compact 3.1

Mobile Application Pocket Guide v1.1

This guide has just been updated, and has advice on design of Mobile Applications, including advice on using SQL Compact in mobile applications. A very useful guide with a practical approach. Get it at: http://www.codeplex.com/AppArch/Release/ProjectReleases.aspx?ReleaseId=19798

Technorati Tags: ,,
READ MORE - Mobile Application Pocket Guide v1.1

Monday, January 12, 2009

ExportSqlCe version 1.5 released

The latest version of the ExportSqlCe command line utility for scripting entire SQL Compact database files has been released on CodePlex.

Since I last blogged about this utility, the following missing features have been added:

Unicode support – the script file is now in Unicode format, which means that all text data is scripted “properly”, also for non-English text.

Image/binary/varbinary in INSERTs – binary and image fields are now scripted with their proper contents (as a hex string), and not “System.Byte[]”

INSERT with IDENTITY columns – CREATE TABLE now properly scripts IDENTITY columns, and using the SET IDENTITY INSERT feature of SQL Compact 3.5, it is now possible to do inserts on the table with the IDENTITY column.Numeric fraction and precision

INSERT statement improvements – INSERTs are now scripted without single quotes around numeric fields, which improves readability, INSERT performance and script size.
Support for rowversion – rowversion (timestamp) is a read-only column and cannot be updated. Therefore the generated script has been improved to exclude rowversion fields in INSERT statements.
Foreign keys with multiple fields – were not properly scripted as reported (and partly fixed) by user hugo on CodePlex.
Handle merge replicated databases – Merge replicated database files contains a number of system fields and tables. These should not be scripted – this  has been implemented.
Handle large tables – scripts of large tables are now flushed to disk in several files, to save memory.

Go and get the latest release/source, and give feedback at http://www.codeplex.com/ExportSqlCE

READ MORE - ExportSqlCe version 1.5 released

Sunday, January 4, 2009

Working with Case Sensitive SQL Compact databases

Up until version 3.5 SP1, SQL Compact have always been case in-sensitive, meaning that a string stored as "Albert" is considered the same as a string stored as "albert" in terms of sorting and selecting etc.

With 3.5 is is now possible to create Case Sensitive SQL Compact databases. This is always done a file creation time, and covers data in all tables in the entire database file.

When working with databases fro SQL Server 2008 Management Studio and VS 2008 SP1, the new database dialog has been updated to include a checkmark for "Case sensitive". In code, add "Case sensitive=true" to the connection string used for creating the database file.

image

image

The following testing is done using SQL 2008 SSMS (RC0).

Let's create a table:

CREATE TABLE CsTest ( TestVal nvarchar(50) NOT NULL )

And add some rows:

INSERT CsTest (TestVal) VALUES ('Albert')
INSERT CsTest (TestVal) VALUES ('albert')

So previously the statement below would have returned 2 rows:

SELECT * FROM CsTest WHERE TestVal = N'albert'

But as the database is case sensitive, only a single row is returned!

If we try to open this database in VS 2008 RTM (with only SQL Compact 3.5 RTM (version 3.5.5386.0) installed) - the engine will throw error 28609:
You are trying to access an older version of a SQL Server Compact 3.5 database. If this is a SQL Server CE 1.0 or 2.0 database, run upgrade.exe. If this is a SQL Server Compact 3.5 database, run Compact/Repair. (Not extremely helpful)

UPDATE: Converting a database to case sensitive:

This can be done with the Compact command as follows (in this sample Northwind.sdf is converted to a case sensitive database):

SqlCeEngine engine = new SqlCeEngine("Data Source=Northwind.sdf");
engine.Compact("Data Source=; Case Sensitive=True;");
READ MORE - Working with Case Sensitive SQL Compact databases