Dim conn As New OracleConnection() ' VB.NETconn.ConnectionString = oradbOracleConnection conn = new OracleConnection(); // C#conn.ConnectionString = oradb;
Dim sql As String = "select dname from dept where deptno = 10" ' VB.NETDim cmd As New OracleCommand(sql, conn)cmd.CommandType = CommandType.Textstring sql = "select dname from dept where deptno = 10"; // C#OracleCommand cmd = new OracleCommand(sql, conn);cmd.CommandType = CommandType.Text;
Dim dr As OracleDataReader = cmd.ExecuteReader() ' VB.NETdr.Read()Label1.Text = dr.Item("dname") ' retrieve by column nameLabel1.Text = dr.Item(0) ' retrieve the first column in the select listLabel1.Text = dr.GetString(0) ' retrieve the first column in the select list
Dim conn As New OracleConnection(oradb) ' VB.NETTry conn.Open() Dim cmd As New OracleCommand cmd.Connection = conn cmd.CommandText = "select dname from dept where deptno = " + TextBox1.Textcmd.CommandType = CommandType.Text If dr.Read() Then Label1.Text = dr.Item("dname") ' or use dr.Item(0) End IfCatch ex As Exception ' catches any error MessageBox.Show(ex.Message.ToString())Finally conn.Dispose()End TryOracleConnection conn = new OracleConnection(oradb); // C#try{conn.Open();OracleCommand cmd = new OracleCommand(); cmd.Connection = conn; cmd.CommandText = "select dname from dept where deptno = " + textBox1.Text;cmd.CommandType = CommandType.Text; if (dr.Read()) // C# { label1.Text = dr.GetString(0); }}catch (Exception ex) // catches any error{ MessageBox.Show(ex.Message.ToString());}finally{ conn.Dispose();}
Label1.Text = "The " + dr.Item(1) + " department is in " + dr.Item("loc") ' VB.NETLabel1.Text = "The " + dr.GetString(1) + " department is in " + dr.GetString(2); // C#
現在我們進行返回多行的查詢:
select deptno, dname, loc from dept
要處理從 DataReader 中返回的多行,需要某種類型的迴圈結構。 此外,需要一個可以顯示多行的控制項。 DataReader 是一個僅正向的唯讀遊標,因此不能將其與可更新或完全可滾動的控制項(如 Windows Forms DataGrid 控制項)捆綁在一起。 DataReader 與 ListBox 控制項相容,如以下程式碼片段所示:
While dr.Read() ' VB.NET ListBox1.Items.Add("The " + dr.Item(1) + " department is in " + dr.Item("loc")) End Whilewhile (dr.Read()) // C#{ listBox1.Items.Add("The " + dr.GetString(1) + " department is in " + dr.GetString(2);}
在 Private Sub 和 End Sub 語句之間添加 VB.NET 版本 事件語句代碼(確保用您的伺服器的主機名稱替代 OTNSRVR):
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.NETconn.Open()Dim cmd As New OracleCommandcmd.Connection = conncmd.CommandText = "select dname from dept where deptno = 10"cmd.CommandType = CommandType.TextDim dr As OracleDataReader = cmd.ExecuteReader()dr.Read()Label1.Text = dr.Item("dname") ' or dr.Item(0)conn.Dispose()
cmd.CommandText = "select deptno, dname, loc from dept" ' VB.NETcmd.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.NETconn.Open()Dim cmd As New OracleCommandcmd.Connection = conncmd.CommandText = "select deptno, dname, loc from dept" cmd.CommandType = CommandType.TextDim dr As OracleDataReader = cmd.ExecuteReader()While dr.Read() ListBox1.Items.Add("The " + dr.Item(1) + _" department is in " + dr.Item("loc"))End Whileconn.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();