0

I'm trying to run the following query in my app:

SELECT * FROM jobs jo
INNER JOIN units un ON jo.unitid = un.unitid
INNER JOIN complex co ON un.complexid = co.complexid
WHERE co.complexid = xcomplexid AND jo.status = xstatus

This should get a list of all maintenance jobs stored in the jobs table for the specified complex (I always use x<column name> for my parameter names and they always work fine) that have the specified status set.

Now, if I run this through as a text command (using System.Data.CommandType.Text), I get the following error (it works if I run the query clean on the db with filled in values):

Unknown column 'xcomplexid' in where clause

If I copy this select into a stored procedure and use System.Data.CommandType.StoredProcedure instead, it'll work.

While this makes it obvious what I should do, I really don't understand why .NET or MySQL are literally forcing me into using SPs... Is this something I'm doing wrong?

My full code follows (please be aware that about half of this is as yet untested):

private enum FilterType
{
    None = 0,
    Complex = 1,
    DateRange = 2,
    Subcontractor = 3,
    MultipleSubcontractors = 4
}

private FilterType filter;

private void loadReport()
{
    string cmd = String.Empty;

    List<MySqlParameter> args = new List<MySqlParameter>();

    // xstatus will be used in all Report queries
    args.Add(new MySqlParameter("xstatus", MySqlDbType.VarChar));
    args[args.Count - 1].Value = ddlStatus.SelectedItem.Value;

    // filter has been set previously in execution by actions performed by the user
    switch (filter)
    {
        case FilterType.Complex:
            cmd = "select * from jobs jo inner join units un on jo.unitid = un.unitid inner join complex co on un.complexid = co.complexid where co.complexid = xcomplexid and jo.status = xstatus";

            args.Add(new MySqlParameter("xcomplexid", MySqlDbType.Int32));
            args[args.Count - 1].Value = Convert.ToInt32(ddlComplex.SelectedItem.Value);
            break;
        // other cases will appear here
    }

    // Database(string CommandText, System.Data.CommandType CommandType, List<MySqlParameter Parameters)
    using (Database db = new Database(cmd, System.Data.CommandType.Text, args))
    {
        Session["reportData"] = db.GetDataTable();
        // db.GetDataTable simply populates a data table
        // with the data returned by the query through the
        // use of a DataAdapter

        gvItems.DataSource = (DataTable)Session["reportData"];
        gvItems.DataBind();
    }
}
4

2 回答 2

0

尝试这个-

cmd = "select * from jobs jo inner join units un on jo.unitid = un.unitid inner join complex co on un.complexid = co.complexid where co.complexid = @xcomplexid and jo.status = @xstatus";

args.Add(new MySqlParameter("@xcomplexid", MySqlDbType.Int32));
于 2013-09-17T10:09:33.263 回答
0

作为文本命令运行时,您必须向命令对象添加一个带有名称的参数才能'@xcomplexid'使其工作。(就像您对存储过程所做的那样。)

cmd = "select * from jobs jo inner join units un on jo.unitid = un.unitid inner join complex co on un.complexid = co.complexid where co.complexid = @xcomplexid and jo.status = xstatus";

            args.Add(new MySqlParameter("@xcomplexid", MySqlDbType.Int32));
            args[args.Count - 1].Value = Convert.ToInt32(ddlComplex.SelectedItem.Value);
            break;
于 2013-09-17T10:06:03.547 回答