{"id":1168,"date":"2025-07-28T17:02:03","date_gmt":"2025-07-28T16:02:03","guid":{"rendered":"https:\/\/wp.bizoir.dk\/?p=1168"},"modified":"2025-08-20T22:03:39","modified_gmt":"2025-08-20T21:03:39","slug":"parsing-a-database-connectionstring-to-individual-values","status":"publish","type":"post","link":"https:\/\/wp.bizoir.dk\/?p=1168","title":{"rendered":"Parsing a Database connectionstring to individual values"},"content":{"rendered":"<p>From time to time, you may have to access (in some code) the individual values of a database connectionstring &#8211; and what to do if you only have the connectionstring available?<\/p>\n<p>In my case, it happened in Azure, where we normally use the connectionstring for Function Apps, but when I needed to deploy a Logic App, I suddenly needed the individual values (for the Logic App&#8217;s associated &#8216;API Connection&#8217;)<\/p>\n<p><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.data.sqlclient.sqlconnectionstringbuilder?view=net-9.0-pp\">Here<\/a> it tells us that this C# class can do the parsing, but it only mentions &#8220;Initial Catalog&#8221; and &#8220;Data Source&#8221; &#8211; and what do we do, if our connectionstring uses &#8220;Server&#8221; and &#8220;Database&#8221;<\/p>\n<p>It turns out (also in above description) that the class can also handle the synonyms &#8211; so if your connectionstring uses &#8220;Database&#8221;, you can get that value via &#8220;InitialCatalog&#8221;.<\/p>\n<p>Short example code showing the synonyms &#8211; it can be tested <a href=\"https:\/\/dotnetfiddle.net\/\">here<\/a><\/p>\n<p><code>public static void Main()<br \/>\n{<br \/>\nvar cs = \u201cServer=tcp:yada1.database.windows.net,1433;Database=yada2-dev;Persist Security Info=False;User ID=yada3-dev;Password=yada4;...................\u201d;<\/p>\n<p>System.Data.SqlClient.SqlConnectionStringBuilder sb = new System.Data.SqlClient.SqlConnectionStringBuilder(cs);<br \/>\nConsole.WriteLine($\u201dServer: {sb[\u201cServer\u201d]}\u201d);<br \/>\nConsole.WriteLine($\u201dDataSource: {sb.DataSource}\u201d);<br \/>\nConsole.WriteLine($\u201dDatabase: {sb[\u201cDatabase\u201d]}\u201d);<br \/>\nConsole.WriteLine($\u201dDatabase: {sb.InitialCatalog}\u201d);<br \/>\nConsole.WriteLine($\u201dUser: {sb.UserID}\u201d);<br \/>\nConsole.WriteLine($\u201dPassword: {sb.Password}\u201d);<br \/>\n}<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>From time to time, you may have to access (in some code) the individual values of a database connectionstring &#8211; and what to do if you only have the connectionstring available? In my case, it happened in Azure, where we normally use the connectionstring for Function Apps, but when I needed to deploy a Logic &hellip; <a href=\"https:\/\/wp.bizoir.dk\/?p=1168\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Parsing a Database connectionstring to individual values&#8221;<\/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":[9],"tags":[7],"class_list":["post-1168","post","type-post","status-publish","format-standard","hentry","category-guides","tag-csharp"],"_links":{"self":[{"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/posts\/1168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1168"}],"version-history":[{"count":1,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/posts\/1168\/revisions"}],"predecessor-version":[{"id":1169,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/posts\/1168\/revisions\/1169"}],"wp:attachment":[{"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}