Created
September 25, 2016 15:57
-
-
Save bolorundurowb/9e76f62c2476e4859147dc4e103f9b37 to your computer and use it in GitHub Desktop.
A C# code snippet to determine if a database file exists, if it doesn't create it and add data to it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.SQLite; | |
package Gist | |
{ | |
public class SeedData | |
{ | |
string readonly dbPath = "database.db"; | |
string readonly dbConnectionString = "Data Source=" + dbPath; | |
public bool VerifyData() | |
{ | |
if(!File.Exists(dbPath)) | |
{ | |
SQLiteConnection.CreateFile(dbPath); | |
return PopulateDatabase(); | |
} | |
else | |
{ | |
return true; | |
} | |
} | |
private bool PopulateDatabase() | |
{ | |
try | |
{ | |
SQLiteConnection connection = new SQLiteConnection(dbConnectionString); | |
connection.Open(); | |
// | |
// Modify this table creation script to fit your needs | |
SQLiteCommand command = new SQLiteCommand("CREATE TABLE IF NOT EXISTS [Users] (Firstname VARCHAR, Surname VARCHAR, Username VARCHAR CONSTRAINT pk PRIMARY KEY, Password VARCHAR)", connection); | |
command.ExecuteNonQuery(); | |
// Insert your data in this way. | |
command = new SQLiteCommand("INSERT INTO [Users] VALUES ()", connection); | |
command.ExecuteNonQuery(); | |
connection.Close(); | |
return true; | |
} | |
catch (Exception) | |
{ | |
return false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment