SqlDataAdapter 输出变量问题 C#

发布于 2024-09-26 06:03:02 字数 3232 浏览 7 评论 0原文

我不清楚如何在使用 C# 时格式化输出变量的 SqlDataAdapter

错误消息:

索引(从零开始)必须大于或等于零且小于参数列表的大小。

代码示例(存储过程工作正常):

private DataTable updateOrdEodHold(DataTable tb, out string mnpft,
    out string authld, out string trd, out string hld, out string extnow)
{
    // Start the connection string.
    string connstr = ConfigurationManager.ConnectionStrings
        ["LocalSqlServer"].ConnectionString;
    SqlConnection myConn = new SqlConnection(connstr);
    // Declare symbol and assign for Errors Catch Exception.
    string Symbol = "";
    string sqlComm = "dbo.UpdateOrdEodHold";
    DataTable HoldVals = new DataTable();
    SqlDataAdapter dataAdp = new SqlDataAdapter(sqlComm, myConn);
    dataAdp.SelectCommand.CommandType = CommandType.StoredProcedure;
    string ticker = (string)Convert.ToString(tb.Rows[0]["Ticker"]);
    // update Symbol for Catch ex
    Symbol = ticker.ToString();
    String company = (string)Convert.ToString(tb.Rows[0]["Company"]);
    String avgprofit = (string)Convert.ToString(tb.Rows[0]["AvgProfit"]);
    String extdte = (string)Convert.ToString(tb.Rows[0]["ExitDate"]);
    dataAdp.SelectCommand.Parameters.Clear();
    dataAdp.SelectCommand.Parameters.Add(new SqlParameter("@ticker",
        SqlDbType.VarChar, 10));
    dataAdp.SelectCommand.Parameters["@ticker"].Value =
        (string)ticker.ToString();
    dataAdp.SelectCommand.Parameters.Add(new SqlParameter("@company",
        SqlDbType.VarChar, 25));
    dataAdp.SelectCommand.Parameters["@company"].Value =
        (string)company.ToString();
    dataAdp.SelectCommand.Parameters.Add(new SqlParameter("@avgpft",
        SqlDbType.VarChar, 10));
    dataAdp.SelectCommand.Parameters["@avgpft"].Value =
        (string)avgprofit.ToString();
    dataAdp.SelectCommand.Parameters.Add(new SqlParameter("@mnpft",
        SqlDbType.VarChar, 10));
    dataAdp.SelectCommand.Parameters["@mnpft"].Direction =
        ParameterDirection.Output;
    dataAdp.SelectCommand.Parameters.Add(new SqlParameter("@authld",
        SqlDbType.VarChar, 6));
    dataAdp.SelectCommand.Parameters["@authld"].Direction =
        ParameterDirection.Output;
    dataAdp.SelectCommand.Parameters.Add(new SqlParameter("@hld",
        SqlDbType.VarChar, 6));
    dataAdp.SelectCommand.Parameters["@hld"].Direction =
        ParameterDirection.Output;
    dataAdp.SelectCommand.Parameters.Add(new SqlParameter("@trd",
        SqlDbType.VarChar, 6));
    dataAdp.SelectCommand.Parameters["@trd"].Direction =
        ParameterDirection.Output;
    dataAdp.SelectCommand.Parameters.Add(new SqlParameter("@extnow",
        SqlDbType.VarChar, 6));
    dataAdp.SelectCommand.Parameters["@extnow"].Direction =
        ParameterDirection.Output;
    dataAdp.SelectCommand.Parameters.Add(new SqlParameter("@extdte",
        SqlDbType.VarChar, 15));
    dataAdp.SelectCommand.Parameters["@extdte"].Value =
        (string)extdte.ToString();
    dataAdp.Fill(HoldVals);
    mnpft = HoldVals.Rows[0]["MinProfit"].ToString();
    authld = HoldVals.Rows[0]["AutoHold"].ToString();
    trd = HoldVals.Rows[0]["Trade"].ToString();
    hld = HoldVals.Rows[0]["Hold"].ToString();
    extnow = HoldVals.Rows[0]["ExitNow"].ToString();
    return HoldVals;
}

I do not clearly understand how to format the SqlDataAdapter for output variables when working with C#

Error Message:

Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

Code Example (Stored Procedure works fine):

