【发布时间】:2019-05-17 15:57:07
【问题描述】:
我正在尝试将 DataTable 插入到 SQLite 表中,并且我正在为此使用 foreach 循环,但它会抛出错误 SQLiteException: 'database is locked.
我试着用谷歌搜索这个,并尝试dispose我与using的联系
这是我的代码
public void Insert_to_db(DataTable data)
{
foreach (DataRow row in data.Rows)
{
string alertTag = row["Alert Tag"].ToString();
int group = Convert.ToInt32(row["Group"]);
int line = Convert.ToInt32(row["Line"]);
int task = Convert.ToInt32(row["Task"]);
string sql = string.Format("insert into alert_tag (alert_tag, layer_group, line, task) values ('{0}','{1}','{2}','{3}')", alertTag, group, line, task);
using (SQLiteConnection dbConn = new SQLiteConnection(Tools.SqliteConnString()))
{
using (SQLiteCommand command = new SQLiteCommand(sql, dbConn))
{
dbConn.Open();
command.ExecuteNonQuery();
dbConn.Close();
}
}
}
}
【问题讨论】:
-
一般说明:使用占位符/参数化查询。 string.Format 应该“从不”用于 SQL 中的值。搜索“SQL 注入”。
标签: c# sqlite datatable dapper