有类似的DataAccessor?泛型方法

minheen 2009-01-15 04:27:33
以下是我自己写的,感觉速度不快,网上也没找到现成的。。。感觉太幼稚了,不成熟。。。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Reflection.Emit;
using System.Reflection;
using System.Xml;

namespace blog.model
{
public class BaseEntity
{
public IList<T> Query<T>(int page, int pageSize, string filter, string sorter) where T : BaseEntity
{
if (page < 1 || (pageSize < 1) && pageSize != -1)
{
return null;
}

string tp = Activator.CreateInstance<T>().GetType().ToString();

string table = GetTableName(tp);

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
{
#region 拼接SQL语句

string sql = "";

if (pageSize != -1)
{
if (page == 1)
{
sql = "select top " + pageSize + " * from " + table + " where 1=1 ";
}
else
{
sql = "select top " + pageSize + " * from " + table + " where id not in "
+ "(select top " + (page - 1) * pageSize + " id from " + table;

//查询条件
if (!string.IsNullOrEmpty(filter))
{
sql += " where " + filter;
}

//排序条件
if (!string.IsNullOrEmpty(sorter))
{
sql += " " + sorter + " ";
}

sql += " ) ";
}
}
else
{
sql = "select * from " + table + " where 1=1 ";
}

//查询条件
if (!string.IsNullOrEmpty(filter))
{
sql += " and " + filter;
}

//排序条件
if (!string.IsNullOrEmpty(sorter))
{
sql += " " + sorter + " ";
}

#endregion

#region 读取数据

try
{
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();

IList<T> list = new List<T>();

SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
T entity = Activator.CreateInstance<T>();

//得到实体对应表的结构
DataTable structTable = GetTableStructure(table);

//获得实体的所有属性集合
PropertyInfo[] properties = entity.GetType().GetProperties();

for (int i = 0; i < structTable.Rows.Count; i++)
{
try
{
//给属性赋值(实体属性必须严格跟数据库中字段对应)
//相当于user.id=(int)reader["id"];
properties[i].SetValue(entity, reader[structTable.Rows[i][1].ToString()], null);
}
catch
{ continue; }
}

list.Add(entity);
}
reader.Close();

return list;
}
catch (Exception ex)
{ return null; }

#endregion
}
}
}
}


使用时就可以直接调用这个泛型方法,返回实体列表

BaseEntity dao = new BaseEntity();

IList<gallerys> list = dao.Query<gallerys>(1, 5, "userid=1", "");

GridView1.DataSource = list;
GridView1.DataBind();

IList<Votes> list2 = dao.Query<Votes>(1, 5, "", "");
IList<VoteRecords> list3 = dao.Query<VoteRecords>(1, 5, "", "");

GridView2.DataSource = list2;
GridView2.DataBind();
GridView3.DataSource = list3;
GridView3.DataBind();
...全文
124 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
a38809972 2009-01-22
  • 打赏
  • 举报
回复
你好,如果要分页的话,最好用存储过程,希望我们交流一下!
ZJ159 2009-01-15
  • 打赏
  • 举报
回复
难啊
minheen 2009-01-15
  • 打赏
  • 举报
回复
自己顶起

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