[英]SQL reader read table 2 data using c#
I have 2 select statement in my stored procedure,im using sql reader to read my data,below is my codes,i wish to read 2nd table,any idea how to do it? 我的存储过程中有2条选择语句,我使用sql阅读器读取我的数据,下面是我的代码,我希望读取第二张表,有什么想法吗?
public BOL.HomeAnnouncement get_total(string subs_cd, string timee)
{
oConnection.ConnectionString = ConnectionString;
oConnection.Open();
BOL.HomeAnnouncement t_list = new BOL.HomeAnnouncement();
SqlCommand oCommand = new SqlCommand();
oCommand.Connection = oConnection;
oCommand.CommandType = CommandType.StoredProcedure;
oCommand.CommandText = "TMS";
oCommand.Parameters.Add(new SqlParameter("@SUBS", subs_cd));
oCommand.Parameters.Add(new SqlParameter("@time_in", timee));
SqlDataReader oReader = oCommand.ExecuteReader();
while (oReader.Read())
{
t_list.Name = oReader.IsDBNull(oReader.GetOrdinal("Total_Employee")) ? "" : oReader["Total_Employee"].ToString();
t_list.status = oReader.IsDBNull(oReader.GetOrdinal("Total_Present")) ? "" : oReader["Total_Present"].ToString();
t_list.Title = oReader.IsDBNull(oReader.GetOrdinal("Total_Absent")) ? "" : oReader["Total_Absent"].ToString();
}
oReader.Close();
oConnection.Close();
return t_list;
}
t_list.Name = oReader.IsDBNull(oReader.GetOrdinal("Table2.Total_Employee")) ? "" : oReader["Table2.Total_Employee"].ToString();
t_list.status = oReader.IsDBNull(oReader.GetOrdinal("Table2.Total_Present")) ? "" : oReader["Table2.Total_Present"].ToString();
t_list.Title = oReader.IsDBNull(oReader.GetOrdinal("Table2.Total_Absent")) ? "" : oReader["Table2.Total_Absent"].ToString();
tried this but not working.. table2.Total_Employee 尝试过此方法,但不起作用.. table2.Total_Employee
EDIT 编辑
Error returned: 返回错误:
System.IndexOutOfRangeException: Table2.Total_Employee
After error occurs: 发生错误后:
Error cause: 错误原因:
This exception has returned bur method GetOrdinal
of SqlDataReader
when column you passed as parameter doesn't exist. 当您作为参数传递的列不存在时,此异常返回了
SqlDataReader
bur方法GetOrdinal
。
If you go here you can read the MSDN explanation. 如果您去这里,您可以阅读MSDN解释。
Solution: 解:
I suppose your stored procedure output is different respect field you want to use in SqlDataReader
. 我想您的存储过程输出是您要在
SqlDataReader
使用的不同方面的字段。 Please post your stored procedure code for further help 请发布您的存储过程代码以获取更多帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.