通过nuget安装 system.data.sqlite
Sqlitehelper中的方法
- using System.Data.SQLite;
-
- public static List<T> ExecuteQueryList<T>(string sqlStr, params SQLiteParameter[] parameters) where T : class, new()
- {
- List<T> list = new List<T>();
- using (SQLiteConnection conn = new SQLiteConnection(connStr))
- {
- conn.Open();
- using (SQLiteCommand cmd = conn.CreateCommand())
- {
- cmd.CommandText = sqlStr;
- foreach (SQLiteParameter parameter in parameters)
- {
- cmd.Parameters.Add(parameter);
- }
-
-
- SQLiteDataReader reader = cmd.ExecuteReader();
- while (reader.Read())
- {
- T obj = new T();
- foreach (PropertyInfo info in obj.GetType().GetProperties())
- {
- info.SetValue(obj, reader[info.Name], null);
- }
- list.Add(obj);
- }
- }
- }
-
- return list;
- }
使用方法
List<PLCDataModel> l = SqliteHelper.ExecuteQueryList<PLCDataModel>(sql);