需要的可以去下载
http://win.51aspx.com/CV/ClassGenerateProper/
或
http://singleyw.download.csdn.net/
可方便生成实体类、数据访问类、数据库文档
代码要想有注释需要在数据库里写上说明(表和字段都可以在属性里写说明)
这里面需要用到 通用数据访问组件
需要的可以去下载
http://win.51aspx.com/CV/CommonTest/
或
http://singleyw.download.csdn.net/
效果如下
Model代码如下:
Model 代码示例
using System;
using System.Data;
/// <summary>
/// 产品信息
/// </summary>
[Serializable]
public class TProduct
{
private int pid;
public int Pid
{
get{ return pid; }
set{ pid = value; }
}
/// <summary>
/// 产品名称
/// </summary>
private string pName;
/// <summary>
/// 产品名称
/// </summary>
public string PName
{
get{ return pName; }
set{ pName = value; }
}
/// <summary>
/// 价格
/// </summary>
private double price;
/// <summary>
/// 价格
/// </summary>
public double Price
{
get{ return price; }
set{ price = value; }
}
/// <summary>
/// 分类ID(tClass.classID)
/// </summary>
private int classid;
/// <summary>
/// 分类ID(tClass.classID)
/// </summary>
public int Classid
{
get{ return classid; }
set{ classid = value; }
}
/// <summary>
/// 图片名称
/// </summary>
private string photo;
/// <summary>
/// 图片名称
/// </summary>
public string Photo
{
get{ return photo; }
set{ photo = value; }
}
/// <summary>
/// 产品信息描述
/// </summary>
private string description;
/// <summary>
/// 产品信息描述
/// </summary>
public string Description
{
get{ return description; }
set{ description = value; }
}
/// <summary>
/// 卖家ID
/// </summary>
private Int64 seller;
/// <summary>
/// 卖家ID
/// </summary>
public Int64 Seller
{
get{ return seller; }
set{ seller = value; }
}
}
ADP代码如下:
ADO 代码示例
using System;
using System.Data;
using Common.DataAccess;
using System.Collections.Generic;
/// <summary>
/// 产品信息
/// </summary>
[Serializable]
public class ADOTProduct
{
IDBOperator idb = DBOperator.GetInstance();
/// <summary>
/// 添加产品信息 TProduct对象(即:一条记录)
/// </summary>
public int Add(TProduct tProduct)
{
string sql = "INSERT INTO tProduct (pName,price,classid,photo,description,seller) VALUES (@pName,@price,@classid,@photo,@description,@seller)";
if (string.IsNullOrEmpty(tProduct.PName))
{
idb.AddParameter("@pName", "''");
}
else
{
idb.AddParameter("@pName", tProduct.PName);
}
if (tProduct.Price == 0)
{
idb.AddParameter("@price", 0);
}
else
{
idb.AddParameter("@price", tProduct.Price);
}
if (tProduct.Classid == 0)
{
idb.AddParameter("@classid", 0);
}
else
{
idb.AddParameter("@classid", tProduct.Classid);
}
if (string.IsNullOrEmpty(tProduct.Photo))
{
idb.AddParameter("@photo", DBNull.Value);
}
else
{
idb.AddParameter("@photo", tProduct.Photo);
}
if (string.IsNullOrEmpty(tProduct.Description))
{
idb.AddParameter("@description", DBNull.Value);
}
else
{
idb.AddParameter("@description", tProduct.Description);
}
if (tProduct.Seller == 0)
{
idb.AddParameter("@seller", 0);
}
else
{
idb.AddParameter("@seller", tProduct.Seller);
}
return idb.ExeCmd(sql);
}
/// <summary>
/// 更新产品信息 TProduct对象(即:一条记录
/// </summary>
public int Update(TProduct tProduct)
{
string sql = @"UPDATE tProduct SET
pName=@pName,price=@price,classid=@classid,photo=@photo,description=@description,seller=@seller WHERE 1=1
and pid=@pid";
if (string.IsNullOrEmpty(tProduct.PName))
{
idb.AddParameter("@pName", "''");
}
else
{
idb.AddParameter("@pName", tProduct.PName);
}
if (tProduct.Price == 0)
{
idb.AddParameter("@price", 0);
}
else
{
idb.AddParameter("@price", tProduct.Price);
}
if (tProduct.Classid == 0)
{
idb.AddParameter("@classid", 0);
}
else
{
idb.AddParameter("@classid", tProduct.Classid);
}
if (string.IsNullOrEmpty(tProduct.Photo))
{
idb.AddParameter("@photo", DBNull.Value);
}
else
{
idb.AddParameter("@photo", tProduct.Photo);
}
if (string.IsNullOrEmpty(tProduct.Description))
{
idb.AddParameter("@description", DBNull.Value);
}
else
{
idb.AddParameter("@description", tProduct.Description);
}
if (tProduct.Seller == 0)
{
idb.AddParameter("@seller", 0);
}
else
{
idb.AddParameter("@seller", tProduct.Seller);
}
idb.AddParameter("@pid", tProduct.Pid);
return idb.ExeCmd(sql);
}
/// <summary>
/// 删除产品信息 TProduct对象(即:一条记录
/// </summary>
public int Delete(int pid)
{
string sql = "DELETE tProduct WHERE 1=1 AND pid=@pid ";
idb.AddParameter("@pid", pid);
return idb.ExeCmd(sql);
}
/// <summary>
/// 获取指定的产品信息 TProduct对象(即:一条记录
/// </summary>
public TProduct GetByKey(int pid)
{
TProduct tProduct = new TProduct();
string sql = "SELECT pid,pName,price,classid,photo,description,seller FROM tProduct WHERE 1=1 AND pid=@pid ";
idb.AddParameter("@pid", pid);
DataTable dt = idb.ReturnDataTable(sql);
foreach (DataRow dr in dt.Rows)
{
if (dr["pid"] != DBNull.Value) tProduct.Pid = Convert.ToInt32(dr["pid"]);
if (dr["pName"] != DBNull.Value) tProduct.PName = Convert.ToString(dr["pName"]);
if (dr["price"] != DBNull.Value) tProduct.Price = Convert.ToDouble(dr["price"]);
if (dr["classid"] != DBNull.Value) tProduct.Classid = Convert.ToInt32(dr["classid"]);
if (dr["photo"] != DBNull.Value) tProduct.Photo = Convert.ToString(dr["photo"]);
if (dr["description"] != DBNull.Value) tProduct.Description = Convert.ToString(dr["description"]);
if (dr["seller"] != DBNull.Value) tProduct.Seller = Convert.ToInt64(dr["seller"]);
}
return tProduct;
}
/// <summary>
/// 获取指定的产品信息 TProduct对象集合
/// </summary>
public List<TProduct> GetListByWhere(string strCondition)
{
List<TProduct> ret = new List<TProduct>();
string sql = "SELECT pid,pName,price,classid,photo,description,seller FROM tProduct WHERE 1=1 ";
if(!string.IsNullOrEmpty(strCondition))
{
strCondition.Replace('\'','"'); //防sql注入
sql += strCondition ;
}
DataTable dt = idb.ReturnDataTable(sql);
foreach (DataRow dr in dt.Rows)
{
TProduct tProduct = new TProduct();
if (dr["pid"] != DBNull.Value) tProduct.Pid = Convert.ToInt32(dr["pid"]);
if (dr["pName"] != DBNull.Value) tProduct.PName = Convert.ToString(dr["pName"]);
if (dr["price"] != DBNull.Value) tProduct.Price = Convert.ToDouble(dr["price"]);
if (dr["classid"] != DBNull.Value) tProduct.Classid = Convert.ToInt32(dr["classid"]);
if (dr["photo"] != DBNull.Value) tProduct.Photo = Convert.ToString(dr["photo"]);
if (dr["description"] != DBNull.Value) tProduct.Description = Convert.ToString(dr["description"]);
if (dr["seller"] != DBNull.Value) tProduct.Seller = Convert.ToInt64(dr["seller"]);
ret.Add(tProduct);
}
return ret;
}
/// <summary>
/// 获取所有的产品信息 TProduct对象(即:一条记录
/// </summary>
public List<TProduct> GetAll()
{
List<TProduct> ret = new List<TProduct>();
string sql = "SELECT pid,pName,price,classid,photo,description,seller FROM tProduct ";
DataTable dt = idb.ReturnDataTable(sql);
foreach (DataRow dr in dt.Rows)
{
TProduct tProduct = new TProduct();
if (dr["pid"] != DBNull.Value) tProduct.Pid = Convert.ToInt32(dr["pid"]);
if (dr["pName"] != DBNull.Value) tProduct.PName = Convert.ToString(dr["pName"]);
if (dr["price"] != DBNull.Value) tProduct.Price = Convert.ToDouble(dr["price"]);
if (dr["classid"] != DBNull.Value) tProduct.Classid = Convert.ToInt32(dr["classid"]);
if (dr["photo"] != DBNull.Value) tProduct.Photo = Convert.ToString(dr["photo"]);
if (dr["description"] != DBNull.Value) tProduct.Description = Convert.ToString(dr["description"]);
if (dr["seller"] != DBNull.Value) tProduct.Seller = Convert.ToInt64(dr["seller"]);
ret.Add(tProduct);
}
return ret;
}
}
数据库文档如下: