删除
/// <summary>
/// 删除的方法
/// </summary>
/// <param name="id">主键列</param>
/// <returns></returns>
public static int Delect(int id)
{
//删除用sql语句
string sql = "delete from Product where Id =" + id;
//调用sqlhelper增删改方法,直接返回影响行数
return SqlHelPer.NonQuery(sql);
}
查询,在查询中我们应要注意,查询应该使用什么样的查询,是需要去获取一张表(一般用来给GridView赋值),还是一个model层的数据类(一般是为了修改选中列跳转后给各个控件赋值),这样我们就知道需要两种查询方法
第一种给Grid View赋值:
/// <summary>
/// 查询
/// </summary>
/// <param name="Id">产品编号</param>
/// <param name="ProductName">产品名称</param>
/// 其中的参数可以安照需要进行更改
/// <returns></returns>
public static DataTable Select(int Id = 0, string ProductName = "")
{
//查询sql语句
var sql = "select Product.*,Name from ProductCategory,Product where ProductCategory.Id=Product.CategoryId ";//注意结尾处空格
//判断是否符合要求注意如果判断条件及该字段默认值一定是表中没有的
if (Id != 0)
{
//注意and前的空格
sql += " and Product.CategoryId=" + Id;
}
//同上
if (ProductName != "")
{
sql += string.Format(" and ProductName like '%{0}%'", ProductName);
}
//返回查询结果,结果是一张表
return SqlHelPer.Query(sql);
}
第二种,跳转后给各个控件赋值
/// <summary>
/// 查询单行,编辑的前置查询
/// </summary>
/// <param name="id">产品编号</param>
///注意返回值是查询到的表对应在model层的类
/// <returns></returns>
public static Product SelectID(int id)
{
//查询sql语句
var sql = "select*from Product where Id=" + id;
//将查询到的表赋值给创建的表table
DataTable table = SqlHelPer.Query(sql);
//判断表中的数据列是否为空,如果为空则返回null
if (table.Rows.Count < 1)
return null;
//将获取到的列赋值给DataROW类型的row
DataRow row = table.Rows[0];
//給需要返回的类赋值
Product product = new Product
{
//int类型
列名= Convert.ToInt32(row["列名"]),
//string类型
列名 = Convert.ToString(row["列名"]),
//Double类型
SellingPrice = Convert.ToDouble(row["SellingPrice"])
};
//返回类
return product;
}
修改的方法
/// <summary>
/// 修改的方法
/// </summary>
/// <param name="product">product类</param>
/// <returns></returns>
public static int Update(Product product)
{
//修改sql语句,传入对应类的数据,及该类的数据是从对应页面控件中获取到的数据如:product.MarketPrice=TxtMarketPrice;
//且该类是从model类中实例化的一个类,在编辑页面后台进行对应的实例化和赋值
var sql = string.Format("update Product set ProductName='{0}',MarketPrice={1},SellingPrice={2},CategoryId={3},Introduction='{4}',IsOnSale={5} where Id={6}"
, product.ProductName, product.MarketPrice, product.SellingPrice, product.CategoryId, product.Introduction, product.IsOnSale, product.Id);
return SqlHelPer.NonQuery(sql);
}
sqlhelper类链接