我目前有一个Windows窗体应用。我正忙着在ODBC连接的DSN中创建一个条目。我通过解析可信连接。这是我班上的代码。
Public Class Adding_ODBC_DNS
{
Private const string ODBC_INI_REG_PATH =
"Software\\ODBC\\ODBC.INI\\";
Private const string ODBCINST_INI_REG_PATH =
"Software\\ODBC\\ODBCINST.INI\\";
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("Database", database);
dsnKey.SetValue("Description", description);
dsnKey.SetValue("Driver", driverPath);
dsnKey.SetValue("LastUser", Environment.UserName);
dsnKey.SetValue("ServerName", server);
dsnKey.SetValue("Database", database);
dsnKey.SetValue("Trusted_Connection", trustedConnection ?
"Yes" : "No");
}
}
我的主要表格如下:
using System;
using System.Windows.Forms;
namespace AddingODBC_DNS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
AddingODBC_DNS NewAddingODBC_DNS = new AddingODBC_DNS();
var dsnName = "Name";
var dsnDescription = "Description";
var dsnServer = "Server";
var dsnDriverName = "DriverName";
var dsnTrustedConnection = false;
var dsnDatabase = "Database";
AddingODBC_DNS.CreateDSN(dsnName, dsnDescription, dsnServer,dsnDriverName,
dsnTrustedConnection,dsnDatabase);
}
}
}
我正在将可信连接解析为false。如果我理解正确,则意味着您必须提供用户名和密码作为用户输入。我将如何提供用户名和密码,例如我是否必须插入文本框等?
非常感谢有关此问题的帮助或建议。