/// <summary> /// 得到一个对象实体 /// </summary> public Model.Web_Products DataRowToModel(DataRow row) { Model.Web_Products model = new Model.Web_Products(); if (row != null) { if (row["Id"] != null && row["Id"].ToString() != "") { model.Id = int.Parse(row["Id"].ToString()); } if (row["Price"] != null) { model.Price = row["Price"].ToString(); } if (row["Amount"] != null) { model.Amount = row["Amount"].ToString(); } if (row["ProductName"] != null) { model.ProductName = row["ProductName"].ToString(); } if (row["ProductUnit"] != null) { model.ProductUnit = row["ProductUnit"].ToString(); } if (row["Total"] != null) { model.Total = row["Total"].ToString(); } } return(model); }
/// <summary> /// 增加一条数据 /// </summary> public int Add(Model.Web_Products model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into Web_Products("); strSql.Append("Price,Amount,ProductName,ProductUnit,Total)"); strSql.Append(" values ("); strSql.Append("@Price,@Amount,@ProductName,@ProductUnit,@Total)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@Price", SqlDbType.NVarChar, 50), new SqlParameter("@Amount", SqlDbType.NVarChar, 50), new SqlParameter("@ProductName", SqlDbType.NVarChar, 50), new SqlParameter("@ProductUnit", SqlDbType.NVarChar, 50), new SqlParameter("@Total", SqlDbType.NVarChar, 50) }; parameters[0].Value = model.Price; parameters[1].Value = model.Amount; parameters[2].Value = model.ProductName; parameters[3].Value = model.ProductUnit; parameters[4].Value = model.Total; object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters); if (obj == null) { return(0); } else { return(Convert.ToInt32(obj)); } }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Model.Web_Products model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update Web_Products set "); strSql.Append("Price=@Price,"); strSql.Append("Amount=@Amount,"); strSql.Append("ProductName=@ProductName,"); strSql.Append("ProductUnit=@ProductUnit,"); strSql.Append("Total=@Total"); strSql.Append(" where Id=@Id"); SqlParameter[] parameters = { new SqlParameter("@Price", SqlDbType.NVarChar, 50), new SqlParameter("@Amount", SqlDbType.NVarChar, 50), new SqlParameter("@ProductName", SqlDbType.NVarChar, 50), new SqlParameter("@ProductUnit", SqlDbType.NVarChar, 50), new SqlParameter("@Total", SqlDbType.NVarChar, 50), new SqlParameter("@Id", SqlDbType.Int, 4) }; parameters[0].Value = model.Price; parameters[1].Value = model.Amount; parameters[2].Value = model.ProductName; parameters[3].Value = model.ProductUnit; parameters[4].Value = model.Total; parameters[5].Value = model.Id; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return(true); } else { return(false); } }
/// <summary> /// 得到一个对象实体 /// </summary> public Model.Web_Products GetModel(int Id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 Id,Price,Amount,ProductName,ProductUnit,Total from Web_Products "); strSql.Append(" where Id=@Id"); SqlParameter[] parameters = { new SqlParameter("@Id", SqlDbType.Int, 4) }; parameters[0].Value = Id; Model.Web_Products model = new Model.Web_Products(); DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { return(DataRowToModel(ds.Tables[0].Rows[0])); } else { return(null); } }