直接上代码 仅供参考。
//创建一个连接到指定数据库,并插入数据
SQLiteConnection m_dbConnection;
public void InsertData(List<RfidModel> rf)
{
try
{
m_dbConnection = new SQLiteConnection("Data Source=D:\\Project\\bin\\Debug\\rfid.db;Version=3;");
m_dbConnection.Open();
Logger.Debug("****连接sqlite成功********");
string sql = "insert into Setting(TagID,DevID,Lf,Hf,Activate,Voltage,DateTime)values (@TagID,@DevID,@Lf,@Hf,@Activate,@Voltage,@DateTime)";
SQLiteCommand cmd = new SQLiteCommand(sql, m_dbConnection);
cmd.Parameters.Add("TagID", DbType.String);
cmd.Parameters.Add("DevID", DbType.String);
cmd.Parameters.Add("Lf", DbType.Int32);
cmd.Parameters.Add("Hf", DbType.Int32);
cmd.Parameters.Add("Activate", DbType.Int32);
cmd.Parameters.Add("Voltage", DbType.Int32);
cmd.Parameters.Add("DateTime", DbType.DateTime);
foreach (var item in rf)
{
cmd.Parameters["TagID"].Value = item.TagID;
cmd.Parameters["DevID"].Value = item.DevID;
cmd.Parameters["Lf"].Value = item.Lf;
cmd.Parameters["Hf"].Value = item.Hf;
cmd.Parameters["Activate"].Value = item.Activate;
cmd.Parameters["Voltage"].Value = item.Voltage;
cmd.Parameters["DateTime"].Value = DateTime.Now;
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
Logger.Error("****插入数据到sqlite错误:****" + ex.ToString());
}
finally
{
m_dbConnection.Close();
Logger.Debug("****关闭sqlite成功********");
}
}