查询没有给出结果c#

时间:2015-02-12 11:55:53

标签: c# sql visual-studio-2010

我已经在这样的SSMS中测试了这个查询

SELECT     S_P_name, S_P_pack, FLOOR(SUM(S_P_ttoqty) / S_P_pack) AS ost, SUM(S_P_ttoqty) % S_P_pack AS otb, FLOOR(SUM(S_P_ttsold) / S_P_pack) AS sst, 
                      SUM(S_P_ttsold) % S_P_pack AS stb, FLOOR(SUM(S_P_ttqty) / S_P_pack) AS pst, SUM(S_P_ttqty) % S_P_pack AS ptb, FLOOR(SUM(S_P_ttavail) / S_P_pack) AS ast, 
                      SUM(S_P_ttavail) % S_P_pack AS atb, CAST(SUM(S_P_ttavail * S_P_prate / S_P_pack) AS decimal(9, 2)) AS grs
FROM         STOCK
WHERE     (S_type <> 'phy') AND (Purchase_date BETWEEN '2014/1/25' AND '2015/12/25')
GROUP BY S_P_name, S_P_pack

它给了我期望的结果。但是当我使用c#代码实现它时,它无法正常工作。

这就是我做的事情

cmd = new SqlCommand("SELECT S_P_name, S_P_pack, FLOOR(SUM(S_P_ttoqty) / S_P_pack) AS ost, SUM(S_P_ttoqty) % S_P_pack AS otb, FLOOR(SUM(S_P_ttsold) / S_P_pack) AS sst, SUM(S_P_ttsold) % S_P_pack AS stb, FLOOR(SUM(S_P_ttqty) / S_P_pack) AS pst, SUM(S_P_ttqty) % S_P_pack AS ptb, FLOOR(SUM(S_P_ttavail) / S_P_pack) AS ast, SUM(S_P_ttavail) % S_P_pack AS atb, cast(SUM(S_P_ttavail*S_P_prate/S_P_pack)as decimal(9,2))AS grs FROM STOCK Where S_type<>'phy' AND Purchase_date between "+fromDate.ToShortDateString()+" And "+Todate.ToShortDateString()+" GROUP BY S_P_name, S_P_pack", conn);
            conn.Open();
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                int i = 0;
                dgv1Clossing.Rows.Insert(i);
                dgv1Clossing.Rows[i].Cells["S_P_name"].Value = dr["S_P_name"].ToString();
                dgv1Clossing.Rows[i].Cells["S_P_pack"].Value = dr["S_P_pack"].ToString();
                dgv1Clossing.Rows[i].Cells["S_opn_strp"].Value = dr["ost"].ToString();
                dgv1Clossing.Rows[i].Cells["S_opn_tabs"].Value = dr["otb"].ToString();
                dgv1Clossing.Rows[i].Cells["S_pur_strp"].Value = dr["pst"].ToString();
                dgv1Clossing.Rows[i].Cells["S_pur_tabs"].Value = dr["ptb"].ToString();
                dgv1Clossing.Rows[i].Cells["S_sold_strp"].Value = dr["sst"].ToString();
                dgv1Clossing.Rows[i].Cells["S_sold_tabs"].Value = dr["stb"].ToString();
                dgv1Clossing.Rows[i].Cells["S_avail_strp"].Value = dr["ast"].ToString();
                dgv1Clossing.Rows[i].Cells["S_avail_tabs"].Value = dr["atb"].ToString();
                dgv1Clossing.Rows[i].Cells["S_avail_total"].Value = dr["grs"].ToString();
                i++;
            }
            conn.Close();

关于调试我发现这不会引发任何异常或错误。但它没有进入循环。 while(dr.read())。我的表中有3个相关的行,这些行是SSMS中的结果,但不在此。 帮助我知道,我在哪里出错了。

2 个答案:

答案 0 :(得分:4)

cmd = new SqlCommand("SELECT S_P_name, S_P_pack, FLOOR(SUM(S_P_ttoqty) / S_P_pack) AS ost, SUM(S_P_ttoqty) % S_P_pack AS otb, FLOOR(SUM(S_P_ttsold) / S_P_pack) AS sst, SUM(S_P_ttsold) % S_P_pack AS stb, FLOOR(SUM(S_P_ttqty) / S_P_pack) AS pst, SUM(S_P_ttqty) % S_P_pack AS ptb, FLOOR(SUM(S_P_ttavail) / S_P_pack) AS ast, SUM(S_P_ttavail) % S_P_pack AS atb, cast(SUM(S_P_ttavail*S_P_prate/S_P_pack)as decimal(9,2))AS grs FROM STOCK Where S_type<>'phy' AND Purchase_date between '"+fromDate.ToShortDateString()+"' And '"+Todate.ToShortDateString()+"' GROUP BY S_P_name, S_P_pack", conn);

把这个sqlcommmand测试一下。我在你失踪之前和之后添加了'。 希望它会对你有所帮助。

  

被修改

cmd = new SqlCommand("SELECT S_P_name, S_P_pack, FLOOR(SUM(S_P_ttoqty) / S_P_pack) AS ost, SUM(S_P_ttoqty) % S_P_pack AS otb, FLOOR(SUM(S_P_ttsold) / S_P_pack) AS sst, SUM(S_P_ttsold) % S_P_pack AS stb, FLOOR(SUM(S_P_ttqty) / S_P_pack) AS pst, SUM(S_P_ttqty) % S_P_pack AS ptb, FLOOR(SUM(S_P_ttavail) / S_P_pack) AS ast, SUM(S_P_ttavail) % S_P_pack AS atb, cast(SUM(S_P_ttavail*S_P_prate/S_P_pack)as decimal(9,2))AS grs FROM STOCK Where S_type<>'phy' AND Purchase_date between '" + fromDate.ToString("yyyyMMdd") +"' And '" + Todate.ToString("yyyyMMdd") +"' GROUP BY S_P_name, S_P_pack", conn);

答案 1 :(得分:1)

尝试使用SqlParameter Class。不建议直接在C#中向字符串查询插入参数。

cmd = new SqlCommand("SELECT S_P_name, S_P_pack, FLOOR(SUM(S_P_ttoqty) / S_P_pack) AS ost, SUM(S_P_ttoqty) % S_P_pack AS otb, FLOOR(SUM(S_P_ttsold) / S_P_pack) AS sst, SUM(S_P_ttsold) % S_P_pack AS stb, FLOOR(SUM(S_P_ttqty) / S_P_pack) AS pst, SUM(S_P_ttqty) % S_P_pack AS ptb, FLOOR(SUM(S_P_ttavail) / S_P_pack) AS ast, SUM(S_P_ttavail) % S_P_pack AS atb, cast(SUM(S_P_ttavail*S_P_prate/S_P_pack)as decimal(9,2))AS grs FROM STOCK Where S_type<>'phy' AND Purchase_date between @FromDate And @Todate GROUP BY S_P_name, S_P_pack", conn);

cmd.Parameters.Add(
"@FromDate", SqlDbType.DateTime).Value = fromDate;
cmd.Parameters.Add(
"@ToDate", SqlDbType.DateTime).Value = Todate;