按列和行或任何替代方式引用 SQLDataReader 中的数据

发布于 2024-12-28 18:19:00 字数 1289 浏览 2 评论 0原文

我正在运行一个存储过程,结果是这种格式

+------+--------+-----+-------+
|  ID  | Resign | Sum | Count |
+------+--------+-----+-------+
| 1234 |      0 | 400 |     3 |
| 1234 |      1 | 800 |     4 |
+------+--------+-----+-------+

我尝试使用此代码来引用查询返回的值,但是它似乎没有按照我想要的方式工作

if (conn.State != ConnectionState.Open)
    conn.Open();
    SqlCommand sc = new SqlCommand();
    sc.CommandText = "usp_GetResignPool";
    sc.CommandType = CommandType.StoredProcedure;
    sc.Connection = conn;
    sc.Parameters.Add(AddParam(EndDate, "@EndDate"));
    sc.Parameters.Add(AddParam(am_id, "@id"));

    SqlDataReader reader;
    reader = sc.ExecuteReader();

 while (reader.Read())
            {
                if reader. // lost here
            }

我该如何做这样的事情。 ↓

int resigned = 0, resign_count = 0, not_resigned = 0, notresign_count =0;

if (read["Resigned"] == 1)
{
    resigned = read["sum"];
    resign_count = read["count"];
}
else
{
    not_resigned = read["sum"];
    notresign_count = read["count"]; 
}           

我使用 SQLDataReader 并不重要。

编辑:真实的列名称

ID        Resigned    sum                    count
--------- ----------- ---------------------- -----------

I am running a stored procedure and the result is this format

+------+--------+-----+-------+
|  ID  | Resign | Sum | Count |
+------+--------+-----+-------+
| 1234 |      0 | 400 |     3 |
| 1234 |      1 | 800 |     4 |
+------+--------+-----+-------+

I tried this code to reference the values returned by the query but, it seem not working the way I want it

if (conn.State != ConnectionState.Open)
    conn.Open();
    SqlCommand sc = new SqlCommand();
    sc.CommandText = "usp_GetResignPool";
    sc.CommandType = CommandType.StoredProcedure;
    sc.Connection = conn;
    sc.Parameters.Add(AddParam(EndDate, "@EndDate"));
    sc.Parameters.Add(AddParam(am_id, "@id"));

    SqlDataReader reader;
    reader = sc.ExecuteReader();

 while (reader.Read())
            {
                if reader. // lost here
            }

How can I do something like this. ↓

int resigned = 0, resign_count = 0, not_resigned = 0, notresign_count =0;

if (read["Resigned"] == 1)
{
    resigned = read["sum"];
    resign_count = read["count"];
}
else
{
    not_resigned = read["sum"];
    notresign_count = read["count"]; 
}           

It is not important that I used SQLDataReader.

Edit: Real column names

ID        Resigned    sum                    count
--------- ----------- ---------------------- -----------

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

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

发布评论

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

