首页 > 解决方案 > 使用 C# windows 应用程序表单创建 DSN

问题描述

大家好,我是 C# 的初学者,所以我需要帮助。我正在制作 C# windows 应用程序表单并使用 Class 并使用 Button 来调用类,但我不知道如何通过单击按钮来调用类。帮助将不胜感激。我正在创建另一个表单,它只包含一个按钮,我要创建的 DSN 名称是“Fassets”数据库名称也是 Fassets 在此处输入图像描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace dsn
{
   public static class Class1
   {
       private const string ODBC_INI_REG_PATH = "SOFTWARE\\ODBC\\ODBC.INI\\";
       private const string ODBCINST_INI_REG_PATH = "SOFTWARE\\ODBC\\ODBCINST.INI\\";

       /// <summary>
       /// Creates a new DSN entry with the specified values. If the DSN exists, the values are updated.
       /// </summary>
       /// <param name="dsnName">Name of the DSN for use by client applications</param>
       /// <param name="description">Description of the DSN that appears in the ODBC control panel applet</param>
       /// <param name="server">Network name or IP address of database server</param>
       /// <param name="driverName">Name of the driver to use</param>
       /// <param name="trustedConnection">True to use NT authentication, false to require applications to supply username/password in the connection string</param>
       /// <param name="database">Name of the datbase to connect to</param>
       public static void CreateDSN(string dsnName, string description, string server, string driverName, bool trustedConnection, string database)
       {
           // Lookup driver path from driver name
           var driverKey = Registry.LocalMachine.CreateSubKey(ODBCINST_INI_REG_PATH + driverName);
           if (driverKey == null) throw new Exception(string.Format("ODBC Registry key for driver '{0}' does not exist", driverName));
           string driverPath = driverKey.GetValue("Driver").ToString();

           // Add value to odbc data sources
           var datasourcesKey = Registry.LocalMachine.CreateSubKey(ODBC_INI_REG_PATH + "ODBC Data Sources");
           if (datasourcesKey == null) throw new Exception("ODBC Registry key for datasources 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_INI_REG_PATH + dsnName);
           if (dsnKey == null) throw new Exception("ODBC Registry key for DSN was not created");
           dsnKey.SetValue("Fassets", database);
           dsnKey.SetValue("Description", description);
           dsnKey.SetValue("driverPath", driverPath);
           dsnKey.SetValue("LastUser", Environment.UserName);
           dsnKey.SetValue("Server", server);
           dsnKey.SetValue("Database", database);
           dsnKey.SetValue("Trusted_Connection", trustedConnection ? "Yes" : "No");
       }

       /// <summary>
       /// Removes a DSN entry
       /// </summary>
       /// <param name="dsnName">Name of the DSN to remove.</param>
       public static void RemoveDSN(string dsnName)
       {
           // Remove DSN key
           Registry.LocalMachine.DeleteSubKeyTree(ODBC_INI_REG_PATH + dsnName);

           // Remove DSN name from values list in ODBC Data Sources key
           var datasourcesKey = Registry.LocalMachine.CreateSubKey(ODBC_INI_REG_PATH + "ODBC Data Sources");
           if (datasourcesKey == null) throw new Exception("ODBC Registry key for datasources does not exist");
           datasourcesKey.DeleteValue(dsnName);
       }

       ///<summary>
       /// Checks the registry to see if a DSN exists with the specified name
       ///</summary>
       ///<param name="dsnName"></param>
       ///<returns></returns>
       public static bool DSNExists(string dsnName)
       {
           var driversKey = Registry.LocalMachine.CreateSubKey(ODBCINST_INI_REG_PATH + "ODBC Drivers");
           if (driversKey == null) throw new Exception("ODBC Registry key for drivers does not exist");

           return driversKey.GetValue(dsnName) != null;
       }

       ///<summary>
       /// Returns an array of driver names installed on the system
       ///</summary>
       ///<returns></returns>
       public static string[] GetInstalledDrivers()
       {
           var driversKey = Registry.LocalMachine.CreateSubKey(ODBCINST_INI_REG_PATH + "ODBC Drivers");
           if (driversKey == null) throw new Exception("ODBC Registry key for drivers does not exist");

           var driverNames = driversKey.GetValueNames();

           var ret = new List<string>();

           foreach (var driverName in driverNames)
           {
               if (driverName != "(Default)")
               {
                   ret.Add(driverName);
               }
           }

           return ret.ToArray();
       }
   }
}

标签: c#

解决方案


如果我正确理解了您的问题,您希望将“单击”事件绑定到按钮并从您的类中运行一个方法。只需双击设计器中的按钮,您就可以进入刚刚创建的功能。在此函数中,您可以从您的类中调用该函数。

请注意!请永远不要使用 Class 作为类的名称。这是一个保留字。https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/


推荐阅读