简体   繁体   English

C# SQL 服务器存储过程未返回预期值

[英]C# SQL Server stored procedure not returning expected value

I wrote up this function to return a dataset, I was expecting a smaller dataset as there's only one value I was expecting back, but I get a rather bloated object back which I cannot find the value I am looking for, this is causing problems as I intend to use this function heavily.我写了这个 function 来返回一个数据集,我期待一个较小的数据集,因为我只期望返回一个值,但是我得到一个相当臃肿的 object 回来,我找不到我正在寻找的值,这会导致问题我打算大量使用这个 function。

I was hoping someone could spot what I am doing wrong, I have included the code, a screenshot of the returned object and what I am expecting.我希望有人能发现我做错了什么,我已经包含了代码、返回的 object 的屏幕截图以及我的期望。 Any help would be greatly appreciated.任何帮助将不胜感激。

If I have not phrased anything in this question correctly feel free to let me know, I struggle to express my thoughts well.如果我在这个问题中没有正确表达任何内容,请随时告诉我,我很难很好地表达我的想法。

public DataSet getPartnerParameter(string parameter)
{
    using (var dbConnection = new SqlConnection(UnityHelper.IocContainer.Resolve<IConfigHelperService>().GetConnectionString("CASConnectionString")))
    {
        dbConnection.Open();

        using (var dbCommand = new SqlCommand("GETPARTNERPARAMETER"))
        {
            dbCommand.CommandType = CommandType.StoredProcedure;
            dbCommand.Connection = dbConnection;

            SqlParameter lstrParameter = new SqlParameter("@Parameter", SqlDbType.VarChar);
            lstrParameter.Value = parameter;
            dbCommand.Parameters.Add(lstrParameter);

            var ldaDPS = new SqlDataAdapter(dbCommand);
            var ldstParameterValues = new DataSet();
            ldaDPS.Fill(ldstParameterValues);

            return ldstParameterValues;
        }
    }
}

数据对象内容

This is what I am expecting to find这是我期望找到的

执行存储过程

edit//编辑//

changed my code slightly but still not working.稍微更改了我的代码,但仍然无法正常工作。

public String[] getPartnerParameter(string parameter)
{
    using (var dbConnection = new SqlConnection(UnityHelper.IocContainer.Resolve<IConfigHelperService>().GetConnectionString("CASConnectionString")))
    {
        dbConnection.Open();

        SqlCommand dbCommand = new SqlCommand("GETPARTNERPARAMETER", dbConnection);
        dbCommand.CommandType = CommandType.StoredProcedure;

        SqlParameter lstrParameter = new SqlParameter("@Parameter", SqlDbType.VarChar);
        lstrParameter.Value = parameter;
        dbCommand.Parameters.Add(lstrParameter);

        SqlDataReader reader = dbCommand.ExecuteReader();
        string[] results = new string[2];
        while (reader.Read())
        {

            results[0] = reader[0].ToString();
            results[1] = reader[1].ToString();
    
        }
        if (results.Length < 1)
        {
            results[0] = "Cannot find Value";
            results[1] = "S";
            return results;
        }
        else
        {

            return results;
        }
    }

The error is this: {"Procedure or function 'GETPARTNERPARAMETER' expects parameter '@Parameter', which was not supplied."}错误是这样的:{“程序或 function 'GETPARTNERPARAMETER' 需要参数 '@Parameter',但未提供。”}

The values you are looking for are probably in the dataSet.Tables[0].Rows[0] row.您要查找的值可能在 dataSet.Tables[0].Rows[0] 行中。

However, if you are expecting one row back, a DataSet object seems like overkill.但是,如果您期望返回一行,则 DataSet object 似乎有点矫枉过正。 I would recommend avoiding the SqlDataAdapter/DataSet and instead use a SqlDataReader.我建议避免使用 SqlDataAdapter/DataSet,而是使用 SqlDataReader。

Untested code, but should give you the gist of how to use it:未经测试的代码,但应该为您提供如何使用它的要点:

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();

    SqlCommand dbCommand = new SqlCommand("GETPARTNERPARAMETER", connection);
    dbCommand.CommandType = CommandType.StoredProcedure;
    SqlParameter lstrParameter = new SqlParameter("@Parameter", SqlDbType.VarChar);
    lstrParameter.Value = "LexisNexisCreditConsentRequired";
    dbCommand.Parameters.Add(lstrParameter);

    SqlDataReader reader = dbCommand.ExecuteReader();
    while (reader.Read())
    {
        var yourValue = reader[0];
        var yourDataType = reader[1];
    }
}

A DataSet is an object which can contain many tables.一个数据DataSet是一个 object 可以包含许多表。 It doesn't have to, but it can, and so it also has a number of fields, properties, and methods to support that role.它不是必须的,但它可以,因此它还有许多字段、属性和方法来支持该角色。

For this query, look at ldstParameterValues.Tables[0].Rows[0] .对于此查询,请查看ldstParameterValues.Tables[0].Rows[0] Within that row, you can also see the columns with another level of bracket-indexing:在该行中,您还可以看到具有另一级别括号索引的列:

DataRow row = ldstParameterValues.Tables[0].Rows[0];
var column0Value row[0];
var column1Value = row[1];

However, the type for these results is object .但是,这些结果的类型是object You'll need to either cast the values or use one of the GetX() methods on the datarow to get results with a meaningful type.您需要转换值或使用数据行上的GetX()方法之一来获得具有有意义类型的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM