本帖最后由 贰的23次方 于 2014-3-19 12:03 编辑
- <p> </p><p>public class InfoCategory
- {
- public Guid ID { set; get; }
- public string InfoName { set; get; }
- }</p><p>//数据库中插入了几条记录,然后用方法读取记录时出现异常“对象为引用到实例”</p><p>//出错代码如下,粗体</p><div class="blockcode"><blockquote> public InfoCategory[] GetInfo(string category)
- {
- string sql="select ID,InfoName from T_InfoCategory where InfoCategory=@InfoCategory";
- DataTable table = SqlHelper.ExecuteDataTable(sql, new SqlParameter("@InfoCateGory",category));
- InfoCategory[] infoCategorys = new InfoCategory[table.Rows.Count];
- for(int i=0;i<table.Rows.Count;++i)
- {
- DataRow row=table.Rows[i];
-
- <strong>infoCategorys[i].ID = (Guid)row["ID"];
- infoCategorys[i].InfoName = (string)row["InfoName"];</strong>
-
- }
- return infoCategorys;
- }
复制代码 //后更正如下,可以运行。
//代码随可以运行,但是对于原因不清楚,希望大家能给我讲讲,这两种写法为什么不同
- public InfoCategory[] GetInfo(string category)
- {
- string sql="select ID,InfoName from T_InfoCategory where InfoCategory=@InfoCategory";
- DataTable table = SqlHelper.ExecuteDataTable(sql, new SqlParameter("@InfoCateGory",category));
- InfoCategory[] infoCategorys = new InfoCategory[table.Rows.Count];
- for(int i=0;i<table.Rows.Count;++i)
- {
- DataRow row=table.Rows[i];
- <strong>InfoCategory info = new InfoCategory();
- info.ID = (Guid)row["ID"];
- info.InfoName = (string)row["InfoName"];
- infoCategorys[i] = info;</strong>
- }
- return infoCategorys;
- }
复制代码
}
|
|