{"id":831,"date":"2012-04-03T10:11:08","date_gmt":"2012-04-03T08:11:08","guid":{"rendered":"http:\/\/hen.co.za\/blog\/?p=831"},"modified":"2012-04-03T10:11:08","modified_gmt":"2012-04-03T08:11:08","slug":"create-sqlce-database-in-code","status":"publish","type":"post","link":"https:\/\/hen.co.za\/blog\/2012\/04\/create-sqlce-database-in-code\/","title":{"rendered":"Create SQLCE database in code"},"content":{"rendered":"<p>This is not exactly something new or breaking any new boundaries but it could be a useful tip for anyone that wants to make use of SQLCE databases from a small .Net application. There are some great tools and utilities to create a new SQL compact edition database using some UI but what if you want to automate the process or allow a client app to create a new database &#8216;on the fly&#8217; or when needed?<\/p>\n<p>It turns out it is not really that hard at all. All you need is a reference to the\u00a0System.Data.SqlServerCe assembly &#8211; which is installed when you run the Sql server compact installer. Then you simply need to use the following code (example):<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">string<\/span> connStr = <span style=\"color: #0000ff;\">string<\/span>.Format(&#8220;Data Source={0};File Mode=Shared Read;Persist Security Info=False&#8221;, sdfFilePath);<br \/>\n<span style=\"color: #0000ff;\">using<\/span> (System.Data.SqlServerCe.<span style=\"color: #0000ff;\">SqlCeEngine<\/span> SQLCEDB = <span style=\"color: #0000ff;\">new<\/span> System.Data.SqlServerCe.<span style=\"color: #0000ff;\">SqlCeEngine<\/span>(connStr))<br \/>\n{<\/p>\n<p style=\"padding-left: 30px;\">SQLCEDB.CreateDatabase();<\/p>\n<p>}<\/p><\/blockquote>\n<p>That will create a new &#8216;blank&#8217; database. What if you want to also create tables inside it? That is also very simple. I created a simple library to do that &#8211; it simply takes an input script and runs each command one by one. Each statement must be separated by a &#8216;GO&#8217; statement that is on its own line.<\/p>\n<p>e.g.<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">CREATE TABLE<\/span> [TableA] (<br \/>\n[SomeID] int NOT NULL IDENTITY (1,1)<br \/>\n, [Desc] nvarchar(100) NOT NULL<br \/>\n);<br \/>\n<span style=\"color: #0000ff;\">GO<\/span><br \/>\n<span style=\"color: #0000ff;\">CREATE TABLE<\/span> [TableB] (<br \/>\n[OtherID] int NOT NULL IDENTITY (1,1)<br \/>\n, [Desc] nvarchar(100) NOT NULL<br \/>\n);<br \/>\n<span style=\"color: #0000ff;\">GO<\/span><br \/>\n&#8230;<\/p><\/blockquote>\n<p>The class that handles it looks like this:<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">public delegate void<\/span> CreateCmndResponseMsgDelegate(<span style=\"color: #0000ff;\">string<\/span> createCMND, <span style=\"color: #0000ff;\">string<\/span> message);<br \/>\n<span style=\"color: #0000ff;\">public class<\/span> <span style=\"color: #0000ff;\">CreateSQLCEDB<\/span> : <span style=\"color: #0000ff;\">BaseSQLCEDAL<\/span><br \/>\n{<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">private List<\/span>&lt;<span style=\"color: #0000ff;\">string<\/span>&gt; createCMDs = <span style=\"color: #0000ff;\">new List<\/span>&lt;<span style=\"color: #0000ff;\">string<\/span>&gt;();<br \/>\n<span style=\"color: #0000ff;\">public int<\/span> ErrorCount { <span style=\"color: #0000ff;\">get<\/span>; <span style=\"color: #0000ff;\">set<\/span>; }<br \/>\n<span style=\"color: #0000ff;\">public event CreateCmndResponseMsgDelegate<\/span> CreateCmndResponseMsg;<br \/>\n<span style=\"color: #0000ff;\">private void<\/span> RaiseCreateCmndResponseMsg(<span style=\"color: #0000ff;\">string<\/span> createCMND, <span style=\"color: #0000ff;\">string<\/span> message)<br \/>\n{<\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">if<\/span> (CreateCmndResponseMsg != <span style=\"color: #0000ff;\">null<\/span>)<br \/>\n{<\/p>\n<p style=\"padding-left: 90px;\">CreateCmndResponseMsg(createCMND, message);<\/p>\n<p style=\"padding-left: 60px;\">}<\/p>\n<p style=\"padding-left: 30px;\">}<br \/>\n<span style=\"color: #0000ff;\">public void<\/span> CreateCommandsFromScript(<span style=\"color: #0000ff;\">string<\/span> script)<br \/>\n{<\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\">string<\/span>[] createCmds = script.Split(<span style=\"color: #0000ff;\">new string<\/span>[] { &#8220;\\r\\nGO\\r\\n&#8221; }, <span style=\"color: #0000ff;\">StringSplitOptions<\/span>.RemoveEmptyEntries);<br \/>\ncreateCMDs.AddRange(createCmds);<\/p>\n<p style=\"padding-left: 30px;\">}<br \/>\n<span style=\"color: #0000ff;\">public bool<\/span> RunCommands()<br \/>\n{<\/p>\n<p style=\"padding-left: 60px;\">ErrorCount = 0;<br \/>\n<span style=\"color: #0000ff;\">base<\/span>.OpenConnection();<br \/>\n<span style=\"color: #0000ff;\">foreach<\/span> (<span style=\"color: #0000ff;\">string<\/span> cmndStr <span style=\"color: #0000ff;\">in<\/span> createCMDs)<br \/>\n{<\/p>\n<p style=\"padding-left: 90px;\"><span style=\"color: #0000ff;\">try<\/span><br \/>\n{<\/p>\n<p style=\"padding-left: 120px;\"><span style=\"color: #0000ff;\">base<\/span>.ExecuteNonQuery(cmndStr);<\/p>\n<p style=\"padding-left: 90px;\">}<br \/>\n<span style=\"color: #0000ff;\">catch<\/span> (<span style=\"color: #0000ff;\">Exception<\/span> ex)<br \/>\n{<\/p>\n<p style=\"padding-left: 120px;\">ErrorCount++;<br \/>\nRaiseCreateCmndResponseMsg(cmndStr, ex.Message);<\/p>\n<p style=\"padding-left: 90px;\">}<\/p>\n<p style=\"padding-left: 60px;\">}<br \/>\n<span style=\"color: #0000ff;\">base<\/span>.CloseConnection();<br \/>\n<span style=\"color: #0000ff;\">return<\/span> ErrorCount == 0;<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p>}<\/p><\/blockquote>\n<p>Note that the base class &#8216;BaseSQLCEDAL&#8217; is simply a wrapper class to handle all interactions with SqlCeConnection,\u00a0SqlCeCommand etc.<\/p>\n<p>See\u00a0<a href=\"https:\/\/hen.co.za\/blog\/wp-content\/uploads\/2012\/04\/CreateSQLCEDB.zip\">CreateSQLCEDB<\/a>\u00a0for full example code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is not exactly something new or breaking any new boundaries but it could be a useful tip for anyone that wants to make use of SQLCE databases from a small .Net application. There are some great tools and utilities &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/hen.co.za\/blog\/2012\/04\/create-sqlce-database-in-code\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[7,8,9,303,49,156,155],"class_list":["post-831","post","type-post","status-publish","format-standard","hentry","category-development","tag-net","tag-c","tag-code","tag-development","tag-sql","tag-sql-compact","tag-sqlce"],"_links":{"self":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/comments?post=831"}],"version-history":[{"count":7,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/831\/revisions"}],"predecessor-version":[{"id":839,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/831\/revisions\/839"}],"wp:attachment":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/media?parent=831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/categories?post=831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/tags?post=831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}