当前位置:   article > 正文

C# 创建Oceanbase ODBC数据源 DSN_oceanbase c#

oceanbase c#
string coon_string = @“Driver={OceanBase ODBC 2.0 Driver};Dsn=OceanbaseC#;uid=YMS@obpoc#obdemo;password=xxx”
  • 1

上面无需在ODBC管理器界面创建数据源(因为已经指明了Driver和连接,但是驱动还是要安装的),下面的可以动态创建DSN

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";
			//如果是用户DSN ,用Registry.CurrentUser.CreateSubKey
            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();





        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/705899
推荐阅读
相关标签
  

闽ICP备14008679号