C# 按列和行或任何替代项引用SQLDataReader中的数据
我正在运行一个存储过程,结果是这种格式C# 按列和行或任何替代项引用SQLDataReader中的数据,c#,asp.net,sqldatasource,sqldatareader,C#,Asp.net,Sqldatasource,Sqldatareader,我正在运行一个存储过程,结果是这种格式 +------+--------+-----+-------+ | ID | Resign | Sum | Count | +------+--------+-----+-------+ | 1234 | 0 | 400 | 3 | | 1234 | 1 | 800 | 4 | +------+--------+-----+-------+ 我尝试用这段代码引用查询返回的值,但它似乎没有按我希望的方式工作 if (
+------+--------+-----+-------+
| 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
}
我怎么能做这样的事。↓强>
我使用SQLDataReader并不重要
编辑:真实列名
ID Resigned sum count
--------- ----------- ---------------------- -----------
它不起作用,因为您的表中没有名为
“dedicated”
的列,就像您使用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]);
}
另外,请记住,在每次迭代或循环中,您将覆盖变量辞职
、辞职计数
或不辞职
和不辞职计数
因为您的表中没有名为辞职
的列,所以它不起作用,就像使用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]);
}
另外,请记住,在每次迭代或循环中,您将覆盖变量辞职
、辞职计数
或不辞职
和不辞职计数
您可以从过程中发布查询吗?
列名真的是“Sum”和“Count”吗?
有保留字,也许您应该尝试使用“AS”,并将这些保留字的其他名称指定给投影中的列。您可以发布过程中的查询吗?
列名真的是“Sum”和“Count”吗?
有保留字,也许您应该尝试使用“AS”,并为投影中的列指定其他名称。这样行吗
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"]);
}
}
这样行吗
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"]);
}
}
是的,列名是相同的。我可以改变它。我的最后一个代码实际上是psuedo代码。我不确定它是否有效。是的,列名是相同的。我可以改变它。我的最后一个代码实际上是psuedo代码。我不确定它是否能工作。Error 6运算符'=='不能应用于'object'和'int'类型的操作数。
我可能需要强制转换?@Thecrocodilehunter检查我的编辑。更改添加参数的方式。是。您需要使用:reader.GetInt()方法来检索值。+1用于代码和帮助。只缺少Convert.Into32()。需要与所有的参考。谢谢<代码>错误6运算符“==”不能应用于“object”和“int”类型的操作数。
我可能需要强制转换?@Thecrocodilehunter请检查我的编辑。更改添加参数的方式。是。您需要使用:reader.GetInt()方法来检索值。+1用于代码和帮助。只缺少Convert.Into32()。需要与所有的参考。谢谢请发布存储过程代码。请发布存储过程代码。