how to create sqlite database in wpf? -
i new windows desktop application development.will please suggest me how create sqlite database in windows presentation foundation(wpf)?
you need 2 things:
- add sqlite dll application references
- write class builds db.
for example:
using system; using system.collections.generic; using system.data.sqlite; using system.diagnostics; using system.io; using system.linq; using system.text; using system.threading.tasks; namespace database { public class dbcreator { sqliteconnection dbconnection; sqlitecommand command; string sqlcommand; string dbpath = system.environment.currentdirectory + "\\db"; string dbfilepath; public void createdbfile() { if (!string.isnullorempty(dbpath) && !directory.exists(dbpath)) directory.createdirectory(dbpath); dbfilepath = dbpath + "\\yourdb.db"; if (!system.io.file.exists(dbfilepath)) { sqliteconnection.createfile(dbfilepath); } } public string createdbconnection() { string strcon = string.format("data source={0};", dbfilepath); dbconnection = new sqliteconnection(strcon); dbconnection.open(); command = dbconnection.createcommand(); return strcon; } public void createtables() { if (!checkifexist("my_table")) { sqlcommand = "create table my_tbale(idnt_test integer primary key autoincrement,code_test_type integer"; executequery(sqlcommand); } } public bool checkifexist(string tablename) { command.commandtext = "select name sqlite_master name='" + tablename + "'"; var result = command.executescalar(); return result != null && result.tostring() == tablename ? true : false; } public void executequery(string sqlcommand) { sqlitecommand triggercommand = dbconnection.createcommand(); triggercommand.commandtext = sqlcommand; triggercommand.executenonquery(); } public bool checkiftablecontainsdata(string tablename) { command.commandtext = "select count(*) " + tablename; var result = command.executescalar(); return convert.toint32(result) > 0 ? true : false; } public void filltable() { if (!checkiftablecontainsdata("my_table")) { sqlcommand = "insert my_table (code_test_type) values (999)"; executequery(sqlcommand); } } } } good luck!
Comments
Post a Comment