oracle學習筆記1--開張! .net連接oracle

項目壓的緊,啥都要學,這不就開始搞oracle了

之前曾經看過一本oracle10g的書,應該是對oracle10g有了一個比較充分的瞭解,也上手操作過oracle,什麼建表空間啊,亂七八糟也會,不過讓我用.net連接oracle10g卻發現困難重重。

先講講我開始犯的錯誤
我用的是vs2003(沒安裝任何oracle的軟件,包括客戶端,這直接導致了我走入了一個誤區),隨便建了一個頁面,寫了一段很簡單的code,的就是測試能不能連上。使用了System.Data.OracleClinet這個命名空間,無奈事實是殘酷的,系統報錯了,System.DllNotFoundException: 無法加載 DLL (oci.dll)。
我原來一直以爲oracle10g是不需要客戶端軟件的,因此之前都沒裝,經過google和baidu,發現我這種想法是及其錯誤的,需要客戶端軟件。
經過一番努力,客戶端到手了。
(ps:oracle提供了各種類型的客戶端軟件,包括號稱輕量級的免安裝的,詳見:
http://blog.csdn.net/spirit0416/archive/2006/10/12/1332297.aspx)

之後要步入正題了(下面都會針對ODAC、ODT等來講,而不會針對輕量級的客戶端)
1.oracle10g的網絡概述
在net manager中可以設置監聽程序,命名方法,本地net服務和目錄使用。最後一個目錄使用沒用過,暫且不說。
監聽程序不需要配置監聽程序,服務器端需要配置命名方法不是很重要,配置的話可以按照oracle的步驟一步一步來,配置完成後生成文件sqlnet.ora本地net服務配置是客戶端的核心,簡單來說,.net通過本地net服務名來連接數據庫,裏面需要注意的是主機名最好使用ip

2.在vs2003中測試
我裝的是ODT,其中包括了Oracle Data Provider for .NET,打開vs後,就會發現多了些東西
在.net中可以using Oracle.DataAccess.Client
這裏面提供了和System.Data.OracleClinet接口一樣的數據庫連接類,如OracleCommand等。
(ps:針對.NET Framework Data Provider for Oracle和Oracle Developer Tools for Visual Studio .NET我在網上見過兩種說法:
(a)是framework1中自帶的類只適用於oracle8以下的版本,這個也沒有加以論證過,不過我覺得有可能,framework1.1能否支持oracle9i,framework2.0能不能支持oracle10g就不清楚了
(b)有人說.NET Framework Data Provider for Oracle中bug比較多,不是很穩定,Oracle Developer Tools for Visual Studio .NET中較爲穩定,具體沒有試過)

語句是肯定沒問題的,很簡單。同時在vs的服務器資源管理器中可以連接到oracle10g上,但是在編譯之後確仍然出現了“無法加載 DLL (oci.dll)”錯誤,這就令我很困惑,爲什麼轉了一圈又回到了這個錯誤之上?

網上的解決方法大體相似一個比較全解決方法如下
oracle的一個bug在9i種就存在這個問題,就是其它用戶沒有權限訪問oracle軟件。

解決辦法:
1.  Log on to Windows as a user with Administrator privileges.

   2.  Launch Windows Explorer from the Start Menu and and navigate to the
       ORACLE_HOME folder.  This is typically the "Ora92" folder under the
       "Oracle" folder (i.e. D:/Oracle/Ora92) in Oracle9iR2 or "Client_1"
       folder under the "Oracle/product/10.1.0" folder (i.e. D:/Oracle/product
       /10.1.0/Client_1) in Oracle10gR1.

   3.  Right-click on the ORACLE_HOME folder and choose the "Properties" option
       from the drop down list.  A "Properties" window should appear.

   4.  Click on the "Security" tab of the "Properties" window.

       NOTE: If you are running Windows XP and do not have a "Security" tab
             please do the following:
 
             (a) Open "Folder Options" in your Control Panel.
             (b) Choose the "View" tab.
             (c) Under "Advanced Settings" uncheck the option "Use simple file
                 sharing (Recommended)".

             The "Security" tab should now be available.

   5.  Click on "Authenticated Users" item in the "Name" list (on Windows XP
       the "Name" list is called "Group or user names").

   6.  Uncheck the "Read and Execute" box in the "Permissions" list under the
       "Allow" column (on Windows XP the "Permissions" list is called
       "Permissions for Authenticated Users").

   7.  Re-check the "Read and Execute" box under the "Allow" column (this is
       the box you just unchecked).

   8.  Click the "Advanced" button and in the "Permission Entries" list make
       sure you see the "Authenticated Users" listed there with:

           Permission = Read & Execute
             Apply To = This folder, subfolders and files

       If this is NOT the case, edit that line and make sure the "Apply onto"
       drop-down box is set to "This folder, subfolders and files".  This
       should already be set properly but it is important that you verify this.

   9.  Click the "Ok" button until you close out all of the security properties
       windows.  The cursor may present the hour glass for a few seconds as it
       applies the permissions you just changed to all subfolders and files.

  10.  Reboot your computer to assure that these changes have taken effect.

  11.  Re-execute the application and it should now work.

同時,也可以考慮提高aspnet這個用戶在系統中的權限,比如提高到administrators

最後還要注意一個非常重要的問題,就是裝了客戶端之後一定要重啓!!
我的客戶端是裝在fat32上的,裝在ntfs上才需要前面的步驟這麼做。

知識都是在不斷的摸索中學習的,如何連接oracle10g折騰了我整整一天,但是在痛苦的掙扎中我也對oracle有了更深刻的認識,這是在書上無法學習到的。

問題:
在使用的過程中遇到了一個問題,希望能夠解決
oracle10g提供了一個isqlplus的功能,我覺得沒sqlplus好用,就下了一個instantclient-sqlplus-win32-10.1.0.5-

20060419.zip
裝上了,也能夠進sqlplus
服務器是192.168.0.3,數據庫是eastern

192.168.0.101上netname我也配置了
-----------------------------------------------
TEST =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.3)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = eastern)
    )
  )
-----------------------------------------------
在net manager進行測試也能夠連接的上
我使用tnsping test
-------------------------------------------
TNS Ping Utility for 32-bit Windows: Version 10.1.0.2.0 - Production on 12-10月-
2006 11:52:08

Copyright (c) 1997, 2003, Oracle.  All rights reserved.

已使用的參數文件:


已使用 TNSNAMES 適配器來解析別名
Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)
(HOST = 192.168.0.3)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = eastern)))
OK (30 毫秒)
-------------------------------------------
但是在sqlplus上寫sqlplus scott/tiger@test
的時候會出現ora12154:tns:could not resolve the connect identifier specified

不清楚是什麼原因,csdn有幾個熱心人提出了他們的看法,不過都不見效,具體見
http://community.csdn.net/Expert/TopicView1.asp?id=5077252

在搞sqlplus過程中,對sqlplus和tnsping有了進一步的認識
詳細信息見轉貼
http://blog.csdn.net/spirit0416/archive/2006/10/12/1332285.aspx
http://blog.csdn.net/spirit0416/archive/2006/10/12/1332296.aspx 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章