{"id":65,"date":"2016-02-11T18:06:21","date_gmt":"2016-02-11T18:06:21","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=65"},"modified":"2022-02-17T06:24:15","modified_gmt":"2022-02-17T06:24:15","slug":"create-new-database","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/databases\/sqlite\/database\/create-new-database","title":{"rendered":"Create New Database"},"content":{"rendered":"<h4>\n\tCreate Database File Example<br \/>\n<\/h4>\n<pre>\r\n<code>\r\nusing System.IO;\r\nusing System.Windows.Forms;\r\n\r\nnamespace MyProjectNamespace\r\n{\r\n\tclass DatabaseSqlite\r\n\t{\r\n\t\tconst string SQLITE_DATABASE_FILE_PASSWORD = &quot;add_my_password_here&quot;;\t\t\/\/Set as &quot;&quot; for no password\r\n\r\n\t\t\/\/*************************************\r\n\t\t\/\/*************************************\r\n\t\t\/\/********** CREATE DATABASE **********\r\n\t\t\/\/*************************************\r\n\t\t\/\/*************************************\r\n\t\tbool CreateDatabase ()\r\n\t\t{\r\n\t\t\tString FilePath;\r\n\t\t\tSystem.Data.SQLite.SQLiteConnection Connection1 = null;\r\n\r\n\t\t\ttry\r\n\t\t\t{\r\n\t\t\t\t\/\/----- CHECK DIRECTORY EXISTS -----\r\n\t\t\t\tif (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + &quot;\\\\&quot; + Application.CompanyName + &quot;\\\\&quot; + Application.ProductName + &quot;\\\\&quot;))\r\n\t\t\t\t\tDirectory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + &quot;\\\\&quot; + Application.CompanyName + &quot;\\\\&quot; + Application.ProductName + &quot;\\\\&quot;);\r\n\r\n\t\t\t\t\/\/----- CREATE NEW DATABASE -----\r\n\t\t\t\tFilePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + &quot;\\\\&quot; + Application.CompanyName + &quot;\\\\&quot; + Application.ProductName + &quot;\\\\mydatabasefile.db&quot;;\r\n\t\t\t\tSystem.Data.SQLite.SQLiteConnection.CreateFile(FilePath);\r\n\r\n\t\t\t\t\/\/Password protect the database\r\n\t\t\t\tConnection1 = new System.Data.SQLite.SQLiteConnection(&quot;data source=&quot; + FilePath);\r\n\t\t\t\tConnection1.SetPassword(SQLITE_DATABASE_FILE_PASSWORD);\r\n\t\t\t\tConnection1.Open();\r\n\t\t\t\treturn (true);\r\n\t\t\t}\r\n\t\t\tcatch (Exception)\r\n\t\t\t{\r\n\t\t\t\treturn (false);\r\n\t\t\t}\r\n\t\t\tfinally\r\n\t\t\t{\r\n\t\t\t\t\/\/----- CLOSE THE DATABASE CONNECTION IF NECESSARY -----\r\n\t\t\t\ttry\r\n\t\t\t\t{\r\n\t\t\t\t\tif (Connection1 != null)\r\n\t\t\t\t\t\tConnection1.Close();\r\n\t\t\t\t}\r\n\t\t\t\tcatch (Exception)\r\n\t\t\t\t{\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t}\r\n\r\n\t}\r\n}\r\n<\/code><\/pre>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Create Database File Example using System.IO; using System.Windows.Forms; namespace MyProjectNamespace { class DatabaseSqlite { const string SQLITE_DATABASE_FILE_PASSWORD = &quot;add_my_password_here&quot;; \/\/Set as &quot;&quot; for no password \/\/************************************* \/\/************************************* \/\/********** CREATE DATABASE ********** \/\/************************************* \/\/************************************* bool CreateDatabase () { String FilePath; System.Data.SQLite.SQLiteConnection Connection1 = null; try { \/\/&#8212;&#8211; CHECK DIRECTORY EXISTS &#8212;&#8211; if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + &quot;\\\\&quot; + [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-65","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/65","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=65"}],"version-history":[{"count":3,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/65\/revisions"}],"predecessor-version":[{"id":100,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/65\/revisions\/100"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=65"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=65"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=65"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}