C# 创建Oceanbase ODBC数据源 DSN

需要管理员权限打开VS,因为只有管理员权限可以修改注册表

csharp 复制代码
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Process install = new Process();
            //ProcessStartInfo processStart = new ProcessStartInfo();
            processStart.WorkingDirectory = @"D:\ESPT_Release\";
            //processStart.Arguments = @"/i D:\ESPT_Release\ob-connector-odbc-2.0.8.2-win32.msi  /q";
            //processStart.FileName = "msiexec";
            processStart.Verb = "runas";

            //install.StartInfo = processStart;
            //install.Start();
            //install.WaitForExit();

            string ODBC_PATH = "SOFTWARE\\ODBC\\ODBC.INI\\";
            string driverName = "OceanBase ODBC 2.0 Driver";
            string dsnName = "OceanbaseC#";
            string database = "aaa";
            string description = "This DSN was created from code!";
            string server = "10.00.0.1";
            string port = "2883";
            bool trustedConnection = true;

            // Lookup driver path from driver name //C:\\Program Files (x86)\\OceanBase\\OceanBase ODBC Driver\\obodbc.dll
            string driverPath = "OceanBase ODBC 2.0 Driver";

            var datasourcesKey = Registry.LocalMachine.CreateSubKey(ODBC_PATH + "ODBC Data Sources");
            if (datasourcesKey == null)
            {
                throw new Exception("ODBC Registry key does not exist");
            }
            datasourcesKey.SetValue(dsnName, driverName);
            // Create new key in odbc.ini with dsn name and add values        
            var dsnKey = Registry.LocalMachine.CreateSubKey(ODBC_PATH + dsnName);
            if (dsnKey == null)
            {
                throw new Exception("ODBC Registry key for DSN was not created");
            }

            dsnKey.SetValue("Database", database);
            dsnKey.SetValue("Description", description);
            dsnKey.SetValue("Driver", driverPath);
            dsnKey.SetValue("TCPIP", 1);
            //dsnKey.SetValue("LastUser", "YMS");
            dsnKey.SetValue("Server", server);
            dsnKey.SetValue("Port", port);
            dsnKey.SetValue("Database", database);
            //dsnKey.SetValue("user name", "YMS@obpoc#obdemo");
            dsnKey.SetValue("UID", "aaa@obpoc#obdemo");
            //dsnKey.SetValue("user name", "YMS@obpoc#obdemo");
            dsnKey.SetValue("password", "bbb");
            dsnKey.SetValue("QUERYTIMEOUT", "10800");
            dsnKey.SetValue("READTIMEOUT", "3600");
            dsnKey.SetValue("WRITETIMEOUT", "3600");
            dsnKey.SetValue("Trusted_Connection", trustedConnection ? "Yes" : "No");
            //DataTable dt = new DataTable();
            //string dns = "Dsn=OceanbaseC#;uid=YMS@obpoc#obdemo";
            //string sql = @"SELECT * from ESPT_ACE_BIN_GROUP where rownum < 10";
            //OdbcConnection conn = new OdbcConnection(dns);
            OdbcCommand cmd = new OdbcCommand(sql, conn);
            //conn.Open();
            //OdbcDataAdapter adapter = new OdbcDataAdapter(sql, conn);
            //adapter.Fill(dt);

            //this.dataGridView1.DataSource = dt;
            //this.dataGridView1.Show();





        }
    }
}
相关推荐
咕白m6254 分钟前
使用 C# 设置 Word 段落对齐样式
后端·c#
九死九歌5 分钟前
【Sympydantic】使用sympydantic,利用pydantic告别numpy与pytorch编程中,tensor形状带来的烦人痛点!
开发语言·pytorch·python·机器学习·numpy·pydantic
Kiri霧5 分钟前
Go切片详解
开发语言·后端·golang
武藤一雄9 分钟前
[.NET] 中 System.Collections.Generic命名空间详解
windows·微软·c#·asp.net·.net·.netcore
kingwebo'sZone10 分钟前
一次找不到“无法加载dll 对应的”,多媒体没有启用(需要安装mediaplayer)
c#
二进制coder13 分钟前
C++ 中的 Interface:概念、实现与应用详解
开发语言·c++
古城小栈13 分钟前
Go 与 Rust:系统编程语言的竞争与融合
开发语言·golang·rust
zxbmmmmmmmmm14 分钟前
Avalonia源码解读:Grid(网格控件)
c#·xaml·avalonia
随风一样自由15 分钟前
React编码时,什么时候用js文件,什么时候用jsx文件?
开发语言·javascript·react.js
by__csdn19 分钟前
Vue3 生命周期全面解析:从创建到销毁的完整指南
开发语言·前端·javascript·vue.js·typescript·前端框架·ecmascript