{"id":689,"date":"2008-06-20T14:20:30","date_gmt":"2008-06-20T13:20:30","guid":{"rendered":"http:\/\/www.itwriting.com\/blog\/?p=689"},"modified":"2008-06-20T14:20:30","modified_gmt":"2008-06-20T13:20:30","slug":"where-is-your-sql-server-ce-database","status":"publish","type":"post","link":"https:\/\/www.itwriting.com\/blog\/689-where-is-your-sql-server-ce-database.html","title":{"rendered":"Where is your SQL Server CE Database?"},"content":{"rendered":"<p>Maybe not where you think. Now, I admit I am three years late with this <strike>bug<\/strike> strange feature of Visual Studio but it wasted some of my time today so it is still worth reporting.<\/p>\n<p>I\u2019ve been writing about creating database applications in Visual Studio. Specifically, I was looking at what happens if you download Visual Basic Express and take the quickest, easiest route to knocking together a database application.<\/p>\n<p>The default local database engine these days is SQL Server Compact 3.5:<\/p>\n<blockquote>\n<p>When you create applications, the preferred local database is SQL Server Compact 3.5.<\/p>\n<\/blockquote>\n<p><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa983341.aspx\">says MSDN<\/a>.<\/p>\n<p>OK, so you add a new database to your project and accept various defaults. The wizard then asks you whether you would like to \u201ccopy the file to your project and modify the connection\u201d?<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.itwriting.com\/images\/vbdatadialog.gif\" \/> <\/p>\n<p>Sounds reasonable, if you can figure out what it means. Default is Yes, so OK do it.<\/p>\n<p>Mistake. Don\u2019t do that. Not, at least, without reading and understanding <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms246989.aspx\">this document<\/a>. But I digress. Next up, you are asked another question: <\/p>\n<blockquote>\n<p>Storing connection strings in your application configuration file eases maintenance and deployment \u2026 do you want to save the connection string to the application configuration file?<\/p>\n<\/blockquote>\n<p>It\u2019s another option that sounds good. OK, do it.<\/p>\n<p>Now you set up a little table or two, add some data-bound controls, and off you go. Run the app, enter some data, save it. Run the app again \u2026 and all your data has disappeared. Why?<\/p>\n<p>Well, it has to be either that the updates are silently failing; or that the database file is getting overwritten. It\u2019s the latter. It turns out that VB is treating your database like any other resource, and copying to bin\/debug when you run the app. This is the copy you are connecting to, you update it, but next time you build and run it gets overwritten.<\/p>\n<p>None of this is obvious, because when you look at the connection string in the application settings (which VB hides by default, sigh), it shows the database file in the root of your project folder. Click Test Connection, all is fine. The only warning sign is that the connection string looks like this:<\/p>\n<blockquote>\n<p>Data Source=|DataDirectory|\\test.sdf<\/p>\n<\/blockquote>\n<p>So where is |DataDirectory| set? That\u2019s not obvious either. <a href=\"http:\/\/forums.microsoft.com\/MSDN\/ShowPost.aspx?PostID=702378&amp;SiteID=1\">Read here<\/a> for the answer. It\u2019s an application property that is not visible anywhere, that gets set to different values depending on how you deploy the app. I can see why someone thought this was a smart idea; but the implementation is horrible. It gives you the illusion of having one database file, when in fact you have multiple copies (source, debug, release etc) overwriting one another, and during testing you are never editing the correct one.<\/p>\n<p>Once you have worked this out you can fix it, of course. But here\u2019s another problem. You are the single user of a database. You insert a record and save it, using all the generated data-bound stuff that Visual Studio provides. Works fine. Then you edit the record you just inserted, and save again. Boom. Concurrency exception. Why?<\/p>\n<p>It is all do to with a limitation of SQL Server Compact 3.5. It can\u2019t handle multiple SQL statements. This means that a feature of the ADO.NET TableAdapter, called <strong>Refresh the Data Table<\/strong> in the configuration wizard, is not available. This option kicks in when you have an identity column that auto-increments, which is by the easiest way to create a primary key. In this scenario, the actual value of the identity column is not known until after you make the insert, because it is generated by the database engine. Normally, the TableAdapter would retrieve it with a Select statement immediately after the Insert statement. However, with SQL Server Compact 3.5 that does not work. <\/p>\n<p>The result is that saving a record works fine, but next time around the row has an incorrect primary key in the DataSet. No wonder you get a concurrency exception.<\/p>\n<p>You can work around this in code, of course. But what surprises me is just how hard Microsoft has made all this for the kind of newbie programmer who might pick up VB Express. In fact, easy database programming in VB has marched backwards since Visual Basic 3 back in 1993.<\/p>\n<p>By the way, I also dislike the way VB adds so much database gunk to your main form, again by default. What if you add another form to your app? What if you want to delete the first form? It all gets messy fast. <\/p>\n<p>Look at <a href=\"http:\/\/www.rubyonrails.org\/\">Ruby on Rails<\/a>. It has simple database handling that works. OK, you are going to have to modify that code eventually; and I accept that database apps have an inherent complexity that no amount of wizards, O\/R layers or even &quot;Convention over Configuration\u201d can remove. I still think that simple, single table, single user apps should be, well, simple. Not in VB, unfortunately.<\/p>\n<div class=\"wlWriterSmartContent\" id=\"scid:0767317B-992E-4b12-91E0-4F059A8CECA8:9a0b553a-6a25-46d0-8b42-ae96f9c47748\" style=\"padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px\">Technorati tags: <a href=\"http:\/\/technorati.com\/tags\/ado.net\" rel=\"tag\">ado.net<\/a>, <a href=\"http:\/\/technorati.com\/tags\/visual+basic\" rel=\"tag\">visual basic<\/a>, <a href=\"http:\/\/technorati.com\/tags\/sql+server+ce\" rel=\"tag\">sql server ce<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Maybe not where you think. Now, I admit I am three years late with this bug strange feature of Visual Studio but it wasted some of my time today so it is still worth reporting. I\u2019ve been writing about creating database applications in Visual Studio. Specifically, I was looking at what happens if you download &hellip; <a href=\"https:\/\/www.itwriting.com\/blog\/689-where-is-your-sql-server-ce-database.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Where is your SQL Server CE Database?<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,80],"tags":[],"class_list":["post-689","post","type-post","status-publish","format-standard","hentry","category-database","category-software-development"],"_links":{"self":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts\/689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/comments?post=689"}],"version-history":[{"count":0,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts\/689\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/media?parent=689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/categories?post=689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/tags?post=689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}