如何在WPF中创建SQLite数据库?

3

我是新手,想学习Windows桌面应用程序开发。请问如何在Windows Presentation Foundation (WPF) 中创建SQLite数据库?


1
请查看此链接:http://www.codeproject.com/Articles/153407/WPF-and-SQLite-Database - lerner1225
2个回答

5
你需要完成以下两个步骤:
  1. 将SQLite dll添加到你的应用程序引用中
  2. 编写一个构建数据库的类。
例如:
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);
        }
    }
  }
}

祝你好运!


2

1.使用System.Data命名空间;

2.使用System.Data.SQLite命名空间;

            DataTable tb = new DataTable();
            string connection = @"Data Source=C:\Folder\SampleDB.db;Version=3;New=False;Compress=True;";
            SQLiteConnection sqlite_conn = new SQLiteConnection(connection);
            string stringQuery = "Select * from Student";
            sqlite_conn.Open();
            var SqliteCmd = new SQLiteCommand();
            SqliteCmd = sqlite_conn.CreateCommand();
            SqliteCmd.CommandText = stringQuery;
            SQLiteDataAdapter da = new SQLiteDataAdapter(SqliteCmd);
            DataSet ds = new DataSet();               
            da.Fill(tb);

这对初学者非常有用。以简单的方式访问SqliteConnection。 - Maghalakshmi Saravana

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接