- 添加一个 ListBox 控件到表单中。 重新调整控件的大小,以填满表单的大部分宽度(如下所示)。
|
图 13: 添加了 ListBox 的表单 |
- 从查询中删除 where 子句,并添加以下列:
cmd.CommandText = "select deptno, dname, loc from dept" ' VB.NET
cmd.CommandText = "select deptno, dname, loc from dept"; // C#
- 修改 VB.NET 代码,最终结果如下:
Dim oradb As String = "Data Source=(DESCRIPTION=(ADDRESS_LIST=" _
+ "(ADDRESS=(PROTOCOL=TCP)(HOST=OTNSRVR)(PORT=1521)))" _
+ "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ORCL)));" _
+ "User Id=scott;Password=tiger;"
Dim conn As New OracleConnection(oradb) ' VB.NET
conn.Open()
Dim cmd As New OracleCommand
cmd.Connection = conn
cmd.CommandText = "select deptno, dname, loc from dept"
cmd.CommandType = CommandType.Text
Dim dr As OracleDataReader = cmd.ExecuteReader()
While dr.Read()
ListBox1.Items.Add("The " + dr.Item(1) + _
" department is in " + dr.Item("loc"))
End While
conn.Dispose()
修改您的 C# 代码,最终结果如下: string oradb = "Data Source=(DESCRIPTION=(ADDRESS_LIST="
+ "(ADDRESS=(PROTOCOL=TCP)(HOST=OTNSRVR)(PORT=1521)))"
+ "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ORCL)));"
+ "User Id=scott;Password=tiger;";
OracleConnection conn = new OracleConnection(oradb); // C#
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = "select dname from dept where deptno = 10";
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
ListBox1.Items.Add("The " + dr.Item(1) +
" department is in " + dr.GetString(0));
}
conn.Dispose();