这个是抽象类Entity的的方法:泛型E 约束为 Entity<E>
/// <summary>
/// 给实体类搜索数据库用
/// </summary>
/// <param name="columnName">要搜索列名</param>
/// <param name="columnValue">要搜索列值</param>
/// <returns></returns>
public static List<E> Select(String columnName, object columnValue)
{
//查询到数据的列表
List<E> rete = new List<E>(); Type type = typeof(E); //获取对象的包含有column的属性,既对应的数据库列名
List<PropertyInfo> pi = new List<PropertyInfo>(
type.GetProperties().Where<PropertyInfo>
(
P => P.GetCustomAttributes(typeof(ColumnAttribute), true).Length > 0
)
); //将列名累加为字符串
StringBuilder sql = new StringBuilder(); foreach (var p in pi)
{
sql.AppendFormat("{0},", p.Name.ToString());
} //操作数据库
using(System.Data.SqlClient.SqlConnection connection = ConnectionHelper.CreateConnection())
{
System.Data.SqlClient.SqlCommand command = connection.CreateCommand();
connection.Open();
command.CommandText = String.Format("SELECT {0} FROM {1} WHERE {2}=@columnValue",
sql.Remove(sql.Length-1,1),
(type.GetCustomAttributes(typeof(TabelAttribute), true)[0] as TabelAttribute).TableName,
columnName); command.Parameters.AddWithValue("@columnValue", columnValue);
using(System.Data.SqlClient.SqlDataReader reader = command.ExecuteReader())
{
int i = 0;
E ne = new E();
while (reader.Read())
{
//要把reader去读到的值赋值到实体类里去,该怎么做
}
rete.Add(ne);
}
return rete;
}
}
User是这个类的一个继承,使用的方法是:User.Select("ID", 2);//意思是要检查所有ID列值是2的记录,返回值是所有检索到的记录生成的user实例的列表
/// <summary>
/// 给实体类搜索数据库用
/// </summary>
/// <param name="columnName">要搜索列名</param>
/// <param name="columnValue">要搜索列值</param>
/// <returns></returns>
public static List<E> Select(String columnName, object columnValue)
{
//查询到数据的列表
List<E> rete = new List<E>(); Type type = typeof(E); //获取对象的包含有column的属性,既对应的数据库列名
List<PropertyInfo> pi = new List<PropertyInfo>(
type.GetProperties().Where<PropertyInfo>
(
P => P.GetCustomAttributes(typeof(ColumnAttribute), true).Length > 0
)
); //将列名累加为字符串
StringBuilder sql = new StringBuilder(); foreach (var p in pi)
{
sql.AppendFormat("{0},", p.Name.ToString());
} //操作数据库
using(System.Data.SqlClient.SqlConnection connection = ConnectionHelper.CreateConnection())
{
System.Data.SqlClient.SqlCommand command = connection.CreateCommand();
connection.Open();
command.CommandText = String.Format("SELECT {0} FROM {1} WHERE {2}=@columnValue",
sql.Remove(sql.Length-1,1),
(type.GetCustomAttributes(typeof(TabelAttribute), true)[0] as TabelAttribute).TableName,
columnName); command.Parameters.AddWithValue("@columnValue", columnValue);
using(System.Data.SqlClient.SqlDataReader reader = command.ExecuteReader())
{
int i = 0;
E ne = new E();
while (reader.Read())
{
//要把reader去读到的值赋值到实体类里去,该怎么做
}
rete.Add(ne);
}
return rete;
}
}
User是这个类的一个继承,使用的方法是:User.Select("ID", 2);//意思是要检查所有ID列值是2的记录,返回值是所有检索到的记录生成的user实例的列表
解决方案 »
- 求reportviewer和winform本体传值的问题。
- datagridview单元格赋值问题
- 如何让ZedGraph 的X轴坐标显示时间?
- 高分求助~System.Transactions.IsolationLevel枚举问题
- 帮忙看看BUG
- 如何在Form2中调用Form1菜单项的Enabled属性?(求救!!!)
- Bad Request (Invalid Hostname)
- C#启动服务的问题
- 暑假在家无聊,谁能给个建议编个什么程序玩玩。。
- C#调用C++写好的dll,dll导出的函数的参数是函数地址
- 如何判断一个数组中是否存在负数,是否存在0,是否存在正数
- c#实现PostScript文件解析标引
this.Passwrod["Password"];
现在用来泛型Entity<E>后就没有办法E.ID=reaerd["ID"];
E.Passwrod["Password"];
╮(╯▽╰)╭小弟学艺不精,各位高手救下,拜谢
E ne = new E();
while (reader.Read())
{
ne = (E)rete[i];
en.ID = reader["ID"].ToString();
en.Passwrod = reader["Passwrod"].ToString();
i++;
}
foreach (var p in pi)
{
string colname = p.GetCustomAttributes(typeof(ColumnAttribute), true);
if (colname.Length > 0)
{
p.SetValue(ne, reader[colname], null);
}
}
while (reader.Read())
{
E ne = new E();
pi = ne.GetType().GetProperties();
foreach (var p in pi)
{
string colname = p.GetCustomAttributes(typeof(ColumnAttribute), true);
p.SetValue(ne, reader[colname], null);
}
rete.Add(ne);
}
没调试,,你看看行不。。
谢谢你帮忙,不过好像不行啊,E是泛型 不能直接点出实例类的属性的啊,password,ID这些是E不拥有的属性
while (reader.Read())
{
E ne = new E();
foreach (var p in pi)
{
p.SetValue(ne, reader[p.Name], null);
}
rete.Add(ne);
}
使用时候这样写: List<User> us = User.Select("Password", "123");