【问题标题】:Retrieving data from Oracle从 Oracle 检索数据
【发布时间】:2014-12-12 18:00:19
【问题描述】:

我想从 oracle 数据库中检索数据,下面是我的代码, 我的 sql 查询有 4 个结果如何在我的标签中检索 4 个结果。

代码

OracleConnection con = new OracleConnection(
"Data Source=10.127.240.216/ipcldb;User ID=ipcltos;Password=ipcltos;Unicode=True");   
con.Open();
OracleCommand cmd = new OracleCommand(
"select Initcap(ZZFNAME) ZZFNAME 
from sap_empmst where substr(GBDAT,1,6) = substr(sysdate,1,6) 
and stat2 =3 and werks in('RIGC','IPGC') and plans<99999999 order by persk", con);      
OracleDataReader dr = cmd.ExecuteReader();
dr.Read();     
// int i;
//   for (i = 0; i < 10; i++)
Label1.Text = dr.GetString(0);
con.Dispose();

提前致谢。

【问题讨论】:

  • 4 个结果是指 4 行吗?您从该结果中获得了哪些数据?

标签: c# asp.net oracle10g


【解决方案1】:

选择使用标签来显示多行信息不是一个好的选择。我建议使用列表框或组合框或多行文本框或事件网格。 (所有为显示多个项目而构建的控件)

但是你的代码还有其他问题。

我会怎么写(仍然使用单个标签)

string cmdText = @"select Initcap(ZZFNAME) ZZFNAME 
                   from sap_empmst 
                   where substr(GBDAT,1,6) = substr(sysdate,1,6) and stat2 =3 
                         and werks in('RIGC','IPGC') 
                         and plans<99999999 
                   order by persk";
using(OracleConnection con = new OracleConnection(.....))
using(OracleCommand cmd = new OracleCommand(cmdText, con))
{
    con.Open();
    using(OracleDataReader dr = cmd.ExecuteReader())
    {
       while(dr.Read())
          Label1.Text += dr.GetString(0);
    }
}

解决单标签问题的方法是使用 dr.Read() 调用的布尔返回值围绕阅读器构建一个循环。然后使用 += 将当前记录值连接到标签的先前内容

另一个重要的变化是围绕using statement 确保正确关闭和处理连接、命令和读取器,即使出现异常

【讨论】:

  • 非常感谢......如果我想在相同标签中显示一个结果后换行,我该怎么做?
  • 在 GetString() 之后添加 &lt;br /&gt;。 IE dr.GetString(0) + &lt;br /&gt; 但标签应该更高。 stackoverflow.com/questions/13878347/…
猜你喜欢
  • 1970-01-01
  • 2017-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-29
  • 2020-07-25
  • 1970-01-01
  • 2021-05-27
相关资源
最近更新 更多