zoukankan      html  css  js  c++  java
  • C# 操作ODBC数据源

    新建

    LocalMachine对应系统
    CurrentUser对应使用者
     string strText= File.ReadAllText(@"C:WindowsEado.ini");            
                Microsoft.Win32.Registry.CurrentUser.OpenSubKey("software").OpenSubKey("ODBC").
                    OpenSubKey("ODBC.INI", true).DeleteSubKey(DsnName.Trim());
                //在HKEY_LOCAL_MACHINE/Software/ODBC/ODBC.INI中创建一个子键和相应的值
                Microsoft.Win32.RegistryKey regkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("software").
                    OpenSubKey("ODBC").OpenSubKey("ODBC.INI", true).CreateSubKey(DsnName.Trim());
                regkey.SetValue("DataBase", "efoxsfcmu3seagate");
                regkey.SetValue("Driver", @"C:WINDOWSSystem32SQLSRV32.dll");
                regkey.SetValue("Server", ServerName.Trim());
                regkey.SetValue("LastUser", "essnlxddl");
                regkey.SetValue("Trusted_Connection", "No");//如果是账密登录,
                //在HKEY_LOCAL_MACHINE/Software/ODBC/ODBC.INI/ODBC Data Sources中增加一个字符串键值
                regkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("software").OpenSubKey("ODBC")
                    .OpenSubKey("ODBC.INI", true).OpenSubKey("ODBC Data Sources", true);
                regkey.SetValue(DsnName.Trim(), "SQL Server");
                return true;

    localMachine

    直接操作注册表,需要引用Microsoft.Win32命名空间
    ///
     <summary>/// 创建SQL数据源 /// </summary> /// <param name="dns">数据源名称</param> /// <param name="server">服务器</param> /// <param name="database">数据库</param> /// <returns></returns> private bool CreateSqlODBC(string dsn, string server,string database) {    try    {       RegistryKey regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("ODBC").OpenSubKey("ODBC.INI"true).CreateSubKey(dsn);       regKey.SetValue("Driver"@"C:WINDOWSsystem32SQLSRV32.dll");       regKey.SetValue("Server", server);       regKey.SetValue("Database", database);       regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("ODBC").OpenSubKey("ODBC.INI"true).OpenSubKey("ODBC Data Sources"true);       regKey.SetValue(dns, "SQL Server");       return true;     }      catch      {       return false;      } }

    方法2:使用P/Invoke(平台调用),需要引用System.Runtime.InteropServices命名空间,具体的函数参数MSDN有比较详细的解释
    [DllImport("ODBCCP32.DLL")] public static extern int SQLConfigDataSource(IntPtr hwndParent, int fRequest, string lpszDriver, string lpszAttributes);  private int CreateSqlODBC(string dsn, string description, string server, string database) {             string lpszAttributes = string.Empty;             lpszAttributes += string.Format("DSN={0}",dsn);             lpszAttributes += string.Format("DESCRIPTION={0}", description);             lpszAttributes += string.Format("SERVER={0}", server);             lpszAttributes += string.Format("DATABASE={0}", database);             return SQLConfigDataSource((IntPtr)04"SQL Server", lpszAttributes); }
  • 相关阅读:
    使用 ASP.NET Core MVC 创建 Web API(五)
    使用 ASP.NET Core MVC 创建 Web API(四)
    使用 ASP.NET Core MVC 创建 Web API(三)
    使用 ASP.NET Core MVC 创建 Web API(二)
    使用 ASP.NET Core MVC 创建 Web API(一)
    学习ASP.NET Core Razor 编程系列十九——分页
    学习ASP.NET Core Razor 编程系列十八——并发解决方案
    一个屌丝程序猿的人生(九十八)
    一个屌丝程序猿的人生(九十七)
    一个屌丝程序猿的人生(九十五)
  • 原文地址:https://www.cnblogs.com/wangjp-1233/p/13600201.html
Copyright © 2011-2022 走看看