作者aeolus0829 (阿洽)
看板C_Sharp
标题[问题] 同一个变数复制值给两个变数
时间Sat Sep 17 21:55:59 2016
说明一下
我的原始资料是以 xml 存在於 table_content 里的 column 里,栏位名称 content
为了要搜寻资料,我用 xml 将 content 里的资料,用解析的方式写成 view
但因为 view 的速度实在太慢,为了改善效能,每天晚上排程将 view 转成 table
部份程式
tbCmd = new SqlCommand(); // 到 table 取值
vwCmd = new SqlCommand(); // 到 view 取值
Cmd = new SqlCommand();
List<string> vwCond = new List<string>();
List<string> tbCond = new List<string>();
List<string> Cond = new List<string>();
DataTable mainDt = new DataTable();
DataTable vwDt = new DataTable();
//开始指定共用的 sql 条件
if (txtVndrNm.Text != "")
{
Cond.Add("VENDOR_NAME LIKE '%' + @vendorNm + '%'");
Cmd.Parameters.Add("@vendorNm", SqlDbType.VarChar).Value =
txtVndrNm.Text.Trim();
}
// 把共用的 command text 分别复制给变数
vwCmd = Cmd;
tbCmd = Cmd;
// 把共用的 condition 分别复制给变数
vwCond = Cond;
tbCond = Cond;
//最後组装
//view 的部份限定只搜寻符合今天日期的资料 -> 效能考量
vwCond.Add("BEGIN_TIME>=@vwbgTimeStart");
vwCmd.Parameters.Add("@vwbgTimeStart", SqlDbType.VarChar).Value =
today + " 00:00";
vwCond.Add("BEGIN_TIME<=@vwbgTimeEnd");
vwCmd.Parameters.Add("@vwbgTimeEnd", SqlDbType.VarChar).Value =
today + " 23:59";
vwCmd.CommandText = bindContition(todaySql, vwCond);
tbCmd.CommandText = bindContition(oldSql, tbCond);
问题来了
在最後组装的时候,我将 "限定资料范围=今天" 的条件指派到 vwCond
但不知为何 tbCond 同样收到了... 囧rz
vwCond = Count = 3
tbCond = Count = 3
内容是一样的
0 = "VENDOR_NAME LIKE '%' + @vendorNm + '%'"
1 = "BEGIN_TIME>=@vwbgTimeStart"
2 = "BEGIN_TIME<=@vwbgTimeEnd"
tbCond 应该只有 count = 1 ,而且内容只有上面的 vendor_name
不知道哪里出了问题
我 google 过传值呼叫和传址呼叫,但和我的问题好像没有关系 ..
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 220.132.93.23
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_Sharp/M.1474120561.A.AF1.html
1F:推 soup514: value type vs reference type 09/17 22:23
意思是 vwCond 和 tbCond 都被我宣告成 reference type 吗?
但我看文件是说必须用 ref 才能宣告成 reference type ... = =a
是我误解了吗?
後来用这个方法处理
vwCmd = tbCmd = Cmd;
List<string> vwCond = new List<string>(Cond);
List<string> tbCond = new List<string>(Cond);
我知道应该是我的宣告是变成 reference type ,
变数2 = 变数1
变数3 = 变数1
刚 google 到大概可以做出这样的结论
想知道有没比较好的做法
m(_ _)m
※ 编辑: aeolus0829 (220.132.93.23), 09/17/2016 23:06:49
2F:推 CrazyAngel: 泛型就是参考型别。也可以用AddRange把相同条件加进去 09/18 00:24
啊.. 这个方法昨天看到的网页好像也有提到
http://iblog.16806.com/?p=119
3F:→ Litfal: 把建立Command的方法拉到另一个函数去做 09/18 01:19
目前这个函数是判断使用者有没有输入条件
有就丢到 list 里
检察完会再呼叫另一个函数将 list 组装成 command text
我想你应该是这个意思?
※ 编辑: aeolus0829 (220.132.93.23), 09/18/2016 11:13:28