private DataTable updateOrdEodHold(DataTable tb, out string mnpft,
    out string authld, out string trd, out string hld, out string extnow)
{
    // Start the connection string.
    string connstr = ConfigurationManager.ConnectionStrings
        ["LocalSqlServer"].ConnectionString;
    SqlConnection myConn = new SqlConnection(connstr);
    // Declare symbol and assign for Errors Catch Exception.
    string Symbol = "";
    string sqlComm = "dbo.UpdateOrdEodHold";
    DataTable HoldVals = new DataTable();
    SqlDataAdapter dataAdp = new SqlDataAdapter(sqlComm, myConn);
    dataAdp.SelectCommand.CommandType = CommandType.StoredProcedure;
    string ticker = (string)Convert.ToString(tb.Rows[0]["Ticker"]);
    // update Symbol for Catch ex
    Symbol = ticker.ToString();
    String company = (string)Convert.ToString(tb.Rows[0]["Company"]);
    String avgprofit = (string)Convert.ToString(tb.Rows[0]["AvgProfit"]);
    String extdte = (string)Convert.ToString(tb.Rows[0]["ExitDate"]);
    dataAdp.SelectCommand.Parameters.Clear();
    dataAdp.SelectCommand.Parameters.Add(new SqlParameter("@ticker",
        SqlDbType.VarChar, 10));
    dataAdp.SelectCommand.Parameters["@ticker"].Value =
        (string)ticker.ToString();
    dataAdp.SelectCommand.Parameters.Add(new SqlParameter("@company",
        SqlDbType.VarChar, 25));
    dataAdp.SelectCommand.Parameters["@company"].Value =
        (string)company.ToString();
    dataAdp.SelectCommand.Parameters.Add(new SqlParameter("@avgpft",
        SqlDbType.VarChar, 10));
    dataAdp.SelectCommand.Parameters["@avgpft"].Value =
        (string)avgprofit.ToString();
    dataAdp.SelectCommand.Parameters.Add(new SqlParameter("@mnpft",
        SqlDbType.VarChar, 10));
    dataAdp.SelectCommand.Parameters["@mnpft"].Direction =
        ParameterDirection.Output;
    dataAdp.SelectCommand.Parameters.Add(new SqlParameter("@authld",
        SqlDbType.VarChar, 6));
    dataAdp.SelectCommand.Parameters["@authld"].Direction =
        ParameterDirection.Output;
    dataAdp.SelectCommand.Parameters.Add(new SqlParameter("@hld",
        SqlDbType.VarChar, 6));
    dataAdp.SelectCommand.Parameters["@hld"].Direction =
        ParameterDirection.Output;
    dataAdp.SelectCommand.Parameters.Add(new SqlParameter("@trd",
        SqlDbType.VarChar, 6));
    dataAdp.SelectCommand.Parameters["@trd"].Direction =
        ParameterDirection.Output;
    dataAdp.SelectCommand.Parameters.Add(new SqlParameter("@extnow",
        SqlDbType.VarChar, 6));
    dataAdp.SelectCommand.Parameters["@extnow"].Direction =
        ParameterDirection.Output;
    dataAdp.SelectCommand.Parameters.Add(new SqlParameter("@extdte",
        SqlDbType.VarChar, 15));
    dataAdp.SelectCommand.Parameters["@extdte"].Value =
        (string)extdte.ToString();
    dataAdp.Fill(HoldVals);
    mnpft = HoldVals.Rows[0]["MinProfit"].ToString();
    authld = HoldVals.Rows[0]["AutoHold"].ToString();
    trd = HoldVals.Rows[0]["Trade"].ToString();
    hld = HoldVals.Rows[0]["Hold"].ToString();
    extnow = HoldVals.Rows[0]["ExitNow"].ToString();
    return HoldVals;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

颜漓半夏 2024-10-03 06:03:02

您需要保存对输出参数变量的引用,以便在适配器执行命令后可以使用parameter.Value访问返回给它的值。

//Create the parameter 
SqlParameter parameter = new SqlParameter("@mnpft", SqlDbType.VarChar);

//Set the parameter direction as output
parameter.Direction = ParameterDirection.Output;

sqlCommand.Parameters.Add(parameter);

SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCommand);
sqlAdapter.Fill(dataSet);

//Fetch the output parameter after doing the Fill
string outputValue = Convert.ToString(parameter.Value);

You need to hold a reference to the Output parameter variable so that you can access the value returned to it using parameter.Value once the adapter has executed the command.

//Create the parameter 
SqlParameter parameter = new SqlParameter("@mnpft", SqlDbType.VarChar);

//Set the parameter direction as output
parameter.Direction = ParameterDirection.Output;

sqlCommand.Parameters.Add(parameter);

SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCommand);
sqlAdapter.Fill(dataSet);

//Fetch the output parameter after doing the Fill
string outputValue = Convert.ToString(parameter.Value);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文