主要接口代码:
//根据Id得到标题
string GetTitle(int nItemId);
//获得点击率比较高的商品
IList<ItemInfo> GetItemByClickTime();
//通过商品的ID获得商品的详细资料
IList<ItemInfo> GetItemById(int nItemId);
//通过Id获得详细信息 返回数据表
DataTable GetItemDetailById(string nItemId);
//通过Id更新点击数
int UpdateClick(int nId);
//新加一个订单
int InsertOrder(OrderInfo nOrder);
//更新详细资料
int UpdateDataOrder(OrderInfo nOrder, string nId);
//根据Id删除订单
int DeleteOrder(string nId);
//根据用户名及密码验证用户
bool CheckUser(string nName, string nPass);
//获取所有产品类别
DataTable GetAllCategory();
//根据类别IP获取
DataTable GetCategory(string categoryId);
//是否存在
bool Exist(string filter);
相应的SQLServerDAL 代码:
//根据Id得到标题
public string GetTitle(int nItemId)
{
SqlParameter[] nParas ={ new SqlParameter("@nId", SqlDbType.Int, 4) };
nParas[0].Value = nItemId;
try
{
string txtTitle = SQLHelper.ExecuteSclare(SQLHelper.txtConnecttionString, CommandType.StoredProcedure, "SelectItemTitle", nParas).ToString();
return txtTitle;
}
catch (SqlException ex)
{
throw new Exception(ex.Message,ex);
}
}
//获得点击率比较高的商品
public IList<ItemInfo> GetItemByClickTime()
{
IList<ItemInfo> item = new List<ItemInfo>();
using (SqlDataReader dr = SQLHelper.ExecuteReader(SQLHelper.txtConnecttionString, CommandType.StoredProcedure, "SelectItemByClickTime", null))
{
while (dr.Read())
{
ItemInfo itemInfo = new ItemInfo(dr.GetInt32(0), dr.GetString(1), dr.GetDecimal(2), dr.GetDecimal(3), dr.GetDecimal(4), dr.GetString(5));
item.Add(itemInfo);
}
}
return item;
}
//通过商品的ID获得商品的详细资料
public IList<ItemInfo> GetItemById(int nId)
{
IList<ItemInfo> item = new List<ItemInfo>();
SqlParameter[] nParas = { new SqlParameter("@Id", SqlDbType.Int, 4) };
nParas[0].Value = nId;
using (SqlDataReader dr = SQLHelper.ExecuteReader(SQLHelper.txtConnecttionString, CommandType.StoredProcedure, "SelectItemByID", nParas))
{
while (dr.Read())
{
ItemInfo itemInfo = new ItemInfo(dr.GetInt32(0), dr.GetString(1), dr.GetString(2), dr.GetDecimal(3), dr.GetDecimal(4), dr.GetDecimal(5), dr.GetString(6), dr.GetString(7), dr.GetString(8), dr.GetInt32(9), dr.GetInt32(10), dr.GetInt32(11), dr.GetString(12), dr.GetString(13), dr.GetString(14), dr.GetBoolean(15));
item.Add(itemInfo);
}
}
return item;
}
//通过Id获得详细信息 返回数据表
public DataTable GetItemDetailById(string nItemId)
{
SqlParameter[] para = { new SqlParameter("@Id", SqlDbType.Int, 4) };
para[0].Value = nItemId;
SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.txtConnecttionString, CommandType.StoredProcedure, "SelectItemByID", para);
DataTable table = new DataTable();
table.Load(reader);
reader.Close();
return table;
}
//通过Id更新点击数
public int UpdateClick(int nId)
{
SqlParameter[] nParas = { new SqlParameter("@Id", SqlDbType.Int, 4) };
nParas[0].Value = nId;
try
{
int rows = SQLHelper.ExecuteNonQuery(SQLHelper.txtConnecttionString, CommandType.StoredProcedure, "UpdateItemClick", nParas);
return rows;
}
catch (SqlException ex) {
throw new Exception(ex.Message, ex);
}
}
//新加一个订单
public int InsertOrder(OrderInfo nOrder)
{
SqlParameter[] paras = { new SqlParameter("@Id", SqlDbType.VarChar, 50),
new SqlParameter("@UserName",SqlDbType.VarChar,30),
new SqlParameter("@ItemName",SqlDbType.NVarChar,50),
new SqlParameter("@Price",SqlDbType.Decimal,5),
new SqlParameter("@Total",SqlDbType.Decimal,5),
};
paras[0].Value = nOrder.OrderId;
paras[1].Value = nOrder.UserName;
paras[2].Value = nOrder.ItemName;
paras[3].Value = nOrder.Price;
paras[4].Value = nOrder.ItemTotal;
try
{
int rows = SQLHelper.ExecuteNonQuery(SQLHelper.txtConnecttionString, CommandType.StoredProcedure, "InsertToOrder", paras);
return rows;
}
catch (SqlException ex)
{
throw new Exception(ex.Message, ex);
}
}
//更新详细资料
public int UpdateDataOrder(OrderInfo nOrder, string nId)
{
IList<OrderInfo> orderInfo = new List<OrderInfo>();
SqlParameter[] paras = { new SqlParameter("@Id", SqlDbType.VarChar,50),
new SqlParameter("@CarryMode", SqlDbType.NVarChar,20),
new SqlParameter("@Adress",SqlDbType.NVarChar,50),
new SqlParameter("@Postalcode",SqlDbType.Int,4),
new SqlParameter("@Phone",SqlDbType.VarChar,30),
new SqlParameter("@Telephone",SqlDbType.BigInt,8),
};
paras[0].Value = nOrder.OrderId;
paras[1].Value = nOrder.CarrayModel;
paras[2].Value = nOrder.UserAdress;
paras[3].Value = nOrder.Postalcode;
paras[4].Value = nOrder.UserPhone;
paras[5].Value = nOrder.TelePhone;
try
{
int rows = SQLHelper.ExecuteNonQuery(SQLHelper.txtConnecttionString, CommandType.StoredProcedure, "UpdateOrderData", paras);
return rows;
}
catch (SqlException ex)
{
throw new Exception(ex.Message, ex);
}
}
//根据Id删除订单
public int DeleteOrder(string nId)
{
SqlParameter[] paras = { new SqlParameter("@Id", SqlDbType.VarChar, 50) };
paras[0].Value = nId;
string sqlText = "DELETE [Order] WHERE Id=@Id";
try
{
int rows = SQLHelper.ExecuteNonQuery(SQLHelper.txtConnecttionString, CommandType.Text, sqlText, paras);
return rows;
}
catch (SqlException ex)
{
throw new Exception(ex.Message, ex);
}
}
//根据用户名及密码验证用户
public bool CheckUser(string nName, string nPass)
{
bool IsBool;
SqlParameter[] paras = { new SqlParameter("@Name", SqlDbType.VarChar, 30), new SqlParameter("@Pass", SqlDbType.VarChar, 30) };
paras[0].Value = nName;
paras[1].Value = nPass;
string sqlText = "SELECT count(*) FROM [User] WHERE Name=@Name AND Pass=@Pass";
try
{
int txtRows = int.Parse(SQLHelper.ExecuteSclare(SQLHelper.txtConnecttionString, CommandType.Text, sqlText, paras).ToString());
if (txtRows > 0)
{
IsBool = true;
}
else
{
IsBool = false;
}
return IsBool;
}
catch (SqlException ex)
{
throw new Exception(ex.Message, ex);
}
}
//获取所有类别
public DataTable GetAllCategory()
{
SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.Con, CommandType.StoredProcedure, "Synchronize_Categorys", null);
DataTable table = new DataTable();
table.Load(reader);
reader.Close();
return table;
}
//根据类别IP获取
public DataTable GetCategory(string categoryId)
{
SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.Con, CommandType.StoredProcedure, "GetCategory", null);
DataTable table = new DataTable();
table.Load(reader);
reader.Close();
return table;
}
//是否存在
private string tableName = "Ljh_Admin";
private ConfigInfo configInfo = new ConfigInfo();
public bool Exist(string filter)
{
bool result = false;
string sql = @"select * from " + tableName;
if (!string.IsNullOrEmpty(filter))
{
sql = sql + " where " + filter;
}
try
{
DataSet dataset = new DataSet();
dataset = Database.ExecuteDataSet(sql);
if (dataset.Tables[0].Rows.Count > 0)
{
result = true;
}
}
catch (Exception ex)
{
throw ex;
}
return result;
}