数据库查询结果集怎么不能转换

叫什么呢 2012-05-31 10:14:24
定义的集合
public class getinform
{
private string _name;
private string _pic;
private double _price;
public string name
{
get { return _name; }
set { _name = value; }
}
public string pic
{
get { return _pic; }
set { _pic = value; }
}
public double price
{
get { return _price; }
set { _price = value; }
}
}

数据库查询代码
public class selc
{
private string _name;
private string _pic;
private double _price;
public string name
{
get { return _name; }
set { _name = value; }
}
public string pic
{
get { return _pic; }
set { _pic = value; }
}
public double price
{
get { return _price; }
set { _price = value; }
}
}
public List<selc> select(string ID)
{
List<selc> results = new List<selc>();
SqlConnection con = new SqlConnection(_comstring);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select 产品名称,单价,小图片 from goods where 产品ID=@ID ";
cmd.Parameters.AddWithValue("ID", ID);
using (con)
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
selc get = new selc();
get.name = (string)reader["产品名称"];
get.pic = Convert.ToString(reader["小图片"]);
get.price = (double)reader["单价"];
results.Add(get);
}
reader.Close();
}
return results;
}

但是我在前面不能赋值呢
string ID = row1["产品ID"].ToString();
object1 sel = new object1();
getinform getinform =(getinform)sel.select(ID);
...全文
138 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
熙风 2012-06-01
  • 打赏
  • 举报
回复

List<selc> selcList =sel.select(ID);
List<getinform > getList=new List<getinform >();
foreach(selc item in selcList )
{
getinform get=new getinform ();
get._name=item._name;
get._pic=item._pic;
get._price=item._price;
getList.add(get);
}
ParanoidKing 2012-06-01
  • 打赏
  • 举报
回复
selc可以继承自getinform,前面重复的就可以不要了
object1是什么东西?应该是selc吧?
selc sel = new selc();
List<selc> getinform =sel.select(ID);
另外还有
cmd.Parameters.AddWithValue("@ID", ID);
叫什么呢 2012-06-01
  • 打赏
  • 举报
回复
那因该怎么转换呢
叫什么呢 2012-06-01
  • 打赏
  • 举报
回复
感谢各位大神们,问题已经解决了
叫什么呢 2012-06-01
  • 打赏
  • 举报
回复
感谢各位大神们,问题已经解决了
SocketUp 2012-05-31
  • 打赏
  • 举报
回复
sel.select方法返回的是List<selc>,你直接强制转成getinform? 哪位老师教的?
人生无悔 2012-05-31
  • 打赏
  • 举报
回复

--你结果中都没把它选出来
cmd.CommandText = "select 产品ID,产品名称,单价,小图片 from goods where 产品ID=@ID ";

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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