获取的SqlDataAdapter和SqlCommand的困惑 [英] Getting SqlDataAdapter and SqlCommand confused

查看:136
本文介绍了获取的SqlDataAdapter和SqlCommand的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个调用方法:

 公共字符串RunReportSteps(INT _reportKey){
    数据表fStepsTable;
    fStepsTable = GetStepsTable(xxx.console.pr_xxx);
    返回(字符串)fStepsTable.Rows [1] [3];
}
 

它调用此私有方法:

 私人数据表GetStepsTable(字符串PROCNAME){
    VAR的connectionString = ConfigurationManager.ConnectionStrings [XXX]的ConnectionString。
    使用(VAR康恩=新的SqlConnection(的connectionString))
    使用(VAR适应=新的SqlDataAdapter())
    使用(VAR CMD =新的SqlCommand(PROCNAME,康涅狄格州)){

        conn.Open();

        SqlParameter的P =新的SqlParameter(@ ReportKey,this.ReportKey);
        p.Direction = ParameterDirection.Input;
        cmd.Parameters.Add(对);
        adapt.SelectCommand = CMD;

        数据集MYSET =新的DataSet();
        adapt.Fill(MYSET); //<<<<<<<<<<<<<<<<<<<错误,在这里
        返回mySet.Tables [0];
    }
}
 

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

  

过程或函数'pr_xxx预计参数   @ReportKey,但未提供。

解决方案

我认为 PROCNAME 是存储过程的名称。您还没有设置<一个href="http://msdn.microsoft.com/en-us/library/system.data.commandtype.aspx"><$c$c>CommandType在SqlCommand的以 StoredProcedure的

 使用(VAR康恩=新的SqlConnection(的connectionString))
使用(VAR CMD =新的SqlCommand(PROCNAME,康涅狄格州))
使用(VAR适应=新的SqlDataAdapter(CMD)){
    cmd.CommandType = CommandType.StoredProcedure; //&LT;&LT;&LT;这是失踪

    SqlParameter的P =新的SqlParameter(@ ReportKey,this.ReportKey);
    p.Direction = ParameterDirection.Input;
    cmd.Parameters.Add(对);

    数据表表=新的DataTable();
    adapt.Fill(表);
    返回表;
}
 

I've got this calling method:

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

It calls this private method:

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];
    }
}

Why am I getting the following error message?

Procedure or function 'pr_xxx' expects parameter '@ReportKey', which was not supplied.

解决方案

I assume that procName is the name of the stored-procedure. You haven't set the CommandType of the SqlCommand to 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;
}

这篇关于获取的SqlDataAdapter和SqlCommand的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