在一个DataTable对象的情况下 多次Load,数据会覆盖吗?
DataReader对象也是多个.请说明原理!!
1 public DataTable GetWaitReport(PageHelper pInfo,string[] roles, string userId, string timeState, string timeEmd, string keyWord) {
2 #region 获取角色报告
3 DataTable dt = new DataTable();
4 if (roles!=null)
5 {
6 foreach (string roleName in roles)
7 {
8 pInfo.Fields = "*";
9 pInfo.Tables = "Report as r"
10 + "inner join Examine as e on(r.ID=e.ReportId)";
11 pInfo.PK = "r.ID";
12 pInfo.Sort = "r.CommitDate";
13 pInfo.Where = "where r.ID in( select ReportId from UserRelation where ExamineLock=1 and RoleName=@RoleName) and e.ExamineState=0";
14 if (!string.IsNullOrEmpty(timeState) && !string.IsNullOrEmpty(timeEmd))
15 pInfo.Where += " and e.ExamineDate between '" + timeState + " 00:00:00' and '" + timeEmd + " 23:59:59'";
16 if (!string.IsNullOrEmpty(keyWord))
17 pInfo.Where += " and r.ReportTitle like '%" + keyWord + "%'";
18
19 SqlParameter[] objPara = new SqlParameter[]{
20 new SqlParameter("@Tables",pInfo.Tables),
21 new SqlParameter("@PK",pInfo.PK),
22 new SqlParameter("@Sort",pInfo.Sort),
23 new SqlParameter("@PageNumber",pInfo.PageIndex),
24 new SqlParameter("@PageSize",pInfo.PageSize),
25 new SqlParameter("@Fields",pInfo.Fields),
26 new SqlParameter("@Filter",pInfo.Where),
27 new SqlParameter("@isCount",pInfo.isCount),
28 new SqlParameter("@RoleName",roleName)
29 };
30 SqlDataReader sdr = null;
31 new SqlHelper().RunProc("Pg_Paging", objPara, out sdr);
32 dt.Load(sdr);
33 //获取数据行数
34 SqlParameter[] objPara2 = new SqlParameter[]{
35 new SqlParameter("@Tables",pInfo.Tables),
36 new SqlParameter("@PK",pInfo.PK),
37 new SqlParameter("@Sort",pInfo.Sort),
38 new SqlParameter("@PageNumber",pInfo.PageIndex),
39 new SqlParameter("@PageSize",pInfo.PageSize),
40 new SqlParameter("@Fields",pInfo.Fields),
41 new SqlParameter("@Filter",pInfo.Where),
42 new SqlParameter("@isCount",pInfo.isCount),
43 new SqlParameter("@RoleName",roleName)
44 };
45 objPara2[objPara2.Length - 1].Value = !pInfo.isCount;
46 pInfo.RecordCount = new SqlHelper().RunProc("Pg_Paging", objPara2);
47 }
48 }
49 #endregion
50 #region 获取个人报告
51 if (userId != null || userId != "")
52 {
53 pInfo.Fields = "*";
54 pInfo.Tables = "Report as r"
55 + "inner join Examine as e on(r.ID=e.ReportId)";
56 pInfo.PK = "r.ID";
57 pInfo.Sort = "r.CommitDate";
58 pInfo.Where = "where r.ID in( select ReportId from UserRelation where ExamineLock=1 and UserId=@UserId) and e.ExamineState=0";
59 if (!string.IsNullOrEmpty(timeState) && !string.IsNullOrEmpty(timeEmd))
60 pInfo.Where += " and e.ExamineDate between '" + timeState + " 00:00:00' and '" + timeEmd + " 23:59:59'";
61 if (!string.IsNullOrEmpty(keyWord))
62 pInfo.Where += " and r.ReportTitle like '%" + keyWord + "%'";
63 //获取数据
64 SqlParameter[] objPara = new SqlParameter[]{
65 new SqlParameter("@Tables",pInfo.Tables),
66 new SqlParameter("@PK",pInfo.PK),
67 new SqlParameter("@Sort",pInfo.Sort),
68 new SqlParameter("@PageNumber",pInfo.PageIndex),
69 new SqlParameter("@PageSize",pInfo.PageSize),
70 new SqlParameter("@Fields",pInfo.Fields),
71 new SqlParameter("@Filter",pInfo.Where),
72 new SqlParameter("@isCount",pInfo.isCount),
73 new SqlParameter("@UserId",userId)
74 };
75 SqlDataReader sdr = null;
76 new SqlHelper().RunProc("Pg_Paging", objPara, out sdr);
77 dt.Load(sdr);
78
79 //获取数据行数
80 SqlParameter[] objPara2 = new SqlParameter[]{
81 new SqlParameter("@Tables",pInfo.Tables),
82 new SqlParameter("@PK",pInfo.PK),
83 new SqlParameter("@Sort",pInfo.Sort),
84 new SqlParameter("@PageNumber",pInfo.PageIndex),
85 new SqlParameter("@PageSize",pInfo.PageSize),
86 new SqlParameter("@Fields",pInfo.Fields),
87 new SqlParameter("@Filter",pInfo.Where),
88 new SqlParameter("@isCount",pInfo.isCount),
89 new SqlParameter("@UserId",userId)
90 };
91 objPara2[objPara2.Length - 1].Value = !pInfo.isCount;
92 pInfo.RecordCount = new SqlHelper().RunProc("Pg_Paging", objPara2);
93 }
94 #endregion
95 return dt;
当然会了~就像 a=2 a=3 a=4 当然 a最后等于4了
DataTable是个集合 不是值类型!你说会覆盖原理在哪? 是Load方法的特性吗
@詹成衡: 是不是 datatable的load方法?
如果是点击:http://msdn.microsoft.com/zh-cn/library/system.data.datatable.load 和原先的数据合并
@詹成衡: 那改变参数试一下不就知道了
@obananao: 我之前查了这个MSDN,不是太理解,合并是不管重复重复都合并,还是不重复的合并!如果我想清楚数据只有重新创建新对象或者使用Clear方法!
load是把数据重新绑定,即重新指向了新的内存堆栈。旧的被回收了。
你确定是一个DataTable对象的情况下嘛 是谁的重新指向啊??
根据传入参数LoadOperation进行合并,如果没有指定LoadOperation则有相同的行时,将进行Merge。
自己可以动手操作试一试,站在业务逻辑的角度上,这个很容易理解的。
根据我现有的的业务需求,不管数据如何重复 都不会合并 ,我将现有的存储过程修改,保证读取的数据唯一!问题虽解决了,但是合不合并跟数据库的标识列有没有关系,现在还不明。等手头工作搞定在研究。