按列和行或任何替代方式引用 SQLDataReader 中的数据
我正在运行一个存储过程,结果是这种格式
+------+--------+-----+-------+
| 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它不起作用,因为您的表中没有名为
"Resigned"
的列,就像使用SqlDataReader
时那样。编辑:我认为问题的根源是您添加参数的方式。
AddParam()
不是您想要使用的方法。因此,您的结果集可能是空的。请注意我如何将您的元素指示器更改为
“辞职”
。这需要与数据集中返回的列相匹配。或者,您可以使用列号来获取此值,如下所示:此外,在每次迭代或
while
循环中保留 my that,您将覆盖变量< code>resigned、resign_count
或not_resigned
和notresign_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 yourSqlDataReader
.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.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:Also, keep in my that in every iteration or your
while
loop, you'll be overwriting the variablesresigned
,resign_count
ornot_resigned
andnotresign_count
.这行得通吗?
Would this work?
您可以通过程序发布您的查询吗?
列名真的是“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.