1

我有这个调用方法:

public string RunReportSteps(int _reportKey) {
    DataTable fStepsTable;
    fStepsTable =  GetStepsTable("xxx.console.pr_xxx");
    return (string)fStepsTable.Rows[1][3];
}

它调用这个私有方法:

private DataTable GetStepsTable(string procName) {
    var connectionString = ConfigurationManager.ConnectionStrings["xxx"].ConnectionString;
    using(var conn = new SqlConnection(connectionString))
    using(var adapt = new SqlDataAdapter())
    using(var cmd = new SqlCommand(procName, conn)) {

        conn.Open();

        SqlParameter p = new SqlParameter("@ReportKey", this.ReportKey);
        p.Direction = ParameterDirection.Input;
        cmd.Parameters.Add(p);
        adapt.SelectCommand = cmd;

        DataSet mySet = new DataSet();
        adapt.Fill(mySet);     //<<<<<<<<<<<<<<<<<<<errors here
        return mySet.Tables[0];
    }
}

为什么我会收到以下错误消息?

过程或函数“pr_xxx”需要未提供的参数“@ReportKey”。

4

1 回答 1

5

我假设这procName是存储过程的名称。您尚未将CommandTypeSqlCommand 设置为StoredProcedure

using(var conn = new SqlConnection(connectionString))
using(var cmd = new SqlCommand(procName, conn))
using(var adapt = new SqlDataAdapter(cmd)) {
    cmd.CommandType = CommandType.StoredProcedure; // <<< this was missing

    SqlParameter p = new SqlParameter("@ReportKey", this.ReportKey);
    p.Direction = ParameterDirection.Input;
    cmd.Parameters.Add(p);

    DataTable table = new DataTable();
    adapt.Fill(table);     
    return table;
}
于 2012-11-26T15:08:25.157 回答