CopyTo方法在参数化查询上构建数据库查询参数

时间:2013-04-04 15:22:39

标签: asp.net sql webmatrix-2

我在构建要传递给SQL查询的参数时遇到问题。提供值的表单有2个下拉列表和3个复选框部分。因此,根据http://forums.asp.net/t/1821264.aspx/1的代码,我有以下内容:

        var person = Request["chkPerson"];
    var spec = Request["chkSpec"];
    var gender = Request["genderSvd"];
    var county = Request["ctyLoc"];
    var crim = Request["chkCrim"];

    var temp1 = Request["chkPerson"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    var parms1 = temp1.Select((s, i) => "@" + i.ToString()).ToArray();
    var inclause1 = string.Join(",", parms1);

    var temp2 = Request["chkSpec"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    var parms2 = temp2.Select((s, i) => "@" + (i + parms1.Length).ToString()).ToArray();
    var inclause2 = string.Join(",", parms2);

    var temp3 = Request["genderSvd"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    var parms3 = temp3.Select((s, i) => "@" + (i + parms1.Length + parms2.Length).ToString()).ToArray();
    var inclause3 = string.Join(",", parms3);

    var temp4 = Request["ctyLoc"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    var parms4 = temp4.Select((s, i) => "@" + (i + parms1.Length + parms2.Length + parms3.Length).ToString()).ToArray();
    var inclause4 = string.Join(",", parms4);

    var temp5 = Request["chkCrim"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    var parms5 = temp5.Select((s, i) => "@" + (i + parms1.Length + parms2.Length + parms3.Length +parms4.Length).ToString()).ToArray();
    var inclause5 = string.Join(",", parms5);

    var db = Database.Open("HBDatabase");
    var selectCommand = String.Format(@"SELECT DISTINCT tblProgram.* FROM lnkPrgPersonSvd INNER JOIN tblProgram ON lnkPrgPersonSvd.prgId = tblProgram.prgId     
        INNER JOIN lnkPrgSpecial ON tblProgram.prgId = lnkPrgSpecial.prgId INNER JOIN lnkPrgCriminal ON tblProgram.prgId = lnkPrgCriminal.prgId
        WHERE lnkPrgPersonSvd.personId IN ({0}) AND lnkPrgSpecial.specId IN ({1}) AND tblProgram.prgGenderSvdId IN ({2})
        AND tblProgram.prgCounty IN ({3}) AND lnkPrgCriminal.criminalId IN ({4})", inclause1, inclause2, inclause3, inclause4, inclause5);

    var temp = new string [temp1.Length + temp2.Length + temp3.Length + temp4.Length + temp5.Length];
    temp1.CopyTo(temp, 0);
    temp2.CopyTo(temp, temp1.Length);
    temp3.CopyTo(temp, temp2.Length);
    temp4.CopyTo(temp, temp3.Length);
    temp5.CopyTo(temp, temp4.Length);

    var prgList = db.Query(selectCommand, temp);

person,spec和crim可以有多个值。性别和县只有一个值。

我似乎遇到问题的方法是建立temp变量,将适当的值传递给查询以匹配标量变量。

例如,如果表单发送:

person = 2,3
spec = 4
gender = 3
county = 2
crim = 1,6,8

我得到了查询的以下输出,每个临时(1,2,3,4,5)变量和临时变量:

* SELECT DISTINCT tblProgram。 FROM lnkPrgPersonSvd INNER JOIN tblProgram ON lnkPrgPersonSvd.prgId = tblProgram.prgId INNER JOIN lnkPrgSpecial ON tblProgram.prgId = lnkPrgSpecial.prgId INNER JOIN lnkPrgCriminal on tblProgram.prgId = lnkPrgCriminal.prgId WHERE LnkPrgPersonSvd.personId IN(@ 0,@ 1)AND lnkPrgSpecial.specId IN(@ 2)AND tblProgram.prgGenderSvdId IN(@ 3)AND tblProgram.prgCounty IN(@ 4)AND lnkPrgCriminal.criminalId IN(@ 5,@ 6 ,@ 7)

temp是:2 1 6 8

person(temp1)是:2 3

spec(temp2)是:4

性别(temp3)是:3

县(temp4)是:2

crim(temp5)是:1 6 8 **

显然,作为参数传递给查询的临时变量不正确。在我看来,应该在temp变量中列出所有temp1,temp2,temp3,temp4和temp5值以匹配标量变量。我甚至无法弄清楚如何使用我正在使用的代码在temp变量中生成这些值。

我曾尝试更改CopyTo代码以添加其他参数,但获取“无'重载方法的'x'参数,因此它只会占用那两个。

当我知道它应该很容易时,我正在把头发撕成一片。

有什么建议吗?

编辑:想出来。这是我应该如何构建临时变量:

int t2len = temp1.Length;
int t3len = t2len + temp2.Length;
int t4len = t3len + temp3.Length;
int t5len = t4len + temp4.Length;

var temp = new string[temp1.Length + temp2.Length + temp3.Length + temp4.Length + temp5.Length];
temp1.CopyTo(temp, 0);
temp2.CopyTo(temp, t2len);
temp3.CopyTo(temp, t3len);
temp4.CopyTo(temp, t4len);
temp5.CopyTo(temp, t5len);

0 个答案:

没有答案