Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create sqlite database in wpf?

Tags:

sqlite

wpf

I am new to windows desktop application development.Will you please suggest me how to create a SQLite database in windows presentation foundation(wpf)?

like image 702
srinivas vadlamudi Avatar asked Apr 21 '26 20:04

srinivas vadlamudi


1 Answers

You need to do 2 things:

  1. Add the SQLite dll to your application references
  2. Write a class that builds the 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 FROM sqlite_master WHERE 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(*) FROM " + tableName;
        var result = command.ExecuteScalar();

        return Convert.ToInt32(result) > 0 ? true : false;
    }


    public void fillTable()
    {
        if (!checkIfTableContainsData("MY_TABLE"))
        {
            sqlCommand = "insert into MY_TABLE (code_test_type) values (999)";
            executeQuery(sqlCommand);
        }
    }
  }
}

Good luck!

like image 51
DasDas Avatar answered Apr 24 '26 10:04

DasDas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!