public bool Insert(User tModel) { using (SqlCommand command = new DbHelper().Command) { command.CommandText = "insert into eduuser(userid,userpass,name,age,birthday) values (@userid,@userpass,@name,@age,@birthday)"; command.Parameters.AddRange(CreateSqlParameters(tModel)); int ret = command.ExecuteNonQuery(); return ret > 0; } }
public User GetOne(Where @where, string fields) { using (SqlCommand command = new DbHelper().Command) { User user = new User(); string sql = $"select top 1 {fields} from {Table} where {where.Result};"; IDataReader reader = command.ExecuteReaderExt(sql, where); if (reader.Read()) { user = ReaderModel(reader, fields); } return user; } }
private SqlParameter[] CreateSqlParameters(User tModel) { SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@userid", SqlDbType.VarChar), new SqlParameter("@userpass", SqlDbType.VarChar), new SqlParameter("@name", SqlDbType.NVarChar), new SqlParameter("@age", SqlDbType.Int), new SqlParameter("@birthday", SqlDbType.DateTime) }; parameters[0].Value = tModel.UserID; parameters[1].Value = tModel.UserPass; parameters[2].Value = tModel.Name; parameters[3].Value = tModel.Age; parameters[4].Value = tModel.Birthday; return parameters; }
public User ReaderModel(IDataReader reader, string fields) { #region check fields string flds = (string.IsNullOrEmpty(fields)) ? "*" : fields.Trim(); IEnumerable<string> fieldList = new List<string>(); if (flds != "*") { fieldList = flds.Split(',').Select(field => field.Trim()); } #endregion User user = new User(); if (CheckField(flds, fieldList, "ID")) { user.ID = reader.GetInt32(reader.GetOrdinal("ID")); } if (CheckField(flds, fieldList, "Age")) { user.Age = reader.GetInt32(reader.GetOrdinal("Age")); } if (CheckField(flds, fieldList, "Birthday")) { user.Birthday = reader.GetDateTime(reader.GetOrdinal("Birthday")); } if (CheckField(flds, fieldList, "Name")) { user.Name = reader.GetString(reader.GetOrdinal("Name")); } if (CheckField(flds, fieldList, "UserID")) { user.UserID = reader.GetString(reader.GetOrdinal("UserID")); } if (CheckField(flds, fieldList, "UserPass")) { user.UserPass = reader.GetString(reader.GetOrdinal("UserPass")); } return user; }
public static async Task<bool> InsertUser(User tModel) { return await Task<bool>.Factory.StartNew(() => Dal.Insert(tModel)); }