DataReader方式 获取数据
///
/// 得到一个对象实体 DataReader方式
///
///
///
public DotNet.Model.Base_Department GetModelUseDR(long Id)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("Select Top 1 Id,DepartmentName,NetCount,Remarks,CreatedOn,IsPublic,IsLock,SavedOn,LockVersion,Operator_Id From Base_Department ");
strSql.Append(" Where Id=@Id ");
SqlParameter[] parameters = {
new SqlParameter("@Id", SqlDbType.BigInt,8)};
parameters[0].Value = Id;
DotNet.Model.Base_Department model=new DotNet.Model.Base_Department();
SqlDataReader reader = DbHelperSQL.ExecuteReader(strSql.ToString(),parameters);
if(reader.Read())
{
model.Id = reader.GetInt64(reader.GetOrdinal("Id"));
model.DepartmentName = reader.GetString(reader.GetOrdinal("DepartmentName"));
model.NetCount = reader.GetInt32(reader.GetOrdinal("NetCount"));
model.Remarks = reader.GetString(reader.GetOrdinal("Remarks"));
model.CreatedOn = reader.GetDateTime(reader.GetOrdinal("CreatedOn"));
model.IsPublic = reader.GetBoolean(reader.GetOrdinal("IsPublic"));
model.IsLock = reader.GetBoolean(reader.GetOrdinal("IsLock"));
model.SavedOn = reader.GetDateTime(reader.GetOrdinal("SavedOn"));
model.LockVersion = reader.GetInt32(reader.GetOrdinal("LockVersion"));
model.Operator_Id = reader.GetInt64(reader.GetOrdinal("Operator_Id"));
reader.Close();
return model;
}
else
{
reader.Close();
return null;
}
}
/// <summary>
/// 获得数据列表List<>,DataReader 使用参数的
/// </summary>
/// <param name="strWhere">条件</param>
/// <param name="ht">HashTable,保存参数名及参数对应的值</param>
/// <returns>List<DotNet.Model.Base_Department></returns>
/// 使用方法:string str = "id=@id";Hashtable ht = new Hashtable();ht.Add("@id", 10);
public List<DotNet.Model.Base_Department> GetListUseDataReader(string strWhere, Hashtable ht=null)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("Select Id,DepartmentName,NetCount,Remarks,CreatedOn,IsPublic,IsLock,SavedOn,LockVersion,Operator_Id from Base_Department");
if(strWhere.Trim().Length!=0)
{
strSql.Append(" Where " + strWhere.Trim());
}
//获取DataReader
SqlDataReader reader;
//组织参数
if (ht != null && ht.Count > 0)
{
SqlParameter[] parameters = new SqlParameter[ht.Count];
int i = 0;
foreach (DictionaryEntry dc in ht)
{
parameters[i] = new SqlParameter() ;
parameters[i].ParameterName = dc.Key.ToString();
parameters[i].Value = dc.Value;
i++;
}
reader = DbHelperSQL.ExecuteReader(strSql.ToString(), parameters);
}
else
reader = DbHelperSQL.ExecuteReader(strSql.ToString());
if (reader.HasRows)
{
List<DotNet.Model.Base_Department> list = new List<DotNet.Model.Base_Department>();
while (reader.Read())
{
DotNet.Model.Base_Department model = new DotNet.Model.Base_Department();
model.Id = reader.GetInt64(reader.GetOrdinal("Id"));
model.DepartmentName = reader.GetString(reader.GetOrdinal("DepartmentName"));
model.NetCount = reader.GetInt32(reader.GetOrdinal("NetCount"));
model.Remarks = reader.GetString(reader.GetOrdinal("Remarks"));
model.CreatedOn = reader.GetDateTime(reader.GetOrdinal("CreatedOn"));
model.IsPublic = reader.GetBoolean(reader.GetOrdinal("IsPublic"));
model.IsLock = reader.GetBoolean(reader.GetOrdinal("IsLock"));
model.SavedOn = reader.GetDateTime(reader.GetOrdinal("SavedOn"));
model.LockVersion = reader.GetInt32(reader.GetOrdinal("LockVersion"));
model.Operator_Id = reader.GetInt64(reader.GetOrdinal("Operator_Id"));
list.Add(model);
}
reader.Close();
return list;
}
else
{
reader.Close();
return null;
}
}