using System.Data.SqlClient;
/// <summary>
///ProductOper 的摘要说明
/// </summary>
public class ProductOper
{
/// <summary>
/// 1,GetAll
/// </summary>
/// <returns></returns>
public static IList<ProductInfo> GetAll()
{
IList<ProductInfo> dals = new List<ProductInfo>();
string sql = "select productId,productName,unitprice,special,categoryId from Product order by productId desc";
//1,创建连接对象
SqlConnection con = new DBConnection().Con;
//2,创建命令对象
SqlCommand cmd = con.CreateCommand();
//3,把sql语句付给命令对象
cmd.CommandText = sql;
//4,打开数据连接
con.Open();
try
{
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
ProductInfo dal = new ProductInfo()
{
ProductId = sdr.GetInt32(0),
ProductName = sdr.GetString(1),
Unitprice = sdr.GetDecimal(2),
Special = sdr.GetString(3),
CategoryId = sdr.GetInt32(4)
};
dals.Add(dal);
}
}
}
finally
{
//,关闭数据连接(释放资源)
con.Close();
}
return dals;
}