C#更新SQLite数据表内指定数据
可以使用以下代码在C#中更新SQLite数据表内的指定数据:
using System.Data.SQLite;
// 创建连接
using (SQLiteConnection connection = new SQLiteConnection("Data Source=<database_path>"))
{
connection.Open();
// 创建更新命令
string updateQuery = "UPDATE <table_name> SET <column_name> = @newValue WHERE <condition>";
using (SQLiteCommand command = new SQLiteCommand(updateQuery, connection))
{
// 设置参数的值
command.Parameters.AddWithValue("@newValue", <new_value>);
// 执行更新命令
int rowsAffected = command.ExecuteNonQuery();
// 输出受影响的行数
Console.WriteLine("Rows Affected: " + rowsAffected);
}
}
上述代码中,需要替换以下内容:
<database_path>:SQLite数据库文件的路径。<table_name>:要更新数据的表名。<column_name>:要更新的列名。<condition>:更新数据的条件(例如:id = 1)。<new_value>:要更新为的新值。
请注意,上述代码中的参数值使用了参数化查询(Parameterized Query),这是为了防止SQL注入攻击,建议在实际应用中使用参数化查询来保证安全性
原文地址: https://www.cveoy.top/t/topic/hDDq 著作权归作者所有。请勿转载和采集!