简体   繁体   English

来自C#的Oracle Connection无法读取数据

[英]Oracle Connection from C# not reading data

So the following code will not enter the loop to populate my list box. 因此,以下代码将不会进入循环以填充我的列表框。 My list box shows "Select All" and test outputs "entering loop". 我的列表框显示“全选”,测试输出“进入循环”。 What would cause the try not to fail but the loop not to execute either? 是什么导致尝试不失败但循环也不执行?

conn.Open();
OracleCommand executeQuery = new OracleCommand(sql, conn);
executeQuery.CommandType = CommandType.Text;

OracleDataReader dr = executeQuery.ExecuteReader();

lstInstructors.Items.Clear();
lstInstructors.Items.Add(new ListItem("Select All", "%"));

string test = "entering loop";

while (dr.Read())
{
    test = "start reading items";
    lstInstructors.Items.Add(new ListItem(dr.GetValue(0).ToString()));
    test += dr.GetValue(0).ToString();
}

Where is the value for "sql" set? “ sql”的值在哪里设置?

A different way to accomplish populating populating a list box would be to fill a data table then bind it to the list box. 完成填充列表框的另一种方法是填充数据表,然后将其绑定到列表框。

OracleDataAdapter da = new OracleDataAdapter();
DataTable dt = new DataTable();
var sql = "some sql string";
OracleCommand executeQuery = new OracleCommand(sql, conn);
using (da = new OracleDataAdapter(executeQuery))
{
    da.Fill(dt);
}
lstInstructors.DataSource = dt;
lstInstructors.DataTextField = "Field Name from sql";
lstInstructors.DataValueField = "Field Value from sql";
lstInstructors.DataBind();
lstInstructors.Items.Insert(0, new ListItem("Choose", "Choose"));
lstInstructors.SelectedValue = "Choose";

be sure to include using System.Data 确保包括使用System.Data

I've adapted this from the SqlDataAdapter so there may be some syntax differences. 我已经从SqlDataAdapter改编了它,因此可能存在一些语法差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM