using System;
using System.Data;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using DSWeb.MvcShipping.Models.CodeCtnSet;
using Microsoft.Practices.EnterpriseLibrary.Data;
namespace DSWeb.MvcShipping.DAL.MsCodeCtnSet
{
public class MsCodeCtnSetDAL
{
static Database db = DatabaseFactory.CreateDatabase();
///
/// 分页获取数据列表
///
static public List GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("SELECT * FROM ( ");
strSql.Append(" SELECT ROW_NUMBER() OVER (");
if (!string.IsNullOrEmpty(orderby.Trim()))
{
strSql.Append("order by T." + orderby);
}
else
{
strSql.Append("order by T.CTNID desc");
}
strSql.Append(")AS Row, T.* from code_ctn T ");
if (!string.IsNullOrEmpty(strWhere.Trim()))
{
strSql.Append(" WHERE " + strWhere);
}
strSql.Append(" ) TT");
strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
return SetData(strSql);
}
///
/// 根据id获取实例
///
///
///
static public CodeCtnSet GetData(string id)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select top 1 CTNID,CTNSIZE,CTNTYPE,CTN,EDICODE,CTNWEIGHT,EEXPLAIN,CEXPLAIN,AFRCODE,TEU,DLIKGS from code_ctn ");
strSql.Append(" where CTNID=@CTNID ");
SqlParameter[] parameters = {new SqlParameter("@CTNID", SqlDbType.VarChar,36)};
parameters[0].Value = id;
return SetData(strSql).FirstOrDefault();
}
///
/// 获取记录总数
///
static public int GetRecordCount(string strWhere)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select count(1) FROM code_ctn ");
if (strWhere.Trim() != "")
{
strSql.Append(" where " + strWhere);
}
return Convert.ToInt32(db.ExecuteScalar(CommandType.Text, strSql.ToString()));
}
///
/// 执行sql
///
///
///
private static List SetData(StringBuilder strSql)
{
var headList = new List();
using (IDataReader row = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (row.Read())
{
CodeCtnSet model = new CodeCtnSet();
#region Set DB data to Object
if (row["CTNID"] != null)
{
model.CTNID = row["CTNID"].ToString();
}
if (row["CTNSIZE"] != null)
{
model.CTNSIZE = row["CTNSIZE"].ToString();
}
if (row["CTNTYPE"] != null)
{
model.CTNTYPE = row["CTNTYPE"].ToString();
}
if (row["CTN"] != null)
{
model.CTN = row["CTN"].ToString();
}
if (row["EDICODE"] != null)
{
model.EDICODE = row["EDICODE"].ToString();
}
if (row["CTNWEIGHT"] != null && row["CTNWEIGHT"].ToString() != "")
{
model.CTNWEIGHT = decimal.Parse(row["CTNWEIGHT"].ToString());
}
if (row["EEXPLAIN"] != null)
{
model.EEXPLAIN = row["EEXPLAIN"].ToString();
}
if (row["CEXPLAIN"] != null)
{
model.CEXPLAIN = row["CEXPLAIN"].ToString();
}
if (row["AFRCODE"] != null)
{
model.AFRCODE = row["AFRCODE"].ToString();
}
if (row["TEU"] != null && row["TEU"].ToString() != "")
{
model.TEU = decimal.Parse(row["TEU"].ToString());
}
if (row["DLIKGS"] != null && row["DLIKGS"].ToString() != "")
{
model.DLIKGS = decimal.Parse(row["DLIKGS"].ToString());
}
#endregion
headList.Add(model);
}
row.Close();
}
return headList;
}
///
/// 增加一条数据
///
static public int Add(CodeCtnSet model)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into code_ctn(");
strSql.Append("CTNID,CTNSIZE,CTNTYPE,CTN,EDICODE,CTNWEIGHT,EEXPLAIN,CEXPLAIN,AFRCODE,TEU,DLIKGS)");
strSql.Append(" values (");
strSql.Append("@CTNID,@CTNSIZE,@CTNTYPE,@CTN,@EDICODE,@CTNWEIGHT,@EEXPLAIN,@CEXPLAIN,@AFRCODE,@TEU,@DLIKGS)");
SqlParameter[] parameters = {
new SqlParameter("@CTNID", SqlDbType.VarChar,36),
new SqlParameter("@CTNSIZE", SqlDbType.VarChar,5),
new SqlParameter("@CTNTYPE", SqlDbType.VarChar,10),
new SqlParameter("@CTN", SqlDbType.VarChar,10),
new SqlParameter("@EDICODE", SqlDbType.VarChar,5),
new SqlParameter("@CTNWEIGHT", SqlDbType.Decimal,9),
new SqlParameter("@EEXPLAIN", SqlDbType.VarChar,100),
new SqlParameter("@CEXPLAIN", SqlDbType.VarChar,100),
new SqlParameter("@AFRCODE", SqlDbType.VarChar,10),
new SqlParameter("@TEU", SqlDbType.Decimal,5),
new SqlParameter("@DLIKGS", SqlDbType.Decimal,9)
};
parameters[0].Value = model.CTNID;
parameters[1].Value = model.CTNSIZE;
parameters[2].Value = model.CTNTYPE;
parameters[3].Value = model.CTN;
parameters[4].Value = model.EDICODE;
parameters[5].Value = model.CTNWEIGHT;
parameters[6].Value = model.EEXPLAIN;
parameters[7].Value = model.CEXPLAIN;
parameters[8].Value = model.AFRCODE;
parameters[9].Value = model.TEU;
parameters[10].Value = model.DLIKGS;
SqlCommand com = new SqlCommand();
com.CommandText = strSql.ToString();
com.Parameters.AddRange(parameters);
return db.ExecuteNonQuery(com);
}
///
/// 更新一条数据
///
static public int Update(CodeCtnSet model)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("update code_ctn set ");
strSql.Append("CTNSIZE=@CTNSIZE,");
strSql.Append("CTNTYPE=@CTNTYPE,");
strSql.Append("CTN=@CTN,");
strSql.Append("EDICODE=@EDICODE,");
strSql.Append("CTNWEIGHT=@CTNWEIGHT,");
strSql.Append("EEXPLAIN=@EEXPLAIN,");
strSql.Append("CEXPLAIN=@CEXPLAIN,");
strSql.Append("AFRCODE=@AFRCODE,");
strSql.Append("TEU=@TEU,");
strSql.Append("DLIKGS=@DLIKGS");
strSql.Append(" where CTNID=@CTNID ");
SqlParameter[] parameters = {
new SqlParameter("@CTNSIZE", SqlDbType.VarChar,5),
new SqlParameter("@CTNTYPE", SqlDbType.VarChar,10),
new SqlParameter("@CTN", SqlDbType.VarChar,10),
new SqlParameter("@EDICODE", SqlDbType.VarChar,5),
new SqlParameter("@CTNWEIGHT", SqlDbType.Decimal,9),
new SqlParameter("@EEXPLAIN", SqlDbType.VarChar,100),
new SqlParameter("@CEXPLAIN", SqlDbType.VarChar,100),
new SqlParameter("@AFRCODE", SqlDbType.VarChar,10),
new SqlParameter("@TEU", SqlDbType.Decimal,5),
new SqlParameter("@DLIKGS", SqlDbType.Decimal,9),
new SqlParameter("@CTNID", SqlDbType.VarChar,36)};
parameters[0].Value = model.CTNSIZE;
parameters[1].Value = model.CTNTYPE;
parameters[2].Value = model.CTN;
parameters[3].Value = model.EDICODE;
parameters[4].Value = model.CTNWEIGHT;
parameters[5].Value = model.EEXPLAIN;
parameters[6].Value = model.CEXPLAIN;
parameters[7].Value = model.AFRCODE;
parameters[8].Value = model.TEU;
parameters[9].Value = model.DLIKGS;
parameters[10].Value = model.CTNID;
SqlCommand com = new SqlCommand();
com.CommandText = strSql.ToString();
com.Parameters.AddRange(parameters);
return db.ExecuteNonQuery(com);
}
///
/// 删除一条数据
///
static public bool Delete(string ctnid)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("delete from code_ctn ");
strSql.Append(" where CTNID=@CTNID ");
SqlParameter[] parameters = {new SqlParameter("@CTNID", SqlDbType.VarChar,36)};
parameters[0].Value = ctnid;
int rows = db.ExecuteNonQuery(strSql.ToString(), parameters);
return rows > 0;
}
}
}