Visual Studio 2012 C#.NET Gridview connect to Oracle DB

1. download Oracle 11g  intall them (link http://www.oracle.com/technetwork/developer-tools/visual-studio/downloads/index.html)

2.download Data Provider Oracle (ODAC) intall them (link http://www.oracle.com/technetwork/developer-tools/visual-studio/downloads/index.html)

3. Config Service Naming

4. copy sqlnet.ora and tnsnames.ora then edit them

# Every line that begins with # is a comment line
# You can modify the entry below for your own database.

# sqlnet.ora Network Configuration File

SQLNET.AUTHENTICATION_SERVICES = (NTS)

 

# 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 = 192.168.0.1)(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
#servicename จะติดตั้งกลับ oracle Net Manager จะตรงกัน
servicename=
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.1)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SID = oraclessid)
    )
  )

5.Test Create Project Visual Studio Add WebForm and Create Gridview then Config Data source select Oracle Database (Oracle ODP.NET)

 

กรณี Data Source Name ไม่ขึ้นให้เลือก ให้เข้าไปที่ C:\app\Administrator\product\11.2.0\client_1\Network\Admin\Sample (ถ้าลงแบบปกติจะเป็นที่อยู่ตามนี้) ให้ทำการ Copy Files 1. sqlnet.ora 2. tnsnames.ora เอาไปวางด้านนอก folder Admin หลังจากนั้นทำการแก้ไขทั้งสองไฟล์ดังนี้

# Every line that begins with # is a comment line
# You can modify the entry below for your own database.

# sqlnet.ora Network Configuration File

SQLNET.AUTHENTICATION_SERVICES= (NTS)

 

# 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 server

ชื่อ datasource ที่จะใช้ =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = ไอพีเครื่องที่ลง Oracle DB)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SID = ชื่อ SID ดูจากตัว Oracle Net Manager ที่ลงไว้)
    )
  )

 

 

 

6. If Unable to find the requested .Net Framework Data Provider.  It may not be installed. you must be change Web.config bellow

<system.data>
    <DbProviderFactories>

      <add name="Oracle Data Provider for .NET" invariant="Oracle.DataAccess.Client" description="Oracle Data Provider for .NET" type="Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess, Version=2.111.6.20, Culture=neutral, PublicKeyToken=89b483f429c47342" />
    </DbProviderFactories>
</system.data>

 

 

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.