虽然oracle,sqlserver都是关系型数据库,sql语句大部分也差不多,但是从sqlserver换到oracle还是有很多不适应的地方,本文旨在帮助广大初次接触oracle的.net程序员快速上手。
一、安装oracle 11g 服务端(可选)1.1 为什么要安装服务端?理论上讲,本机只需要安装oracle客户端即可,但是很多时候本机有一个服务端学习起来会更方便。比如:数据库的导入/导出,数据库的创建等,均需要服务端。注:oracle的server真的很占内存,如果您的爱姬内存在2G以下,建议直接跳过本步骤吧.1.2 服务端的安装文件下载,在win200 r2上安装时,会提示操作系统不满足安装要求,如下图:
1.3 如何测试服务端是否安装正确了先打开Net Manager
展开服务命名
点击左侧“红色叉”按钮下的图标
如果提示测试成功,则表示server端运行正常了。
二、安装for .Net特定的客户端 ODAC with Oracle Developer Tools for Visual Studio这是oracle官方推出的for .net的连接客户端,性能优于微软自带的System.Data.OracleClient下的东东,也是目前综合性能最好的。
三、安装pl/sql developeroracle安装完成后,自带了一个叫sql plus的查询工具,相当于sqlserver中的查询分析器,能用它练习sql,但是这个东东功能太弱,实在是难用。所以推荐大家用第三方的pl/sql developer,百度一下就能找到下载地址和注册码。这个软件第一次启动的界面如下:
但是如果输入scott/tiger@orcl,并不能正确连接
因为我们还没有配置tnsnames.ora文件(这个可以理解为web.config或machine.config,用于保存本机oracle client端的所有连接信息,只有正确配置以后,oracle client才能正确连接oracle db server)进入 %oracle_home%\Network\Admin\Sample(注:%oracle_home%指oracle客户端安装后的根目录) 找到tnsnames.ora文件,把它复制到%oracle_home%\Network\Admin\下用记事本打开,参照下面修改:# Every line that begins with # is a comment line
#
# Create Oracle net service names, or aliases, for each database server
# you need to connect to.
#
# TNSNames.ora sample entry
#
# alias =
# (DESCRIPTION =
# (ADDRESS = (PROTOCOL = TCP)(HOST = myserver.mycompany.com)(PORT = 1521))
# (CONNECT_DATA =
# (SERVER = DEDICATED)
# (SERVICE_NAME = orcl)
# )
# )
#
# You can modify the entry below for your own database.
# <data source alias> = Name to use in the connection string Data Source
# <hostname or IP> = name or IP of the database server machine
# <port> = database server machine port to use
# <database service name> = name of the database service on the serverlocal =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
解释一下:local是自己定义的名称,可以随便改,只要不重复就行了,host后面的部分是服务器ip地址,port是端口号,SERVICE_NAME是oracle server安装时的实例命名,修改完成后,保存。再次打开pl/sql,会发现database下拉框里多出了一个local,如下图:
用户名输入scott,密码输入tiger,选择local,登录,成功!
三、.net与oracle的连接这是初学者最头痛的问题,oracle有4种方式可供.net连接3.1 古老的ODBC数据源连接先打开"Microsoft ODBC管理员",如下图
在vs2010的server explorer面板中,创建一个connection
选择Change,再选择ODBC数据源
接下来的事情,大家照提示来就行了,不过我测试发现,ODBC方式在vs.net 2010/win2008 r2下,始终连接不上,
但是在控制面板的数据源里,test connection是成功的。
个中原因,也许只有微软知道,所以这种方式我是没实践成功,放弃!反正odbc这种老古董我也不喜欢。3.2 微软自带的System.Data.OracleClient同样,server explorer面板中,add 一个connection,在出来的界面中,选择change,切换成
输入用户名和密码后,就能连接成功。
特别提醒:自从oracle官方推出for .net的客户端后,微软就宣告在未来的.net版本中,将移除System.Data.OracleClient命名空间,不再提供微软版的oracle client! 详情见:http://go.microsoft.com/fwlink/?LinkID=144260除非你的项目将来不打算升级,否则不建议大家用这种方式。为了引用System.Data.OracleClient,需要添加对System.Data.OracleClient.dll的引用,默认是在C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0目录下添加引用成功后,就能用下面的代码进行查询了:
using System; using System.Data.OracleClient; namespace Sample { class Program { static void Main(string[] args) { string connString = "Data Source=local;Persist Security Info=True;User ID=scott;Password=tiger;Unicode=True"; using (OracleConnection conn = new OracleConnection(connString)) { OracleCommand cmd = new OracleCommand("select * from emp", conn); conn.Open(); OracleDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { Console.WriteLine("{0}\t{1}", dr[0].ToString(), dr[1].ToString()); } dr.Close(); } Console.ReadLine(); } } }
3.3 oledb方式
连接字符串为
Provider=MSDAORA;Data Source=local;Persist Security Info=True;User ID=scott;Password=tiger
示例代码:
using System; using System.Data.OleDb; namespace Sample { class Program { static void Main(string[] args) { string connString = "Provider=MSDAORA;Data Source=local;Persist Security Info=True;User ID=scott;PassWord=tiger"; using (OleDbConnection conn = new OleDbConnection(connString)) { OleDbCommand cmd = new OleDbCommand("select * from emp", conn); conn.Open(); OleDbDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { Console.WriteLine("{0}\t{1}", dr[0], dr[1]); } dr.Close(); } Console.ReadLine(); } } }
3.4 Oracle官方的ODP.Net
连接字符串为 DATA SOURCE=local;PERSIST SECURITY INFO=True;USER ID=SCOTT;Password=tiger要使用ODP.Net,必须先添加对Oracle.DataAccess.dll的引用,该文件位于%Oracle_Home%\11.2.0\odp.net\bin\4下示例代码如下:
using System; using Oracle.DataAccess.Client; using System.Data; using System.Data.Common; namespace Sample { class Program { static void Main(string[] args) { string connString = "DATA SOURCE=local;PERSIST SECURITY INFO=True;USER ID=SCOTT;Password=tiger"; string ProviderName = "Oracle.DataAccess.Client"; DbProviderFactory factory = DbProviderFactories.GetFactory(ProviderName); using (DbConnection conn = factory.CreateConnection()) { conn.ConnectionString = connString; conn.Open(); DbCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from emp"; cmd.CommandType = CommandType.Text; DbDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { Console.WriteLine("{0}\t{1}", dr[0], dr[1]); } dr.Close(); } Console.ReadLine(); } } }
为了测试这三种方式(ODBC不考虑)的性能,简单写了一段代码测试了一下:
using System; using Oracle.DataAccess.Client; using System.Data; using System.Data.OleDb; using System.Data.Common; using System.Diagnostics; using MSOracle = System.Data.OracleClient; namespace Sample { class Program { static void Main(string[] args) { string temp = ""; string connString = "DATA SOURCE=local;PERSIST SECURITY INFO=True;USER ID=SCOTT;Password=tiger"; string ProviderName = "Oracle.DataAccess.Client"; DbProviderFactory factory = DbProviderFactories.GetFactory(ProviderName); int max = 5000; Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < max; i++) { using (DbConnection conn = factory.CreateConnection()) { conn.ConnectionString = connString; conn.Open(); DbCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from emp"; cmd.CommandType = CommandType.Text; DbDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { //Console.WriteLine("{0}\t{1}", dr[0], dr[1]); temp = dr[0].ToString(); } dr.Close(); //Console.WriteLine("第{0}次\t----------------------------------------", i); } } sw.Stop(); Console.WriteLine("Oracle.DataAccess.Client\t{0}次耗时:{1}毫秒", max, sw.ElapsedMilliseconds); string connString2 = "Provider=MSDAORA;Data Source=local;Persist Security Info=True;User ID=scott;PassWord=tiger"; sw.Reset(); sw.Start(); for (int i = 0; i < max; i++) { using (OleDbConnection conn =new OleDbConnection(connString2)) { conn.Open(); OleDbCommand cmd = new OleDbCommand("select * from emp", conn); OleDbDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { //Console.WriteLine("{0}\t{1}", dr[0], dr[1]); temp = dr[0].ToString(); } dr.Close(); //Console.WriteLine("第{0}次\t----------------------------------------", i); } } sw.Stop(); Console.WriteLine("System.Data.OleDb\t{0}次耗时:{1}毫秒", max, sw.ElapsedMilliseconds); string connString3 = "Data Source=local;Persist Security Info=True;User ID=scott;Password=tiger;Unicode=True"; sw.Reset(); sw.Start(); for (int i = 0; i < max; i++) { using (MSOracle.OracleConnection conn = new MSOracle.OracleConnection(connString3)) { conn.Open(); MSOracle.OracleCommand cmd = new MSOracle.OracleCommand("select * from emp", conn); MSOracle.OracleDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { //Console.WriteLine("{0}\t{1}", dr[0], dr[1]); temp = dr[0].ToString(); } dr.Close(); //Console.WriteLine("第{0}次\t----------------------------------------", i); } } sw.Stop(); Console.WriteLine("System.Data.OracleClient\t{0}次耗时:{1}毫秒", max, sw.ElapsedMilliseconds); Console.ReadLine(); } } }
运行结果:
Oracle.DataAccess.Client 5000次耗时:4711毫秒
System.Data.OleDb 5000次耗时:53684毫秒
System.Data.OracleClient 5000次耗时:8436毫秒
相信大家知道如何选择了吧,果然还是Oracle官方更熟悉自己的产品。