Reputation: 13
I'm creating a c# console app and using sqlite as my db, and I have two scripts the first script (DBCreationScript.sql) contains the database schema(tables to be created) and the second script (DefaultData.sql) populates the database tables with default data. Is there a way or specific code that I could use to load/import/run the two scripts in my console application.
My goal is to : 1. Create the sqlite db (by loading the script) using code from my console app. 2. Populate the database (by loading the second script). 3. Then query the data and display on the console screen
see below the two scripts.
CREATE TABLE tbLocation
(
LocationCd varchar(3) PRIMARY KEY
,LocationName varchar(50)
,CountryCd varchar(2) REFERENCES tbCountry(CountryCd)
,CountryName varchar(25)
,PassportRequired bit
,SortOrder Integer
);
CREATE TABLE tbManage
(
ConfigParamCd varchar(25) PRIMARY KEY
,ConfigParamDesc varchar(225)
,ConfigParamTypeCd varchar(20)
,Value varchar(2000)
,[Status] bit
);
CREATE TABLE tbPerson
(
PersonNo varchar(10) PRIMARY KEY
,FirstName varchar(25)
,LastName varchar(25)
,PinCode varchar(5)
,FleetCd varchar(10)
,[Status] bit
);
.
INSERT INTO tbManage (ConfigParamCd,ConfigParamDesc,ConfigParamTypeCd,Value,Status)
select 'AirlineCd' ConfigParamCd, 'The current airline code.' ConfigParamDesc, 'String' ConfigParamTypeCd, 'BA' Value, '1' Status UNION
select 'CanCreateNewSector' ConfigParamCd, 'Does this airline allow crew members to create new sectors?' ConfigParamDesc, 'Boolean' ConfigParamTypeCd, '1' Value, '1' Status UNION
select 'CanEditSeals' ConfigParamCd, 'Does this airline allow crew members to add, edit or delete seals?' ConfigParamDesc, 'Boolean' ConfigParamTypeCd, '1' Value, '1' Status UNION
select 'CanSectorsReopen' ConfigParamCd, 'Does this airline allow crew members to reopen sectors?' ConfigParamDesc, 'Boolean' ConfigParamTypeCd, '1' Value, '1' Status UNION
select 'DefaultCurrency' ConfigParamCd, 'The default currency code.' ConfigParamDesc, 'String' ConfigParamTypeCd, 'ZAR' Value, '1' Status UNION
select 'DefaultCurrencySymbol' ConfigParamCd, 'The default currency symbol.' ConfigParamDesc, 'String' ConfigParamTypeCd, 'R' Value, '1' Status UNION
select 'EmergencyNumber' ConfigParamCd, 'The emergency number of the airline.' ConfigParamDesc, 'String' ConfigParamTypeCd, '911' Value, '1' Status UNION
Upvotes: 1
Views: 1414
Reputation: 203
You should create a db script consisting of DDL and DML first and call, SQLiteConnection.CreateFile(string databaseFileName) method. Specify IF NOT EXISTS in the CREATE statement.
Upvotes: 1