用C#怎么连接oracle11g-CSDN论坛
1个回答
2016-03-21
展开全部
一. 在VS2010中连接 Oracle数据库有两种方法:
第一种:微软提供的连接方法 : using System.Data.OracleClient;
第二种:Oracle自己提供的方法:using Oracle.DataAccess.Client;
连接字符串:
connectionString="Password=czh;User ID=czh;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=XXX.XXX.XXX.XXX)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=skydream)));"
1. 微软提供的连接方法 : using System.Data.OracleClient;
测试例程:
··1.在VS2010新建控制台应用程序(C#);
··2.右键、引用,在.NET中选择System.Data.OracleClient;
··3.在程序中 using System.Data.OracleClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string connectionString;
string queryString;
connectionString = "Data Source=202.200.136.125/orcl;User ID=openlab;PassWord=open123";
queryString = "SELECT * FROM T_USER";
OracleConnection myConnection = new OracleConnection(connectionString);
OracleCommand myORACCommand = myConnection.CreateCommand();
myORACCommand.CommandText = queryString;
myConnection.Open();
OracleDataReader myDataReader = myORACCommand.ExecuteReader();
myDataReader.Read();
Console.WriteLine("email: " + myDataReader["EMAIL"]);
myDataReader.Close();
myConnection.Close();
}
}
}
2.Oracle自己提供的方法:using Oracle.DataAccess.Client;
前提条件:安装oracle或者oracle client以及安装 Oracle Client 。
··1.在VS2010新建控制台应用程序(C#);
··2.右键、引用,在.NET/组件中选择Oracle.DataAccess.Client;如果找不到则选择 浏览,进入到oracleclient的安装目录寻找 Oracle.Data.Access.dll (典型目录为:E:\app\Administrator\product\11.2.0\client_1\ODP.NET\bin\2.x\Oracle.Data>Access.dll)
··3.程序中添加引用:using Oracle.DataAccess.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oracle.DataAccess.Client;
namespace testConnectionOracle
{
class Program
{
static void Main(string[] args)
{
string connectionString;
string queryString;
connectionString = "Data Source=202.200.155.123/orcl;User ID=openlab;PassWord=open123";
queryString = "SELECT * FROM T_USER";
OracleConnection myConnection = new OracleConnection(connectionString);
OracleCommand myORACCommand = myConnection.CreateCommand();
myORACCommand.CommandText = queryString;
myConnection.Open();
OracleDataReader myDataReader = myORACCommand.ExecuteReader();
myDataReader.Read();
Console.WriteLine("email: " + myDataReader["EMAIL"]);
myDataReader.Close();
myConnection.Close();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询