using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
namespace BLL.DAL
{
/// <summary>
/// 数据访问类:AreaDAL
/// </summary>
public class AreaDAL
{
public AreaDAL()
{}
#region Method
/// <summary>
/// 是否存在该记录
/// </summary>
public bool Exists(decimal id)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("select count(1) from Area");
strSql.Append(" where id=@id ");
SqlParameter[] parameters = {
new SqlParameter("@id", SqlDbType.Float)};
parameters[0].Value = id;
return DbHelperSQL.Exists(strSql.ToString(),parameters);
}
/// <summary>
/// 增加一条数据
/// </summary>
public void Add(BLL.Model.AreaModel model)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("insert into Area(");
strSql.Append("id,areaname,sortid,parentid,datapath)");
strSql.Append(" values (");
strSql.Append("@id,@areaname,@sortid,@parentid,@datapath)");
SqlParameter[] parameters = {
new SqlParameter("@id", SqlDbType.Float,8),
new SqlParameter("@areaname", SqlDbType.NVarChar,255),
new SqlParameter("@sortid", SqlDbType.Float,8),
new SqlParameter("@parentid", SqlDbType.Float,8),
new SqlParameter("@datapath", SqlDbType.NVarChar,255)};
parameters[0].Value = model.id;
parameters[1].Value = model.areaname;
parameters[2].Value = model.sortid;
parameters[3].Value = model.parentid;
parameters[4].Value = model.datapath;
DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
}
/// <summary>
/// 更新一条数据
/// </summary>
public bool Update(BLL.Model.AreaModel model)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("update Area set ");
strSql.Append("areaname=@areaname,");
strSql.Append("sortid=@sortid,");
strSql.Append("parentid=@parentid,");
strSql.Append("datapath=@datapath");
strSql.Append(" where id=@id ");
SqlParameter[] parameters = {
new SqlParameter("@id", SqlDbType.Float,8),
new SqlParameter("@areaname", SqlDbType.NVarChar,255),
new SqlParameter("@sortid", SqlDbType.Float,8),
new SqlParameter("@parentid", SqlDbType.Float,8),
new SqlParameter("@datapath", SqlDbType.NVarChar,255)};
parameters[0].Value = model.id;
parameters[1].Value = model.areaname;
parameters[2].Value = model.sortid;
parameters[3].Value = model.parentid;
parameters[4].Value = model.datapath;
int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
if (rows > 0)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 删除一条数据
/// </summary>
public bool Delete(decimal id)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("delete from Area ");
strSql.Append(" where id=@id ");
SqlParameter[] parameters = {
new SqlParameter("@id", SqlDbType.Float)};
parameters[0].Value = id;
int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
if (rows > 0)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 删除一条数据
/// </summary>
public bool DeleteList(string idlist )
{
StringBuilder strSql=new StringBuilder();
strSql.Append("delete from Area ");
strSql.Append(" where id in ("+idlist + ") ");
int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
if (rows > 0)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 得到一个对象实体
/// </summary>
public BLL.Model.AreaModel GetModel(decimal id)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("select top 1 id,areaname,sortid,parentid,datapath from Area ");
strSql.Append(" where id=@id ");
SqlParameter[] parameters = {
new SqlParameter("@id", SqlDbType.Float)};
parameters[0].Value = id;
BLL.Model.AreaModel model = new BLL.Model.AreaModel();
DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
if(ds.Tables[0].Rows.Count>0)
{
if(ds.Tables[0].Rows[0]["id"].ToString()!="")
{
model.id=decimal.Parse(ds.Tables[0].Rows[0]["id"].ToString());
}
model.areaname=ds.Tables[0].Rows[0]["areaname"].ToString();
if(ds.Tables[0].Rows[0]["sortid"].ToString()!="")
{
model.sortid=decimal.Parse(ds.Tables[0].Rows[0]["sortid"].ToString());
}
if(ds.Tables[0].Rows[0]["parentid"].ToString()!="")
{
model.parentid=decimal.Parse(ds.Tables[0].Rows[0]["parentid"].ToString());
}
model.datapath=ds.Tables[0].Rows[0]["datapath"].ToString();
return model;
}
else
{
return null;
}
}
/// <summary>
/// 获得数据列表
/// </summary>
public DataSet GetList(string strWhere, string order)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select * ");
strSql.Append(" FROM area ");
if (strWhere.Trim() != "")
{
strSql.Append(" where " + strWhere);
}
strSql.Append(" order by " + order + "");
return DbHelperSQL.Query(strSql.ToString());
}
/// <summary>
/// 获得数据列表
/// </summary>
public DataSet GetList(string strWhere)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("select * ");
strSql.Append(" FROM Area ");
if(strWhere.Trim()!="")
{
strSql.Append(" where "+strWhere);
}
return DbHelperSQL.Query(strSql.ToString());
}
/// <summary>
/// 获得前几行数据
/// </summary>
public DataSet GetList(int Top,string strWhere,string filedOrder)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("select ");
if(Top>0)
{
strSql.Append(" top "+Top.ToString());
}
strSql.Append(" id,areaname,sortid,parentid,datapath ");
strSql.Append(" FROM Area ");
if(strWhere.Trim()!="")
{
strSql.Append(" where "+strWhere);
}
strSql.Append(" order by " + filedOrder);
return DbHelperSQL.Query(strSql.ToString());
}
/// <summary>
/// 获取当前城市下包含县的个数
/// </summary>
/// <param name="cityid"></param>
/// <returns></returns>
public int GetCountXian(int cityid)
{
string strSql = "select count(1) from area where parentid='" + cityid + "'";
return int.Parse(DbHelperSQL.GetSingle(strSql).ToString());
}
public static string GetAreaName(int id)
{
string strSql = "select areaname from area where id='"+id+"'";
object areaname = DbHelperSQL.GetSingle(strSql);
if (areaname != null)
{
return areaname.ToString();
}
else
{
return "";
}
}
public static string GetUrl(int id)
{
string strSql = "select url from area where id='" + id + "'";
object areaname = DbHelperSQL.GetSingle(strSql);
if (areaname != null)
{
return areaname.ToString();
}
else
{
return "";
}
}
#endregion Method
}
}