评论(3

伪装你 2025-01-04 18:19:00

它不起作用,因为您的表中没有名为 "Resigned" 的列,就像使用 SqlDataReader 时那样。

编辑:我认为问题的根源是您添加参数的方式。 AddParam() 不是您想要使用的方法。因此,您的结果集可能是空的。

....

SqlCommand sc = new SqlCommand(); 
sc.CommandText = "usp_GetResignPool"; 
sc.CommandType = CommandType.StoredProcedure; 
sc.Connection = conn; 
sc.Parameters.AddWithValue("@EndDate", EndDate);
sc.Parameters.AddWithValue("id", am_id);

SqlDataReader reader; 
reader = sc.ExecuteReader(); 

using (reader = sc.ExecuteReader())
{
    while (reader.Read())
    {
        if (Convert.ToInt32(read["Resign"]) == 1)   
        {   
            resigned = Convert.ToInt32(read["Sum"]);   
            resign_count = Convert.ToInt32(read["Count"]);   
        }   
        else   
        {   
            not_resigned = Convert.ToInt32(read["Sum"]);   
            notresign_count = Convert.ToInt32(read["Count"]);    
        } 
    }
}

请注意我如何将您的元素指示器更改为“辞职”。这需要与数据集中返回的列相匹配。或者,您可以使用列号来获取此值,如下所示:

        if (Convert.ToInt32(read[1]) == 1)   
        {   
            resigned = Convert.ToInt32(read[2]);   
            resign_count = read[3];   
        }   
        else   
        {   
            not_resigned = Convert.ToInt32(read[2]);   
            notresign_count = Convert.ToInt32(read[3]);    
        } 

此外,在每次迭代或 while 循环中保留 my that,您将覆盖变量< code>resigned、resign_countnot_resignednotresign_count

It didn't work because you don't have a column in your table named "Resigned", like you have when you are working with your SqlDataReader.

EDIT: I think the root of the problem is the way you are adding parameters. AddParam() is not the method you want to be using. Therefore, your result set is probably empty.

....

SqlCommand sc = new SqlCommand(); 
sc.CommandText = "usp_GetResignPool"; 
sc.CommandType = CommandType.StoredProcedure; 
sc.Connection = conn; 
sc.Parameters.AddWithValue("@EndDate", EndDate);
sc.Parameters.AddWithValue("id", am_id);

SqlDataReader reader; 
reader = sc.ExecuteReader(); 

using (reader = sc.ExecuteReader())
{
    while (reader.Read())
    {
        if (Convert.ToInt32(read["Resign"]) == 1)   
        {   
            resigned = Convert.ToInt32(read["Sum"]);   
            resign_count = Convert.ToInt32(read["Count"]);   
        }   
        else   
        {   
            not_resigned = Convert.ToInt32(read["Sum"]);   
            notresign_count = Convert.ToInt32(read["Count"]);    
        } 
    }
}

Notice how I changed your element indicator to "Resign". This needs to match the column that is returned in your dataset. Or, you could use a column number to get this, like so:

        if (Convert.ToInt32(read[1]) == 1)   
        {   
            resigned = Convert.ToInt32(read[2]);   
            resign_count = read[3];   
        }   
        else   
        {   
            not_resigned = Convert.ToInt32(read[2]);   
            notresign_count = Convert.ToInt32(read[3]);    
        } 

Also, keep in my that in every iteration or your while loop, you'll be overwriting the variables resigned, resign_count or not_resigned and notresign_count.

与往事干杯 2025-01-04 18:19:00

这行得通吗?

int resign = 0;
int not_resign = 0;
int resign_count = 0;
int not_resign_count = 0;

while (reader.Read())
{   
    if (Convert.ToInt32(reader["Resigned"]) == 1)
    {
        resign = Convert.ToInt32(reader["Sum"]);        
        resign_count = Convert.ToInt32(reader["Count"]);        
    }
    else
    {
        not_resign = Convert.ToInt32(reader["Sum"]);        
        not_resign_count = Convert.ToInt32(reader["Count"]);        
    } 
}

Would this work?

int resign = 0;
int not_resign = 0;
int resign_count = 0;
int not_resign_count = 0;

while (reader.Read())
{   
    if (Convert.ToInt32(reader["Resigned"]) == 1)
    {
        resign = Convert.ToInt32(reader["Sum"]);        
        resign_count = Convert.ToInt32(reader["Count"]);        
    }
    else
    {
        not_resign = Convert.ToInt32(reader["Sum"]);        
        not_resign_count = Convert.ToInt32(reader["Count"]);        
    } 
}
站稳脚跟 2025-01-04 18:19:00

您可以通过程序发布您的查询吗?
列名真的是“Sum”和“Count”吗?
有保留字,也许您应该尝试使用“AS”并为投影中的列指定其他名称。

Can you post your query from the procedure?
Are the column names really "Sum" and "Count"?
There are reserved words, maybe you should try using "AS" and give other names to these to columns in the projection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文