{"id":1479,"date":"2022-10-22T16:10:38","date_gmt":"2022-10-22T22:10:38","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=1479"},"modified":"2022-10-22T16:10:38","modified_gmt":"2022-10-22T22:10:38","slug":"using-other-databases-in-c","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2022\/10\/22\/using-other-databases-in-c\/","title":{"rendered":"Using other databases in C#"},"content":{"rendered":"\n<p>A new version of the Einhugur C# UI framework was released a short while ago.<\/p>\n\n\n\n<p>Since the framework is still literally a work in progress some changes an dupes are to be expected. Listbox now exists and its constructor now takes an array of column titles as well as the initial dimensions. So you may need to update code like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nlist = new ListBox(10, 20, 80, 20 );<\/code><\/pre>\n\n\n\n<p>to add a new last parameter<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list = new ListBox(10, 20, 80, 20, new string&#91;] {\"col 1\", \"col 2\"} );<\/code><\/pre>\n\n\n\n<p>to set the column titles.<\/p>\n\n\n\n<p>Dont be surprised won the road if there are several constructors to pick from where you dont have to set the titles right then.<\/p>\n\n\n\n<p>With that out of the way lets turn to using the Mono.SQLite database &#8211; with one on disk ! <\/p>\n\n\n\n<p>In prior editions of the tutorial we used the in memory SQLite database. We had <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sqlite_conn = new SqliteConnection(\"Data Source=:memory:\");<\/code><\/pre>\n\n\n\n<p>To switch this to use a database on disk we just need to alter the connection string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  sqlite_conn = new SqliteConnection(\"URI=file:\/Users\/npalardy\/testdb.sqlite;\");\n<\/code><\/pre>\n\n\n\n<p>With the Mono driver for SQLite when you connect this will create the database if it doesnt already exist.<\/p>\n\n\n\n<p>And .. tada ! Thats it \ud83d\ude42 Instead of an in memory database now we are using one from disk.<br>Here&#8217;s the <a href=\"https:\/\/great-white-software.com\/miscellaneous\/CSharp\/getting_started-2022-10-22a.zip\">project as it exists so far.<\/a><\/p>\n\n\n\n<p>In the<a href=\"https:\/\/ifnotnil.com\/t\/getting-started-in-c-on-macos-where-next\/2824\"> poll I posted<\/a> half the respondents \ud83d\ude1bSia they wanted to see how to use a different db. So we&#8217;ll alter this to use PostgreSQL since I happen to have a handy configuration already on my machine &#8211; but the steps will be similar for most databases.<\/p>\n\n\n\n<p>First we need to grab a PostgreSQL database driver. We&#8217;ll add a NuGet package for this. Right click on <em>Packages<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"475\" height=\"538\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-3.21.42-PM.png?resize=475%2C538&#038;ssl=1\" alt=\"\" class=\"wp-image-1480\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-3.21.42-PM.png?w=475&amp;ssl=1 475w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-3.21.42-PM.png?resize=265%2C300&amp;ssl=1 265w\" sizes=\"auto, (max-width: 475px) 100vw, 475px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"360\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-3.21.51-PM.png?resize=525%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-1481\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-3.21.51-PM.png?resize=1024%2C703&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-3.21.51-PM.png?resize=300%2C206&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-3.21.51-PM.png?resize=768%2C527&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-3.21.51-PM.png?w=1071&amp;ssl=1 1071w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>We can filter using the field in the upper right to quickly find relevant packages<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"353\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-3.22.05-PM.png?resize=525%2C353&#038;ssl=1\" alt=\"\" class=\"wp-image-1482\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-3.22.05-PM.png?resize=1024%2C689&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-3.22.05-PM.png?resize=300%2C202&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-3.22.05-PM.png?resize=768%2C516&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-3.22.05-PM.png?w=1050&amp;ssl=1 1050w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>Note there are many PostgreSQL packages we can pick from. I honestly dont know all the pro&#8217;s and cons of the different packages. I have used the Nppqsql package ands so far it works as I need &amp; expect.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"359\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-3.22.19-PM.png?resize=525%2C359&#038;ssl=1\" alt=\"\" class=\"wp-image-1483\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-3.22.19-PM.png?resize=1024%2C700&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-3.22.19-PM.png?resize=300%2C205&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-3.22.19-PM.png?resize=768%2C525&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-3.22.19-PM.png?w=1055&amp;ssl=1 1055w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>Select that package and add it to the project. You may see several other packages also get aded. When a package depends on another to be able to work the packages that are depended on are also brought in automatically.<\/p>\n\n\n\n<p>In Xojo terms we&#8217;ve basically added a &#8220;plugin&#8221; &#8211; but only to this project. Projects in VS have &#8220;per project plugins&#8221;<\/p>\n\n\n\n<p>In our code we&#8217;ll need to addd a using clause to let the compiler know it should expect references to this new plugin in this code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Collections.Generic;\nusing System.Data;\nusing System.Diagnostics;\nusing CloudKit;\nusing Einhugur.Forms;\n\nusing Mono.Data.Sqlite;\nusing Npgsql; \/\/ &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;<\/code><\/pre>\n\n\n\n<p>As well we need to adjust our connection so instead of using SQLite it uses PostgreSQL.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> \/\/ I deliberately left the variable name the same so you can see how\n \/\/ little needs adjustment\n\n \/\/ Mono.Data.Sqlite.SqliteConnection sqlite_conn;\n NpgsqlConnection sqlite_conn;<\/code><\/pre>\n\n\n\n<p>And the code where we create the connection needs to connect to the server<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ sqlite_conn = \n\/\/ new SqliteConnection(\"URI=file:\/Users\/npalardy\/testdb.sqlite;\");\n\nsqlite_conn = \nnew NpgsqlConnection(\"Host=localhost;Username=XXXX;Password=XXXX;Database=norm_csharp\");\n<\/code><\/pre>\n\n\n\n<p>The ONE other things we have to account for, since we&#8217;re using raw SQL. While the following will work for SQLite it wont work for PostgreSQL.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>IDbCommand dbcmd = sqlite_conn.CreateCommand();\nstring sql = \"create table if not exists employee ( firstname, lastname )\";<\/code><\/pre>\n\n\n\n<p>SQLite is OK with an untyped create statement. PostgreSQL isn&#8217;t. So we do need to alter that as well.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>IDbCommand dbcmd = sqlite_conn.CreateCommand();\nstring sql = \"create table if not exists employee ( firstname varchar(50), lastname varchar(50) )\";<\/code><\/pre>\n\n\n\n<p>And with that change everything should work again and now were using PostreSQL.<\/p>\n\n\n\n<p>We <strong>could<\/strong> use a more generic data type for the now badly named <em>sqlite_conn<\/em>. Something more akin to Xojo&#8217;s <em>Database<\/em>. We could declare it as<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>            System.Data.IDbConnection sqlite_conn;<\/code><\/pre>\n\n\n\n<p>And our code could create &amp; assign either a new sqlite connection or PostgreSQL one<\/p>\n\n\n\n<p>Here&#8217;s the<a href=\"https:\/\/great-white-software.com\/miscellaneous\/CSharp\/getting_started-2022-10-22b.zip\"> project as it exists <\/a>so far.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A new version of the Einhugur C# UI framework was released a short while ago. Since the framework is still literally a work in progress some changes an dupes are to be expected. Listbox now exists and its constructor now takes an array of column titles as well as the initial dimensions. So you may &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2022\/10\/22\/using-other-databases-in-c\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using other databases in C#&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[34,4],"class_list":["post-1479","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-csharp","tag-macos"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1479","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/comments?post=1479"}],"version-history":[{"count":1,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1479\/revisions"}],"predecessor-version":[{"id":1484,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1479\/revisions\/1484"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=1479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=1479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=1479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}