我正在使用具有C ++扩展存储过程的旧版应用程序。 此xsproc使用ODBC连接到数据库,这意味着它需要配置DSN。
我正在更新安装程序(使用Visual Studio 2008安装项目创建),并希望有一个可以创建ODBC DSN条目的自定义操作,但正在努力在Google上找到有用的信息。
有人可以帮忙吗?
最后,我实际上通过操作注册表解决了这个问题。我创建了一个包含功能的类,其内容包括在这里:
///<summary>
/// Class to assist with creation and removal of ODBC DSN entries
///</summary>
public static class ODBCManager
{
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("Database", database);
dsnKey.SetValue("Description", description);
dsnKey.SetValue("Driver", 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();
}
}
在chrfalch的帖子之后,下面是一些使用API??调用而不是通过注册表直接(通过使用来自pinvoke.net页的信息):
[DllImport("ODBCCP32.DLL", CharSet = CharSet.Unicode, SetLastError = true)]
static extern bool SQLConfigDataSourceW(UInt32 hwndParent, RequestFlags fRequest, string lpszDriver, string lpszAttributes);
enum RequestFlags : int
{
ODBC_ADD_DSN = 1,
ODBC_CONFIG_DSN = 2,
ODBC_REMOVE_DSN = 3,
ODBC_ADD_SYS_DSN = 4,
ODBC_CONFIG_SYS_DSN = 5,
ODBC_REMOVE_SYS_DSN = 6,
ODBC_REMOVE_DEFAULT_DSN = 7
}
bool UpdateDsnServer(string name, string server)
{
var flag = RequestFlags.ODBC_CONFIG_SYS_DSN;
string dsnNameLine ="DSN=" + name;
string serverLine ="Server=" + server;
string configString = new[] { dsnNameLine, serverLine }.Aggregate("", (str, line) => str + line +"\\0");
return SQLConfigDataSourceW(0, flag,"SQL Server", configString);
}
有一个API可以做这样的事情。使用API??还将确保您的应用程序将与Windows的较新版本保持兼容。可以在以下位置找到该API:
http://msdn.microsoft.com/zh-CN/library/ms716476(VS.85).aspx
在C#中P调用此函数可以在PInvoke.net上找到。
为Barnwell的代码+1!
短码网:DuanMa.NET
但是,我认为他的DSNExists()正在查询错误的密钥。我认为应该是这样的:
public static bool DSNExists(string dsnName)
{
var sourcesKey = Registry.LocalMachine.CreateSubKey(ODBC_INI_REG_PATH +"ODBC Data Sources");
if (sourcesKey == null) throw new Exception("ODBC Registry key for sources does not exist");
return sourcesKey.GetValue(dsnName) != null;
}
有一个有关读取ODBC信息的CodeProject页。
阅读该文章后,您将获得所需的信息,以进行反向工程以编写所需的注册表项。
从该代码;
private const string ODBC_LOC_IN_REGISTRY ="SOFTWARE\\\\ODBC\\\";
private const string ODBC_INI_LOC_IN_REGISTRY =
ODBC_LOC_IN_REGISTRY +"ODBC.INI\\\";
private const string DSN_LOC_IN_REGISTRY =
ODBC_INI_LOC_IN_REGISTRY +"ODBC Data Sources\\\";
private const string ODBCINST_INI_LOC_IN_REGISTRY =
ODBC_LOC_IN_REGISTRY +"ODBCINST.INI\\\";
private const string ODBC_DRIVERS_LOC_IN_REGISTRY =
ODBCINST_INI_LOC_IN_REGISTRY +"ODBC Drivers\\\";
谢谢,这是一个很大的帮助,如果您要使dsn变得更好,则可能需要添加类似的内容
var dsnKeyEng = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(ODBC_INI_REG_PATH + dsnName +"\\\\Engines");
var dsnKeyExl = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(ODBC_INI_REG_PATH + dsnName +"\\\\Engines\\\\Excel");
dsnKeyExl.SetValue("FirstRowHasNames", 01);
dsnKeyExl.SetValue("MaxScanRows", 8);
dsnKeyExl.SetValue("Threads",3);
dsnKeyExl.SetValue("UserCommitSync","Yes")
感谢您提供此代码,我自己使用了它。我不得不改变两件事:
要获取
// Lookup driver path from driver name
var driverKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
ODBCINST_INI_REG_PATH + driverName);
由于运行的是Vista,因此必须使用应用程序清单并将
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
以下文章帮助我找到了
以上就是短码网小编为大家整理的《如何使用C#创建ODBC DSN条目?》相关内容,希望大家喜欢。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将联系本站反馈,一经查实,立即处理!
《如何使用C#创建ODBC DSN条目?》文档下载仅供参考学习,下载后请在24小时内删除。
转载注明出处:https://www.duanma.net/article/4bd2bdfa279.html