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();





        }
    }
}
相关推荐
小六子成长记2 分钟前
【C++】:多态的实现
开发语言·c++
flysh052 分钟前
关于C#编程中的async / await关键字
开发语言·c#
好好学操作系统3 分钟前
flash_attn ImportError undefined symbol:
开发语言·python
CCPC不拿奖不改名3 分钟前
面向对象编程:继承与多态+面试习题
开发语言·数据结构·python·学习·面试·职场和发展
꧁Q༒ོγ꧂6 分钟前
算法详解(一)--算法系列开篇:什么是算法?
开发语言·c++·算法
m5655bj9 分钟前
使用 C# 将 RTF 文档转换为 PDF 格式
pdf·c#
星火开发设计16 分钟前
Python数元组完全指南:从基础到实战
开发语言·windows·python·学习·知识·tuple
wuk99817 分钟前
栅格障碍物地图生成与机器人路径规划MATLAB程序
开发语言·matlab
郝学胜-神的一滴18 分钟前
深入浅出:Python类变量与实例变量的核心差异与应用实践
开发语言·python·程序人生
froginwe1120 分钟前
C 未定义行为
开发语言