一、安装oracle 11g 服务端(可选)
二、安装for .Net特定的客户端 ODAC with Oracle Developer Tools for Visual Studio
#
# 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 server
(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安装时的实例命名,修改完成后,保存。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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();
}
}
}
|
Provider=MSDAORA;Data Source=local;Persist Security Info=True;User ID=scott;Password=tiger
示例代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
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();
}
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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();
}
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
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官方更熟悉自己的产品。
出处: http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。



8034



被折叠的 条评论
为什么被折叠?



