首页 新闻 赞助 找找看

泛型方法操作,根据传递参数查询数据,赋值到T返回对象

0
悬赏园豆:10 [已解决问题] 解决于 2012-04-27 10:03
public class Student
{
    public int ID{get;set;}
    public string Name{get;set;}
}
public T GetModule(T obj,string where)
{
    //怎么通过传递的类型赋值
             //要求:1.根据传递的SQL语句查询一条记录
             //2.把查询的数据赋值到T obj中
             //3.最后返回已赋值的obj;
}
//调用
public void Main()
{
   Student st=GetModule<Student>(new Student(),"select * from Student where ID=1");
   
}
小明冥的主页 小明冥 | 菜鸟二级 | 园豆:222
提问于:2012-04-26 17:17
< >
分享
最佳答案
0

public class Student
{
private string id;

public string Id
{
get { return id; }
set { id = value; }
}
}
public class StuBLL<T>
{

public T GetStuInfo(T obj)
{
Student stu = obj as Student;
StringBuilder sql = new StringBuilder("SELECT * FROM stuInfo WHERE Id=" + stu.Id + " ");
SqlConnection con = new SqlConnection("连接字符串");
SqlCommand com = new SqlCommand(sql.ToString(), con);
SqlDataReader read = com.ExecuteReader();
while (read.Read())
{
stu.Id = read["Id"].ToString();
}
return obj;
}
}

 

 

刚刚做的   不符合在说

收获园豆:10
┢┦偉 | 小虾三级 |园豆:1240 | 2012-04-26 17:45

不行啊!~

Student stu = obj as Student;//这个对象不能在此方法中直接知道

T 这个类型是未知的.

如果定死为Student那么如果有来一个UserInfo类型呢?!~

      

小明冥 | 园豆:222 (菜鸟二级) | 2012-04-26 18:05


public class StuDAL<T>
{
DataSet ds = null;
/// <summary>
/// 获取查询出来的Table
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public IList<T> GetTable(string sql)
{
SqlConnection con = new SqlConnection("");
ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(sql, con);
adapter.Fill(ds);
return GetTableInfo(ds.Tables[0]);
}
/// <summary>
/// 把数据库中表的列和实体类中的列一一匹配
/// </summary>
/// <param name="table">数据表</param>
/// <returns>返回一个</returns>

public IList<T> GetTableInfo(DataTable table)
{
if (table == null)
return null;
if (table.Rows.Count < 1)
return null;
IList<T> list = new List<T>();
T t = Activator.CreateInstance<T>();
//获取当前T对象的所有属性
PropertyInfo[] propertys = t.GetType().GetProperties();
for (int i = 0; i < table.Rows.Count; i++)
{
//通过属性集合迭代出每一个属性对象
foreach (PropertyInfo item in propertys)
{
//循环迭代出DataTable的每一列
for (int j = 0; j < table.Columns.Count; j++)
{
//判断当前迭代出的属性名称是否和迭代出的DataTable的列名称一致
if (item.Name.ToLower().Equals(table.Columns[j].ColumnName.ToLower()))
{
//判断当前DataTable的单列值是否为null
if (table.Rows[i][j] != DBNull.Value)
//将当前DataTable的单列值赋予相匹配的属性,否则赋予一个null值.
item.SetValue(t, (table.Rows[i][j]), null);
else
item.SetValue(t, null, null);
break;
}
}
}
list.Add(t);
}
return list;
}
}
public class UI
{
public void s()
{

//最后返回的是一个集合,按你的想法,返回对象有点难度吧..
IList<Student> stuList = new StuDAL<Student>().GetTable("Sql语句");
//读取数据
foreach (var item in stuList)
{

}
}
}

┢┦偉 | 园豆:1240 (小虾三级) | 2012-04-26 19:16

@┢┦偉: 返回对象没有什么难度啊!~

      照你这么写很简单啊!~

 

UsersEntity un = new StuDAL<UsersEntity>().GetModule("select * from Users where ID=1");

public class StuDAL<T>
{
    public T GetModule(string sql)
    {
        T t=default(T);
        using (SqlConnection con = new SqlConnection("。。。。"))
        {
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                t=GetModuleInfo(dt);
            }
            else {
                throw new NotImplementedException();
            }            
        }
        return t;
    }

    private T GetModuleInfo(DataTable dt)
    {
        T t = default(T);
        T tobj=Activator.CreateInstance<T>();
        PropertyInfo[] pr = tobj.GetType().GetProperties();
        foreach (System.Reflection.PropertyInfo item in pr)
        {
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                if (item.Name.ToLower().Equals(dt.Columns[i].ColumnName.ToLower()))
                {
                    if (dt.Rows[0][i] != DBNull.Value)
                        item.SetValue(tobj, (dt.Rows[0][i]), null);
                    else
                        item.SetValue(tobj, null, null);
                }
            }
        }
        t = tobj;
        return t;
    }

}
小明冥 | 园豆:222 (菜鸟二级) | 2012-04-27 10:02

@小明冥: 好吧。我承认没好好想 ,我的错

┢┦偉 | 园豆:1240 (小虾三级) | 2012-04-27 10:12
其他回答(1)
0
public T GetModule<T>(T obj,string where)
{
    //怎么通过传递的类型赋值
             //要求:1.根据传递的SQL语句查询一条记录
             //2.把查询的数据赋值到T obj中
             //3.最后返回已赋值的obj;
}
无之无 | 园豆:5095 (大侠五级) | 2012-04-26 21:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册