C#/C++ 通過ODBC連接OceanBase Oracle租戶的詳細(xì)過程
概述
近期我們項(xiàng)目正處于將Oracle數(shù)據(jù)庫遷移到OceanBase Oracle租戶模式的階段??紤]到我們項(xiàng)目采用了C++和C#混合開發(fā),并且使用了多種技術(shù),因此存在多種數(shù)據(jù)庫連接方式。然而,針對C#連接OceanBase的案例相對較少,因此我特意記錄下這一過程。
開放數(shù)據(jù)庫互連(ODBC)是微軟公司開放服務(wù)結(jié)構(gòu)(WOSA,Windows Open Services Architecture)中有關(guān)數(shù)據(jù)庫的一個(gè)組成部分,基本思想是為用戶提供簡單、標(biāo)準(zhǔn)、透明的數(shù)據(jù)庫連接的公共編程接口,開發(fā)廠商根據(jù) ODBC 的標(biāo)準(zhǔn)去實(shí)現(xiàn)底層的驅(qū)動程序,這個(gè)驅(qū)動對用戶是透明的,并允許根據(jù)不同的 DBMS 采用不同的技術(shù)加以優(yōu)化實(shí)現(xiàn)。
驅(qū)動下載
下載地址:https://www.oceanbase.com/softwarecenter-cloud
根據(jù)平臺類型選擇驅(qū)動。
驅(qū)動配置
打開 ODBC 數(shù)據(jù)源管理員:在 Windows 中,按下 Windows鍵 + R 打開“運(yùn)行”對話框,然后輸入 odbcad32 并按下 Enter 鍵,這將打開 ODBC 數(shù)據(jù)源管理員。
選擇系統(tǒng)的或用戶的數(shù)據(jù)源:在 ODBC 數(shù)據(jù)源管理員中,有兩個(gè)選項(xiàng)卡:用戶 DSN 和 系統(tǒng) DSN。選擇一個(gè)適合你的選項(xiàng),通常建議選擇 系統(tǒng) DSN,因?yàn)樗鼘λ杏脩舳伎捎谩?/p>
添加一個(gè)新的數(shù)據(jù)源:點(diǎn)擊 添加 按鈕,然后在彈出的對話框中選擇 OceanBase ODBC 2.0 Driver 數(shù)據(jù)庫。
配置數(shù)據(jù)源連接信息:在配置 OceanBase 數(shù)據(jù)庫數(shù)據(jù)源時(shí),你需要提供以下信息:
數(shù)據(jù)源名稱(DSN):為你的數(shù)據(jù)源指定一個(gè)唯一的名稱。這里命名為OceanBase ODBC
描述:提供有關(guān)此數(shù)據(jù)源的描述,以便識別它。
TNS 服務(wù)名稱:輸入你要連接的 Oracle 服務(wù)的 TNS 服務(wù)名稱。IP: XX.XX.XX.XX
用戶名和密碼:提供用于連接到 Oracle 數(shù)據(jù)庫的用戶名和密碼。
UserName: 用戶名@租戶名#集群
測試連接:在完成配置后,你可以點(diǎn)擊 測試連接 按鈕來驗(yàn)證是否能夠成功連接到 OceanBase Oracle 數(shù)據(jù)庫。
注意:不通過DSN也可以連接
C++ 代碼案例
#include <stdio.h> #include <assert.h> #include <windows.h> #include <sql.h> #include <sqlext.h> #include <iostream> void print_error(SQLSMALLINT HandleType, SQLHANDLE Handle) { SQLWCHAR SQLState[6]; SQLINTEGER NativeError; SQLWCHAR SQLMessage[SQL_MAX_MESSAGE_LENGTH] = { 0 }; SQLSMALLINT TextLengthPtr; SQLGetDiagRec(HandleType, Handle, 1, SQLState, &NativeError, SQLMessage, SQL_MAX_MESSAGE_LENGTH, &TextLengthPtr); std::wcerr << L"[" << SQLState << L"] (" << NativeError << L") " << SQLMessage << std::endl; } int main() { // Allocate environment handle SQLHANDLE henv; SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv); SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0); // Allocate connection handle SQLHANDLE hdbc; SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc); // Connection string //使用DSN SQLWCHAR* connectionString = (SQLWCHAR*)L"DSN=OceanBase ODBC;UID=用戶名@住戶名#集群;PWD=密碼"; //不使用DSN //SQLWCHAR* connectionString = (SQLWCHAR*)L"Driver={OceanBase ODBC 2.0 Driver};Server=xx.xx.xx.xx;Port=2883;Database=用戶名;User=用戶名@住戶名#集群;Password=密碼;Option=3;"; // Connect to the database SQLRETURN retcode = SQLDriverConnect(hdbc, NULL, connectionString, SQL_NTS, NULL, 0, NULL, SQL_DRIVER_NOPROMPT); if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) { std::cerr << "Error connecting to database:" << std::endl; print_error(SQL_HANDLE_DBC, hdbc); return 1; } std::cout << "Connected to database successfully!" << std::endl; // Allocate statement handle SQLHANDLE hstmt; SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt); // Execute a query SQLWCHAR* query = (SQLWCHAR*)L"SELECT * FROM TIERS WHERE ROWNUM <= 1;"; retcode = SQLExecDirect(hstmt, query, SQL_NTS); if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) { std::cerr << "Error executing query:" << std::endl; print_error(SQL_HANDLE_STMT, hstmt); SQLFreeHandle(SQL_HANDLE_STMT, hstmt); SQLDisconnect(hdbc); SQLFreeHandle(SQL_HANDLE_DBC, hdbc); SQLFreeHandle(SQL_HANDLE_ENV, henv); return 1; } // Fetch and print results SQLCHAR name[256]; SQLLEN nameLen; while (SQLFetch(hstmt) == SQL_SUCCESS) { SQLGetData(hstmt, 1, SQL_C_CHAR, name, sizeof(name), &nameLen); std::cout << "Name: " << name << std::endl; } // Cleanup SQLFreeHandle(SQL_HANDLE_STMT, hstmt); SQLDisconnect(hdbc); SQLFreeHandle(SQL_HANDLE_DBC, hdbc); SQLFreeHandle(SQL_HANDLE_ENV, henv); return 0; }
C# 代碼案例
using System; using System.Data.Odbc; namespace ConsoleApp { internal class Program { static void Main(string[] args) { //使用DSN string connectionString = "DSN=OceanBase ODBC;Uid=用戶名@住戶名#集群;Pwd=你的密碼;"; //不使用DSN //string connectionString = "Driver={OceanBase ODBC 2.0 Driver};Server=xx.xx.xx.xx;Port=2883;Database=用戶名;User=用戶名@住戶名#集群;;Password=密碼;Option=3;"; using (OdbcConnection connection = new OdbcConnection(connectionString)) { try { connection.Open(); Console.WriteLine("Connected to Oracle database!"); // Perform database operations here string query = "SELECT *FROM TIERS WHERE ROWNUM <= 1;"; using (OdbcCommand command = new OdbcCommand(query, connection)) { using (OdbcDataReader reader = command.ExecuteReader()) { while (reader.Read()) { int id = reader.GetInt32(0); // 假設(shè)第一列是 ID string name = reader.GetString(1); // 假設(shè)第二列是 Name Console.WriteLine($"ID: {id}, Name: {name}"); } } } connection.Close(); } } } }
NHibernate示例
安裝NHibernate
使用NuGet包管理器安裝NHibernate。你可以在Visual Studio中打開NuGet包管理器控制臺,然后運(yùn)行以下命令:
Install-Package NHibernate
這將自動下載并安裝NHibernate及其所有依賴項(xiàng)到你的項(xiàng)目中。
配置NHibernate
NHibernate.cfg.xml
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.driver_class">NHibernate.Driver.OdbcDriver</property> <!--<property name="connection.connection_string">DSN=OceanBase ODBC;Database=用戶名;Uid=用戶名@住戶名#集群;Pwd=密碼;</property>--> <property name="connection.connection_string">Driver={OceanBase ODBC 2.0 Driver};Server=xx.xx.xx.xx;Port=2883;Database=用戶名;Uid=用戶名@住戶名#集群;Pwd=密碼;</property> <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property> </session-factory> </hibernate-configuration>
配置映射類
public class TestEntity { public virtual int Ident { get; set; } public virtual string Name { get; set; } }
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> <class name="ConsoleApp2.TestEntity, ConsoleApp2" table="TESTENTITY"> <id name="Ident" column="IDENT" /> <property name="Name" column="NAME" /> <!-- 其他屬性映射... --> </class> </hibernate-mapping>
查詢案例
using NHibernate.Cfg; using System; using System.IO; class Program { static void Main(string[] args) { string baseDirectory = AppDomain.CurrentDomain.BaseDirectory; var cfg = new Configuration().Configure(Path.Combine(baseDirectory, "NHibernate.cfg.xml")); cfg.AddFile(Path.Combine(baseDirectory, "TestEntity.xml")); var sessionFactory = cfg.BuildSessionFactory(); using (var session = sessionFactory.OpenSession()) { var query = session.CreateQuery("FROM TestEntity") .SetMaxResults(10); var results = query.List(); } } }
到此這篇關(guān)于C#/C++ 通過ODBC連接OceanBase Oracle租戶的文章就介紹到這了,更多相關(guān)C++ 連接OceanBase Oracle租戶內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入分析緩存依賴中cachedependency對象及周邊小講
本篇文章是對緩存依賴中cachedependency對象進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06C#控制臺程序?qū)崿F(xiàn)開啟、關(guān)閉SQLServer服務(wù)的代碼分享
這篇文章主要介紹了C#控制臺程序?qū)崿F(xiàn)開啟、關(guān)閉SQLServer服務(wù)的代碼分享,需要的朋友可以參考下2014-05-05NGUI實(shí)現(xiàn)滑動翻頁效果實(shí)例代碼
本文通過一段實(shí)例代碼給大家介紹NGUI實(shí)現(xiàn)滑動翻頁效果,代碼簡單易懂,對ngui 滑動翻頁相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-04-04C#同步網(wǎng)絡(luò)時(shí)間的方法實(shí)例詳解
這篇文章主要介紹了C#同步網(wǎng)絡(luò)時(shí)間的方法,以實(shí)例形式較為詳細(xì)的分析了C#獲取網(wǎng)絡(luò)時(shí)間與同步本機(jī)系統(tǒng)時(shí)間的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05