C#添加数据到新建的SQLite数据表内
要将数据添加到新建的SQLite数据表内,您可以使用以下步骤:
-
首先,确保您已经安装了SQLite数据库,并且已经在C#项目中添加了对SQLite的引用。
-
在C#代码中,使用SQLite连接字符串来连接到SQLite数据库。例如:
using System.Data.SQLite;
string connectionString = "Data Source=mydatabase.sqlite;Version=3;";
SQLiteConnection connection = new SQLiteConnection(connectionString);
connection.Open();
- 创建一个新的SQLite数据表。例如,创建一个名为"mytable"的数据表,并定义所需的列:
string createTableQuery = "CREATE TABLE IF NOT EXISTS mytable (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER);";
SQLiteCommand createTableCommand = new SQLiteCommand(createTableQuery, connection);
createTableCommand.ExecuteNonQuery();
- 使用INSERT INTO语句将数据插入到数据表中。例如,插入一条名为"John"、年龄为25的数据:
string insertDataQuery = "INSERT INTO mytable (name, age) VALUES ('John', 25);";
SQLiteCommand insertDataCommand = new SQLiteCommand(insertDataQuery, connection);
insertDataCommand.ExecuteNonQuery();
- 关闭与SQLite数据库的连接:
connection.Close();
完成上述步骤后,数据将成功添加到新建的SQLite数据表中。您可以根据需要重复步骤4来插入更多的数据
原文地址: https://www.cveoy.top/t/topic/hDw5 著作权归作者所有。请勿转载和采集!