C# sqlhelper 通用类 实现方法

 public class Produce
    {
        #region 增加单条记录
        public static  int ProductAdd(produce product)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" insert into  dbo.produce ( CategoryID,produceName,producePrice,produceStock,produceDescription)");
            strSql.Append(" values (@CategoryID,@produceName,@producePrice,@produceStock,@produceDescription)");
            SqlParameter[] parmaeter =    { 
                                       new SqlParameter("@CategoryID",SqlDbType.Int,4),
                                       new SqlParameter("@produceName",SqlDbType.VarChar,100),
                                       new SqlParameter("@producePrice",SqlDbType.Decimal,9),
                                       new SqlParameter("@produceStock",SqlDbType.Int,4),
                                       new SqlParameter("@produceDescription",SqlDbType.VarChar,200),
                                  
                                  
                                   };
            parmaeter[0].Value = product.CategoryID;
            parmaeter[1].Value = product.ProduceName;
            parmaeter[2].Value = product.ProducePrice;
            parmaeter[3].Value = product.ProduceStock;
            parmaeter[3].Value = product.ProduceDescription;
            return SQLHelper.ExecuteSql(strSql.ToString(), parmaeter);


        }
        #endregion

        #region 修改一条记录
        public static bool ProductUpdate(produce product)
        {
            bool isError = false;
            try
            {

                StringBuilder strSql = new StringBuilder();
                strSql.Append(" update  dbo.produce set CategoryID=@CategoryID,produceName=@produceName");
                strSql.Append(" ,producePrice=@producePrice, produceStock=@produceStock,produceDescription=@produceDescription");
                strSql.Append(" where produceID =6");
                SqlParameter[] parmaeter =    { 
                                       new SqlParameter("@CategoryID",SqlDbType.Int,4),
                                       new SqlParameter("@produceName",SqlDbType.VarChar,100),
                                       new SqlParameter("@producePrice",SqlDbType.Decimal,9),
                                       new SqlParameter("@produceStock",SqlDbType.Int,4),
                                       new SqlParameter("@produceDescription",SqlDbType.VarChar,200),
                                  
                                  
                                   };
                parmaeter[0].Value = product.CategoryID;
                parmaeter[1].Value = product.ProduceName;
                parmaeter[2].Value = product.ProducePrice;
                parmaeter[3].Value = product.ProduceStock;
                parmaeter[3].Value = product.ProduceDescription;
                int i = SQLHelper.ExecuteSql(strSql.ToString(), parmaeter);
                if (i > 0)
                {
                    isError = true;
                }
            }
            catch (Exception ex)
            {
                isError = false;
            }
            return isError;

        }
        #endregion

        #region 得到一个实体
        public  static  produce GetproduceModel(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select top 1 * from dbo.View_Produce");
            strSql.Append(" where  produceID = " + ID + " ");
            // strSql.Append(" where  produceID = @produceID");
            //SqlParameter[] parameter = {
            //                           new SqlParameter("@produceID",SqlDbType.Int ,4)
            //                           };
            //   parameter[0].Value = ID;
            produce product = new produce();//实例化
            DataSet ds = SQLHelper.ExecuteDs(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["produceID"].ToString() != null)
                {
                    product.ProduceID = Convert.ToInt32(ds.Tables[0].Rows[0]["produceID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CategoryID"].ToString() != null)
                {
                    product.CategoryID = Convert.ToInt32(ds.Tables[0].Rows[0]["CategoryID"].ToString());

                }
                product.ProduceName = ds.Tables[0].Rows[0]["produceName"].ToString();
                product.ProducePrice = Convert.ToDecimal(ds.Tables[0].Rows[0]["producePrice"].ToString());
                product.ProduceStock = Convert.ToInt32(ds.Tables[0].Rows[0]["produceStock"].ToString());
                product.ProduceDescription = ds.Tables[0].Rows[0]["produceDescription"].ToString();

            }
            else
            {
                return null;
            }
            return product;

        }
        #endregion

        #region 得到列表
        public  DataTable GetTable()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select top * from dbo.View_Produce");
            DataTable  ds = SQLHelper.ExecuteDt(strSql.ToString());
            return ds;
        }
        #endregion

    }

原文地址:https://www.cnblogs.com/dullbaby/p/2950020.html