C# connect to SQLite database and create table

SQLite is a software library that implements a self-sufficient, serverless, zero-configuration, transactional lightweight SQL database engine.

  1. Declare the variable Conn connected to SQLite
    Add SQLite operation driver dll reference: System.Data.SQLite.dll
using System.Data.SQLite; 

SQLiteConnection Conn;

Direct NuGet package search for System.Data.SQLite.Core
insert image description here

  1. Create a connection to the specified database
    to determine whether the file exists, create it in the path if it does not exist, and then connect to the database
string FilePath = Application.StartupPath + "\\transfer.db";
if ( !File.Exists(FilePath))

SQLiteConnection.CreateFile(FilePath);
}
try

Conn = new SQLiteConnection("Data Source=" + FilePath + ";Version=3;"
);
Conn.Open();
}
catch (Exception ex)
throw new Exception("打开数据库:" + FilePath + "的连接失败: " + ex.Messag e);
}
  1. create table
string sql = "create table info (name varchar(20), address varchar(40))";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();

Author: Wu Hongtao

Guess you like

Origin blog.csdn.net/ekcchina/article/details/130991090