1. 程式人生 > >C艹連結oracle資料庫

C艹連結oracle資料庫

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Oracle.ManagedDataAccess.Client;
using Oracle.ManagedDataAccess.Types;
namespace oraclelink
{
    class Program
    {
        static void Main(string[] args)
        {
            ///需求:連線資料庫,查詢表單,並列印到控制檯視窗上
///1.連線資料庫using ///2.宣告一個連線物件 string connString = "User ID=smart2;Password=smart2;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.10.99)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = orcl)))"; ///建立連線 OracleConnection conn =
new OracleConnection(connString); try { conn.Open(); } catch (Exception oe) { Console.WriteLine(oe.Message); } ///開啟連線 ///建立要執行的sql語句 Console.WriteLine
("請輸入序號"); string stu_nu = Console.ReadLine(); // string sqlStr = "SELECT * FROM SMART_PERSONNEL WHERE SMT_PERSONNELID='"+stu_nu +"'"; ///建立用於執行SQL語句的物件 OracleCommand comm = conn.CreateCommand();///執行命令 comm.CommandText = "SELECT * FROM SMART_PERSONNEL WHERE SMT_PERSONNELID='" + stu_nu + "'";///執行命令語句 //執行comm物件,並接受到的SQL結果 OracleDataReader odr = comm.ExecuteReader();///其實是一個結果的集合 ///讀取資料 while (odr.Read())//Read往下讀; { Console.WriteLine("姓名:"+odr["SMT_NAME"].ToString());///給定序列號的情況下,獲取指定列的以本機格式表示的值 Console.WriteLine("性別:" + odr["SMT_SEX"].ToString()); Console.WriteLine("工資號:" + odr["SMT_SALARYNO"].ToString()); Console.WriteLine("身份證號:" + odr["SMT_IDNO"].ToString()); } Console.ReadLine(); } } }