0

アプリで次のクエリを実行しようとしています。

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

x<column name>これにより、指定されたステータス セットを持つ、指定されたコンプレックス (私は常にパラメーター名に使用し、常に正常に動作します) のジョブ テーブルに格納されているすべてのメンテナンス ジョブのリストが取得されます。

ここで、これをテキスト コマンドとして (System.Data.CommandType.Text を使用して) 実行すると、次のエラーが発生します (値を入力してデータベースでクリーンなクエリを実行すると機能します)。

where 句の不明な列 'xcomplexid'

この選択をストアド プロシージャにコピーしてSystem.Data.CommandType.StoredProcedure代わりに使用すると、機能します。

これにより、私が何をすべきかが明らかになりますが、なぜ.NETまたはMySQLが文字通りSPの使用を強制しているのか本当に理解できません...これは私が間違っていることですか?

私の完全なコードは次のとおりです (この約半分はまだテストされていないことに注意してください)。

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 に答える