Create Database File Example


#define SQLITE_DATABASE_FILE_PASSWORD		"mypasswordgoeshere"		//Set as "" for no password
	bool DatabaseSqlite::CreateDatabase(void)
	{
		String ^FilePath;
		System::Data::SQLite::SQLiteConnection ^Connection1;

		try
		{

			//----- CHECK DIRECTORY EXISTS -----
			if (!Directory::Exists(Environment::GetFolderPath(Environment::SpecialFolder::ApplicationData) + "\\" + Application::CompanyName + "\\" + Application::ProductName + "\\"))
				Directory::CreateDirectory(Environment::GetFolderPath(Environment::SpecialFolder::ApplicationData) + "\\" + Application::CompanyName + "\\" + Application::ProductName + "\\");

			//----- CREATE NEW DATABASE -----
			System::Data::SQLite::SQLiteConnection::CreateFile(Environment::GetFolderPath(Environment::SpecialFolder::ApplicationData) + "\\" + Application::CompanyName + "\\" + Application::ProductName + "\\mydatabasefile.db");
			
			//Password protect the database
			System::Data::SQLite::SQLiteConnection ^Connection1;
			Connection1 = gcnew System::Data::SQLite::SQLiteConnection("data source=" + FilePath);
			Connection1->SetPassword(SQLITE_DATABASE_FILE_PASSWORD);
			Connection1->Open();
			return(true);
		}
		catch (Exception ^e)
		{
			return(false);
		}
		finally
		{
			//----- CLOSE THE DATABASE CONNECTION IF NECESSARY -----
			try
			{
				if (Connection1 != nullptr)
					Connection1->Close();
			}
			catch (Exception ^)
			{
			}
		}
		
	}

 

 

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *