C#操作SQLite数据库增、删、改、查 欢迎转载
转载记得留下链接地址哦!!!
最近项目上要使用SQLite数据库,不怕大伙笑话毕业四年多了,一直使用Oracle或者MySQL或者SQLServer,但是真的是没有用过SQLite数据库,据说非常轻量级,但是真没有用过,于是网上大概搜索下,例子比较多,但是么有一个完整的,即使有,也五花八门。。看的人头晕,可能对于他们大神来讲这都太简单了,算求了更新一篇,新来的小伙伴请看下文,大神你就直接飘过吧!
本例子运行效果
正式开始吧
1.SQLite的下载安装
自然官网下载对应版本即可,我这里下载的是 Precompiled Binaries for 32-bit Windows (.NET Framework 4.0) 不愿意下载的 可以直接使用我下载过的 点击这里下载
2.减压找出我们所需要的库文件 这里我们只需要俩个文件下图所示
3.下载一个数据库管理系统来创建数据库
我是用的是SQLiteStudio3.1.1版本 自己下载吧 下载地址:https://sqlitestudio.pl/index.rvt?act=download
4.创建数据库以及表
这里创建一个简单的表 UserInfo 完了代码中会有这个数据库 在Debug\bin目录下
SQLite数据库文件就一个后缀为.db的文件,就没了。。
通过SQLiteStudio打开该数据库
5.开始使用大伙所熟悉的VS了创建相关类
userinfo的Mode类
1 /// <summary> 2 /// UserInfo:实体类(属性说明自动提取数据库字段的描述信息) 3 /// </summary> 4 [Serializable] 5 public partial class UserInfo 6 { 7 public UserInfo() 8 { } 9 #region Model 10 private int _id; 11 private string _username; 12 private string _pwd; 13 private int? _age; 14 /// <summary> 15 /// 16 /// </summary> 17 public int ID 18 { 19 set { _id = value; } 20 get { return _id; } 21 } 22 /// <summary> 23 /// 24 /// </summary> 25 public string UserName 26 { 27 set { _username = value; } 28 get { return _username; } 29 } 30 /// <summary> 31 /// 32 /// </summary> 33 public string Pwd 34 { 35 set { _pwd = value; } 36 get { return _pwd; } 37 } 38 /// <summary> 39 /// 40 /// </summary> 41 public int? Age 42 { 43 set { _age = value; } 44 get { return _age; } 45 } 46 #endregion Model 47 48 }
UserInfo 的Dal类
1 /// <summary> 2 /// 数据访问类:UserInfo 3 /// </summary> 4 public partial class UserInfo 5 { 6 public UserInfo() 7 { } 8 #region BasicMethod 9 10 /// <summary> 11 /// 得到最大ID 12 /// </summary> 13 public int GetMaxId() 14 { 15 return DbHelperSQLite.GetMaxID("ID", "UserInfo"); 16 } 17 18 /// <summary> 19 /// 是否存在该记录 20 /// </summary> 21 public bool Exists(int ID) 22 { 23 StringBuilder strSql = new StringBuilder(); 24 strSql.Append("select count(1) from UserInfo"); 25 strSql.Append(" where ID=@ID "); 26 SQLiteParameter[] parameters = { 27 new SQLiteParameter("@ID", DbType.Int32,8) }; 28 parameters[0].Value = ID; 29 30 return DbHelperSQLite.Exists(strSql.ToString(), parameters); 31 } 32 33 34 /// <summary> 35 /// 增加一条数据 36 /// </summary> 37 public bool Add(Model.UserInfo model) 38 { 39 StringBuilder strSql = new StringBuilder(); 40 strSql.Append("insert into UserInfo("); 41 strSql.Append("UserName,Pwd,Age)"); 42 strSql.Append(" values ("); 43 strSql.Append("@UserName,@Pwd,@Age)"); 44 SQLiteParameter[] parameters = { 45 new SQLiteParameter("@UserName", DbType.String,50), 46 new SQLiteParameter("@Pwd", DbType.String,25), 47 new SQLiteParameter("@Age", DbType.Int32,8)}; 48 parameters[0].Value = model.UserName; 49 parameters[1].Value = model.Pwd; 50 parameters[2].Value = model.Age; 51 52 int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters); 53 if (rows > 0) 54 { 55 return true; 56 } 57 else 58 { 59 return false; 60 } 61 } 62 /// <summary> 63 /// 更新一条数据 64 /// </summary> 65 public bool Update(Model.UserInfo model) 66 { 67 StringBuilder strSql = new StringBuilder(); 68 strSql.Append("update UserInfo set "); 69 strSql.Append("UserName=@UserName,"); 70 strSql.Append("Pwd=@Pwd,"); 71 strSql.Append("Age=@Age"); 72 strSql.Append(" where ID=@ID "); 73 SQLiteParameter[] parameters = { 74 new SQLiteParameter("@UserName", DbType.String,50), 75 new SQLiteParameter("@Pwd", DbType.String,25), 76 new SQLiteParameter("@Age", DbType.Int32,8), 77 new SQLiteParameter("@ID", DbType.Int32,8)}; 78 parameters[0].Value = model.UserName; 79 parameters[1].Value = model.Pwd; 80 parameters[2].Value = model.Age; 81 parameters[3].Value = model.ID; 82 83 int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters); 84 if (rows > 0) 85 { 86 return true; 87 } 88 else 89 { 90 return false; 91 } 92 } 93 94 /// <summary> 95 /// 删除一条数据 96 /// </summary> 97 public bool Delete(int ID) 98 { 99 100 StringBuilder strSql = new StringBuilder(); 101 strSql.Append("delete from UserInfo "); 102 strSql.Append(" where ID=@ID "); 103 SQLiteParameter[] parameters = { 104 new SQLiteParameter("@ID", DbType.Int32,8) }; 105 parameters[0].Value = ID; 106 107 int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters); 108 if (rows > 0) 109 { 110 return true; 111 } 112 else 113 { 114 return false; 115 } 116 } 117 /// <summary> 118 /// 批量删除数据 119 /// </summary> 120 public bool DeleteList(string IDlist) 121 { 122 StringBuilder strSql = new StringBuilder(); 123 strSql.Append("delete from UserInfo "); 124 strSql.Append(" where ID in (" + IDlist + ") "); 125 int rows = DbHelperSQLite.ExecuteSql(strSql.ToString()); 126 if (rows > 0) 127 { 128 return true; 129 } 130 else 131 { 132 return false; 133 } 134 } 135 136 137 /// <summary> 138 /// 得到一个对象实体 139 /// </summary> 140 public Model.UserInfo GetModel(int ID) 141 { 142 143 StringBuilder strSql = new StringBuilder(); 144 strSql.Append("select ID,UserName,Pwd,Age from UserInfo "); 145 strSql.Append(" where ID=@ID "); 146 SQLiteParameter[] parameters = { 147 new SQLiteParameter("@ID", DbType.Int32,8) }; 148 parameters[0].Value = ID; 149 150 Model.UserInfo model = new Model.UserInfo(); 151 DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters); 152 if (ds.Tables[0].Rows.Count > 0) 153 { 154 return DataRowToModel(ds.Tables[0].Rows[0]); 155 } 156 else 157 { 158 return null; 159 } 160 } 161 162 163 /// <summary> 164 /// 得到一个对象实体 165 /// </summary> 166 public Model.UserInfo DataRowToModel(DataRow row) 167 { 168 Model.UserInfo model = new Model.UserInfo(); 169 if (row != null) 170 { 171 if (row["ID"] != null && row["ID"].ToString() != "") 172 { 173 model.ID = int.Parse(row["ID"].ToString()); 174 } 175 if (row["UserName"] != null) 176 { 177 model.UserName = row["UserName"].ToString(); 178 } 179 if (row["Pwd"] != null) 180 { 181 model.Pwd = row["Pwd"].ToString(); 182 } 183 if (row["Age"] != null && row["Age"].ToString() != "") 184 { 185 model.Age = int.Parse(row["Age"].ToString()); 186 } 187 } 188 return model; 189 } 190 191 /// <summary> 192 /// 获得数据列表 193 /// </summary> 194 public DataSet GetList(string strWhere) 195 { 196 StringBuilder strSql = new StringBuilder(); 197 strSql.Append("select ID,UserName,Pwd,Age "); 198 strSql.Append(" FROM UserInfo "); 199 if (strWhere.Trim() != "") 200 { 201 strSql.Append(" where " + strWhere); 202 } 203 return DbHelperSQLite.Query(strSql.ToString()); 204 } 205 206 /// <summary> 207 /// 获取记录总数 208 /// </summary> 209 public int GetRecordCount(string strWhere) 210 { 211 StringBuilder strSql = new StringBuilder(); 212 strSql.Append("select count(1) FROM UserInfo "); 213 if (strWhere.Trim() != "") 214 { 215 strSql.Append(" where " + strWhere); 216 } 217 object obj = DbHelperSQLite.GetSingle(strSql.ToString()); 218 if (obj == null) 219 { 220 return 0; 221 } 222 else 223 { 224 return Convert.ToInt32(obj); 225 } 226 } 227 /// <summary> 228 /// 分页获取数据列表 229 /// </summary> 230 public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex) 231 { 232 StringBuilder strSql = new StringBuilder(); 233 strSql.Append("SELECT * FROM ( "); 234 strSql.Append(" SELECT ROW_NUMBER() OVER ("); 235 if (!string.IsNullOrEmpty(orderby.Trim())) 236 { 237 strSql.Append("order by T." + orderby); 238 } 239 else 240 { 241 strSql.Append("order by T.ID desc"); 242 } 243 strSql.Append(")AS Row, T.* from UserInfo T "); 244 if (!string.IsNullOrEmpty(strWhere.Trim())) 245 { 246 strSql.Append(" WHERE " + strWhere); 247 } 248 strSql.Append(" ) TT"); 249 strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex); 250 return DbHelperSQLite.Query(strSql.ToString()); 251 } 252 253 #endregion BasicMethod 254 }
Userinfo的BLL类
1 public partial class UserInfo 2 { 3 private readonly DAL.UserInfo dal = new DAL.UserInfo(); 4 public UserInfo() 5 { } 6 #region BasicMethod 7 8 /// <summary> 9 /// 得到最大ID 10 /// </summary> 11 public int GetMaxId() 12 { 13 return dal.GetMaxId(); 14 } 15 16 /// <summary> 17 /// 是否存在该记录 18 /// </summary> 19 public bool Exists(int ID) 20 { 21 return dal.Exists(ID); 22 } 23 24 /// <summary> 25 /// 增加一条数据 26 /// </summary> 27 public bool Add(Model.UserInfo model) 28 { 29 return dal.Add(model); 30 } 31 32 /// <summary> 33 /// 更新一条数据 34 /// </summary> 35 public bool Update(Model.UserInfo model) 36 { 37 return dal.Update(model); 38 } 39 40 /// <summary> 41 ///
请发表评论