C# 连接 Navicat 创建的 SQLite 数据库和数据表
要在 C# 中连接 Navicat 创建的 SQLite 数据库和数据表,可以使用 System.Data.SQLite 命名空间中的 SQLiteConnection 和 SQLiteCommand 类。
首先,确保已经安装了 System.Data.SQLite NuGet 包。
然后,可以使用以下代码示例来连接 Navicat 创建的 SQLite 数据库和数据表:
using System.Data.SQLite;
public class SQLiteHelper
{
private SQLiteConnection connection;
public SQLiteHelper(string databasePath)
{
connection = new SQLiteConnection('Data Source=' + databasePath);
connection.Open();
}
public void CreateTable(string tableName, string columns)
{
string query = 'CREATE TABLE IF NOT EXISTS ' + tableName + ' (' + columns + ')';
using (SQLiteCommand command = new SQLiteCommand(query, connection))
{
command.ExecuteNonQuery();
}
}
// 其他数据库操作方法...
}
使用示例:
string databasePath = 'your_database_path';
SQLiteHelper helper = new SQLiteHelper(databasePath);
string tableName = 'your_table_name';
string columns = 'id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER';
helper.CreateTable(tableName, columns);
// 其他数据库操作...
请确保将'your_database_path'替换为 SQLite 数据库文件的实际路径,'your_table_name'替换为要操作的数据表名称,'columns'替换为要创建的数据表的列定义。
原文地址: https://www.cveoy.top/t/topic/o5qX 著作权归作者所有。请勿转载和采集!