Sqlite查询结果为List<T>

通过nuget安装 system.data.sqlite 

Sqlite查询结果为List<T>_第1张图片 

Sqlitehelper中的方法 

using System.Data.SQLite; 

   public static List ExecuteQueryList(string sqlStr, params SQLiteParameter[] parameters) where T : class, new()
   {
       List list = new List();
       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 l = SqliteHelper.ExecuteQueryList(sql);

你可能感兴趣的:(#,SQLite,windows,sqlite)