首页 新闻 会员 周边 捐助

获取实体对象的效率问题。

0
悬赏园豆:10 [已关闭问题]
<P>/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 得到一个对象实体<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;public Model.Product GetModel(int id)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;StringBuilder strSql=new StringBuilder();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strSql.Append("select * from Products ");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strSql.Append(" where ProductId=" + id);<BR>&nbsp;&nbsp;&nbsp;Model.Product model=new Model.Product();<BR>&nbsp;&nbsp;&nbsp;DataSet ds=SQLHelper.PopulateDataSet(strSql.ToString());<BR>&nbsp;&nbsp;&nbsp;model.ProductId=id;<BR>&nbsp;&nbsp;&nbsp;if(ds.Tables[0].Rows.Count&gt;0)<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (ds.Tables[0].Rows[0]["Category1"].ToString() != "")<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; model.Category1 = int.Parse(ds.Tables[0].Rows[0]["Category1"].ToString());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (ds.Tables[0].Rows[0]["Category2"].ToString() != "")<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; model.Category2 = int.Parse(ds.Tables[0].Rows[0]["Category2"].ToString());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (ds.Tables[0].Rows[0]["Category3"].ToString() != "")<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; model.Category3 = int.Parse(ds.Tables[0].Rows[0]["Category3"].ToString());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;model.ProductSN=ds.Tables[0].Rows[0]["ProductSN"].ToString();<BR>&nbsp;&nbsp;&nbsp;&nbsp;model.ProductName=ds.Tables[0].Rows[0]["ProductName"].ToString();<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(ds.Tables[0].Rows[0]["MarketPrice"].ToString()!="")<BR>&nbsp;&nbsp;&
问题补充: if (ds.Tables[0].Rows[0]["Relationship"].ToString() != "") { if ((ds.Tables[0].Rows[0]["Relationship"].ToString() == "1") || (ds.Tables[0].Rows[0]["Relationship"].ToString().ToLower() == "true")) { model.Relationship = true; } else { model.Relationship = false; } } model.RelationshipProducts = ds.Tables[0].Rows[0]["RelationshipProducts"].ToString(); return model; } else { return null; } } 从数据库中获取某款商品的信息赋予实体对象,请问有没有更高效的写法? -------------------------------------------- IDataReader dr IList<Model.FileInfo> IFile.GetFiles IList<Model.FileInfo> fileInfos = new List<Model.FileInfo>(); 效率不会更低吗? 系统特定于SQLServer数据库。不考虑移植,有比SQLDateReader dr 更性能一点写法吗?
红尘中迷茫的主页 红尘中迷茫 | 初学一级 | 园豆:60
提问于:2007-12-08 15:40
< >
分享
其他回答(7)
0
用DataReader会更好些
Anders Cui | 园豆:1570 (小虾三级) | 2007-12-08 15:50
0
DataReader 加 List<>
天际翔龙 | 园豆:215 (菜鸟二级) | 2007-12-08 16:15
0
楼上说的会好很多. DataReader 加 List<> 如果要取大量数据的话才用DataSet.
"鸟巢" | 园豆:175 (初学一级) | 2007-12-08 18:27
0
DataReader怎么加List<>?........
(心)Shing Ye | 园豆:125 (初学一级) | 2007-12-08 18:35
0
IDataReader dr = XXX\ while(dr.Read()) { Model m = new Model(); m.Name = dr["ColumnsName"]; } dr.Close();
Lowkey | 园豆:220 (菜鸟二级) | 2007-12-09 01:28
0
IList<Model.FileInfo> IFile.GetFiles(int blogId) { string SQL_THIS = SQL_SELECT_FILE_BY_BLOGID; SqlParameter[] paras = new SqlParameter[]{ new SqlParameter(PARAM_BLOG_ID,SqlDbType.Int)}; paras[0].Value = blogId; IList<Model.FileInfo> fileInfos = new List<Model.FileInfo>(); using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.ConnectionString, CommandType.Text, SQL_THIS, paras)) { while(rdr.Read()) { FileInfo fileInfo = new FileInfo(rdr.GetInt32(0), rdr.GetInt32(1), rdr.GetString(2), rdr.GetDouble(3), rdr.GetString(4), rdr.GetString(5), rdr.GetDateTime(6), rdr.GetInt32(7), rdr.GetInt32(8)); fileInfos.Add(fileInfo); } } return fileInfos; //throw new Exception("The method or operation is not implemented."); }
volnet(可以叫我大V) | 园豆:720 (小虾三级) | 2007-12-09 02:16
0
datareader是最效率的数据库取值方法,如果返回数据集比较大的话,一定要做好分页 datatoentity这里没必要考虑效率,这个损耗太小了,你可以用nbearmapping
willsun | 园豆:225 (菜鸟二级) | 2007-12-30 23:42
0
比较同意 DataReader 加 List<> 效率应该好不少
我是七哥 | 园豆:212 (菜鸟二级) | 2008-05-04 10:03
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册