我使用下面的代码从c#代码创建odbc:
string ODBC_PATH = "SOFTWARE\\ODBC\\ODBC.INI\\";
string driverName = "SQL Server";
string dsnName = "DSNName";
string database = "DBName";
string description = "Description";
string server = "Server";
bool trustedConnection = false;
string driverPath = "C:\\WINDOWS\\System32\\sqlsrv32.dll";
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);
var dsnKey = Registry.LocalMachine.CreateSubKey(ODBC_PATH + dsnName);
if (dsnKey == null)
{
throw new Exception("DSN was not created");
}
dsnKey.SetValue("Database", database);
dsnKey.SetValue("Description", description);
dsnKey.SetValue("Driver", driverPath);
dsnKey.SetValue("LastUser", "sa");
dsnKey.SetValue("Server", server);
dsnKey.SetValue("Database", database);
dsnKey.SetValue("username", "sa");
dsnKey.SetValue("password", "sa");
dsnKey.SetValue("Trusted_Connection", trustedConnection ? "Yes" : "No");但是上面的代码创建了“系统DNS”。我想创建“用户DNS”,而不需要管理员权限。
发布于 2018-10-21 17:42:33
您需要执行一些步骤:
UACUAC清单以管理员(Run as Administrator)身份启动您的应用程序
基于MSDN的
XML®提供了在可移植可执行文件(PE)图像的资源部分中自动嵌入
应用程序清单文件的功能。本节介绍如何使用Visual Studio创建包含应用程序清单的签名PE映像。因此,此应用程序清单可以包括必要的requestedExecutionLevel属性,从而允许应用程序在Windows Vista上以所需的特权级别运行。当程序启动时,将从PE的资源部分提取应用清单信息并由操作系统使用。不需要使用Visual Studio图形用户界面(GUI)来包含清单。一旦在源代码中进行了必要的更改,使用命令行工具进行编译和链接也会在生成的PE映像中包含应用程序清单。
清单文件若要使用requestedExecutionLevel标记应用程序,请首先创建要与目标应用程序一起使用的应用程序清单文件。可以使用任何文本编辑器创建此文件。应用程序清单文件应与扩展名为.manifest的目标可执行文件同名。例如:IsUserAdmin.exe.manifest
示例:
Executable: IsUserAdmin.exe
Manifest:IsUserAdmin.exe.manifest
Sample application manifest file:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0"
processorArchitecture="X86"
name="IsUserAdmin"
type="win32"/>
<description>Description of your application</description>
<!-- Identify the application security requirements. -->
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges>
<requestedExecutionLevel
level="requireAdministrator"
uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>https://stackoverflow.com/questions/52913857
复制相似问题