C#連線Oracle資料庫(直接引用dll使用),查詢資料
阿新 • • 發佈:2018-12-26
首先,下載Oracle.ManagedDataAccess.dll將其放到自己的專案中,然後引用。程式碼如下:
static void Main(string[] args)
{
string connectionString;
connectionString = "User Id=system;Password=*填寫自己的密碼*;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1) (PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=myoracle)))";
string queryString = "select * from student";
OracleConnection myConnection = new OracleConnection(connectionString);
myConnection.Open();
OracleCommand command = new OracleCommand(queryString, myConnection);
OracleDataReader myDataReader = command.ExecuteReader();
int i = 0;
while (myDataReader.Read())//讀取資料,如果返回為false的話,就說明到記錄集的尾部了
{
string s= myDataReader.GetOracleString(0).ToString();
Console.WriteLine(s);
}
myDataReader.Close();
myConnection.Close();
Console.WriteLine("connect success");
Console.ReadLine();
}