Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@bolorundurowb
Created September 25, 2016 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bolorundurowb/9e76f62c2476e4859147dc4e103f9b37 to your computer and use it in GitHub Desktop.
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.
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