如何通过ODBC连接到Oracle

时间:2019-04-02 08:36:11

标签: sql-server oracle odbc

我必须从Microsoft SQL Server Management Studio连接到Oracle数据库。 客户端说我可以通过ODBC访问。 我知道ip,端口,用户名,密码和SID。 我应该添加链接服务器吗?还是只能在查询中执行? 我从未在其他数据库之前使用过,所以我不知道如何连接。

2 个答案:

答案 0 :(得分:0)

            MSSQL to Oracle


1.    Install Oracle binaries in windows machine (select software only option while installation. Do not select database)


2.    In windows machine
a.    Check if the listener exists
b.    If listener exists , use that listener (here listener is 1521 , use the listener in given listener)
c.    If not , open Net Configuration Assistant and follow steps to create listener.
d.    Check the status of listener. Open command prompt and type lsnrctl status. If the listener is not active type lsnctrl start . the listener should start.
e.    Open file listener.ora (E:\Oracle_file\product\11.2.0\dbhome_1\NETWORK\ADMIN)
f.    In listener.ora : write following contents:
# listener.ora Network Configuration File: E:\Oracle_file\product\11.2.0\dbhome_1\network\admin\listener.ora
# Generated by Oracle configuration tools.
LISTENER=  (DESCRIPTION_LIST =    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = ip)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))  )  )
SID_LIST_LISTENER =  (SID_LIST =    (SID_DESC =      (SID_NAME = dg4odbc)      (ORACLE_HOME = E:\Oracle_file\product\11.2.0\dbhome_1)
      (PROGRAM = dg4odbc)      (ENVS = "EXTPROC_DLLS=ONLY:E:\Oracle_file\product\11.2.0\dbhome_1\bin\oraclr11.dll")    )  )
ADR_BASE_MSSQL = E:\Oracle_file
g.    Edit the locations as per your installation folder
h.    Save the file listener.ora
i.    Open tnsnames.ora (E:\Oracle_file\product\11.2.0\dbhome_1\NETWORK\ADMIN)
j.    Write following content in tnsnames.ora
# tnsnames.ora Network Configuration File: E:\Oracle_file\product\11.2.0\dbhome_1\network\admin\tnsnames.ora
# Generated by Oracle configuration tools.

ORACLE =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = oracle)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = oracle)
    )
  )

MSSQL =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = tcp)(HOST = ip)(PORT = 1521))
    (CONNECT_DATA =
      (SID = dg4odbc)
    )
    (HS = OK)
  )

ORACLR_CONNECTION_DATA =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
    (CONNECT_DATA =
      (SID = CLRExtProc)
      (PRESENTATION = RO)
    )
  )

k.    Open administrative tools
l.    Open ODBC Data Sources (64-bit)
m.    In System DSN , 
i.    add a data source in sql native client
ii.    write the name as the listener name (here LISTENER)
iii.    point the server to the sql server where you want to access the location
iv.    In connection point , write the username and password , ie connect in sql server authentication mode
v.    Test the connection 
vi.    Close the wizard
n.    On Windows machine where we installed SQL Server navigate to
ORACLE_HOME\hs\admin and edit initdg4odbc.ora and save as initdg4dg4odbc.ora and both the files should have the following entries:

Location: ORACLE_HOME\HS\ADMIN
FileName: initdg4odbc.ora and initdg4dg4odbc.ora

Insert The Following
# This is a sample agent init file that contains the HS parameters that are
# needed for the Database Gateway for ODBC
#
# HS init parameters
#

HS_FDS_CONNECT_INFO = <Listener_Name>

HS_FDS_TRACE_LEVEL = off

#
# Environment variables required for the non-Oracle system
#
#set <envvar>=<value>

Where,
<Listener_Name> should be as per entered in the file listener.ora


3.    In linux machine
a.    Locate tnsnames.ora 
(/u01/app/oracle/product/11.2.0/db_1/network/admin/) 
b.    Add following in listener.ora
SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (GLOBAL_DBNAME = kvlali)
      (ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1)
      (SID_NAME = kvlali)    )  )
LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = kvlali.d2hs.com)(PORT = 1522))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
    )  )ADR_BASE_LISTENER = /u01/app/oracleSave the content
c.    Save the file
d.    Start the listener by : lsnrctl start
e.    If listener is started successfully , the connection has been done

4.    Now go to sql server management studio
a.    Connect to the instance from where you want to create linked server
b.    Create a linked server as follows:
•    Connect to the instance to which the listener connection was created
•    Go to server objects->linked servers->providers
•    Right click on providers
•    Enable allow in process
•    Go to server objects->linked servers
•    Right click on linked server and click on ‘New Linked Server’
•    ‘New Linked Server wizard’ is opened
Linked server: name of linked server via which you like to access the remote oracle server
Server type:Other data source
Provider: Oracle Provider for Ole DB
Product name:Listener name
Data Source: Listener name
In sercurity tab: add login users
Select be made using the security contest: enter remote username and password(of oracle connection)
In server options: set Data Access, RPC,RPC out, Use Remote Collation and Enable Promotion of Distributed Transaction to ‘True’
        OR
Execute following query by changing the required parameters
EXEC master.dbo.sp_addlinkedserver @server = N'KVLALI', @srvproduct=N'kvlali', @provider=N'OraOLEDB.Oracle', @datasrc=N'kvlali'
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'KVLALI',@useself=N'False',@locallogin=NULL,@rmtuser=N'utility',@rmtpassword='########'
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'KVLALI',@useself=N'False',@locallogin=N'D2HS\Administrator',@rmtuser=N'utility',@rmtpassword='########'
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'KVLALI',@useself=N'False',@locallogin=N'D2HS\i82355',@rmtuser=N'utility',@rmtpassword='########'
EXEC master.dbo.sp_addlinkedsrvlogin    @rmtsrvname=N'KVLALI',@useself=N'False',@locallogin=N'sa',@rmtuser=N'utility',@rmtpassword='########'

EXEC master.dbo.sp_serveroption @server=N'KVLALI', @optname=N'collation compatible', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'KVLALI', @optname=N'data access', @optvalue=N'true'
EXEC master.dbo.sp_serveroption @server=N'KVLALI', @optname=N'dist', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'KVLALI', @optname=N'pub', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'KVLALI', @optname=N'rpc', @optvalue=N'true'
EXEC master.dbo.sp_serveroption @server=N'KVLALI', @optname=N'rpc out', @optvalue=N'true'
EXEC master.dbo.sp_serveroption @server=N'KVLALI', @optname=N'sub', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'KVLALI', @optname=N'connect timeout', @optvalue=N'0'
EXEC master.dbo.sp_serveroption @server=N'KVLALI', @optname=N'collation name', @optvalue=null
EXEC master.dbo.sp_serveroption @server=N'KVLALI', @optname=N'lazy schema validation', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'KVLALI', @optname=N'query timeout', @optvalue=N'0'
EXEC master.dbo.sp_serveroption @server=N'KVLALI', @optname=N'use remote collation', @optvalue=N'true'
EXEC master.dbo.sp_serveroption @server=N'KVLALI', @optname=N'remote proc transaction promotion', @optvalue=N'true'



Where: @server, @rmtsrvname = server where you want to create link
    @locallogin=the login user of sql server
    @rmtuser=username of oracle user login
    @rmtpassword=password of given user login

c.    In providers for linked server , right click in ‘OraOLEDB.Oracle’
And enable ‘Allow inprocess’
d.    Test the linked server by right click on test connection
e.    Execute query as:
select * from openquery (kvlali,'select * from dbua_audit_report')


Enjoy!!

答案 1 :(得分:0)

我解决了问题:) 只需从Oracle下载该软件包,其中包含“适用于ssms x32或x64的Oracle Provider for OLE DB xxxxxx” 以管理员身份打开cmd,然后输入regsvr32 / i。 在提供程序中将显示oraOLEDB.Oracle并通过选择此提供程序来创建链接服务器,在数据源中:// /可能以安全性登录名和密码