標籤:
和OCI, PRO*C, JDBC串連Timesten一樣,.NET串連TimesTen也非常簡單。只不過需要安裝的組件比較多些而已。
在運行樣本程式之前,需要在Windows上先安裝:
1. TimesTen Windows用戶端,本例中,由於TimesTen資料庫在Windows上,因此完整安裝TimesTen
2. Oracle Database or Oracle Data Access Components (ODAC),其中包含了ODP.NET 12.1
3. Miicrosoft Visual Studio,免費的社區版即可
由於.NET是基於OCI的,OCI的驅動已經包含在TimesTen用戶端安裝中了。
TimesTen文檔中與.NET相關的只有一個PDF: Oracle? Data Provider for .NET - Oracle TimesTen In-Memory Database Support User’s Guide, 僅28頁,說明絕大部分的內容都與串連Oracle相同。
在TimesTen安裝目錄下的quickstart/sample_code/odp.net下有樣本程式DemoODP.cs, 不過為了簡便,我還是提供了一個HelloWorld.cs程式,儘管我從未寫過C#代碼,感覺和Java有點像。
using System;using System.Collections.Generic;using System.Text;using System.IO;using Oracle.DataAccess.Client;using Oracle.DataAccess.Types;using System.Data;using System.Data.Common;public class HelloWorld{ private static string connStr = ""; private OracleConnection conn = null; public HelloWorld () { OracleCommand crtab = null; OracleCommand insert = null; OracleCommand select = null; OracleDataReader reader = null; try { conn = new OracleConnection (); conn.ConnectionString = connStr; conn.Open (); crtab = new OracleCommand ("create table a(a int)", conn); crtab.CommandType = CommandType.Text; crtab.ExecuteNonQuery (); crtab.Dispose (); insert = new OracleCommand ("insert into a values(1)", conn); insert.CommandType = CommandType.Text; insert.ExecuteNonQuery (); insert.Dispose (); select = new OracleCommand("select max(a) from a", conn); select.CommandType = CommandType.Text; reader = select.ExecuteReader(); if (reader.Read()) { object[] values = new object[1]; reader.GetValues(values); if (values[0] != DBNull.Value) { Console.WriteLine ("Fetched Value is: " + Convert.ToInt32(values[0])); } } reader.Close(); select.Dispose(); conn.Close (); conn.Dispose (); } catch (Exception e) { Console.WriteLine ("Test Failed"); Console.WriteLine (e.Message); Console.WriteLine (e.StackTrace); Environment.Exit (-1); } } public static void Main (string[]args) { connStr = //"Data Source=sampledb_1122; user id=appuser; password=appuser"; "Data Source=localhost/sampledb_1122:timesten_direct;user id=appuser; password=appuser"; new HelloWorld (); }}
原始碼中最關鍵的是Main中的connStr,可以是TNS形式,即:
connStr = "Data Source=sampledb_1122; user id=appuser; password=appuser";
也可以是easy connect形式:
connStr = "Data Source=localhost/sampledb_1122:timesten_direct;user id=appuser;
進入 “開發人員命令提示”
設定TNS_ADMIN,此路徑下有tnsnames.ora檔案:
set TNS_ADMIN=D:\TimesTen\tt1122_64\network\admin
我們需要的TNS服務為:
SAMPLEDB_1122 =(DESCRIPTION=(CONNECT_DATA =(SERVICE_NAME = SAMPLEDB_1122)(SERVER = timesten_direct)))
編譯:
csc /out:HelloWorld.exe /reference:D:\app\client\yyxiao\product\12.1.0\client_1\odp.net\bin\4\Oracle.DataAccess.dll HelloWorld.cs
其中D:\app\client\yyxiao\product\12.1.0\client_1\odp.net\bin\4\Oracle.DataAccess.dll為ODAC驅動檔案
運行程式:
極簡.NET串連TimesTen程式