Thursday, November 29, 2012

使用aspx與VB連結oracle資料庫 connect Oracle DB with Aspx and VB

4 Steps~
1. Add oracle DB connect config in Web.config file.
2. Import System.Data.OracleClient in VB file. (exp: myjob.vb)
3. Define Connectionstring in VB file.
4. Set Getdata  Function for connect DB
5. Get data

Find the web.config file in the root Directory of aspx, and add the DB connect config.

<add name="this_is_the_connectionstring_name" connectionString="Data Source=database_name;Persist Security Info=True; User ID=db_user_id;Password=db_password" providerName="System.Data.OracleClient" />

Imports System.Data.OracleClient

Private myconnectionString as String= ConfigurationManager.ConnectionStrings("this_is_the_connection_name").ConnectionString

Function GetTableData(ByVal queryString as String) as System.Data.Dataset
  Dim ds as New System.Data.Dataset()
     Dim connection as New OracleConnection(myconnectionString)
     Dim adapter as New OracleDataAdapter(queryString,connection)
  Catch ex as Exception
  End Try
  Return ds
End Function

Dim sql as String="select * from test_table "
Dim test_dataset as System.Data.Dataset=GetTableData(sql)

Now, You can use the test_dataset to do something~

No comments:

Post a Comment