以编程方式添加ODBC数据库 [英] Adding ODBC Database Programmatically

查看:80
本文介绍了以编程方式添加ODBC数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#编写一个使用ODBC数据库作为其数据源的数据库应用程序。关键是,只要应用程序安装在另一台计算机上,就必须手动完成ODBC连接。

我想以编程方式进行,以便减少对用户的负担。我试过但有一个安全异常错误,这意味着我没有权利写信给HLM。



请问有没有出路。我将感激任何给予的帮助。 Thanx。

I am writing a database application with C# that uses an ODBC database as its data source. The point is that any time the application is installed on a different computer the ODBC connection must be done manually.
I would want to do it programmaticaly so that less hectic is placed on the user. I tried but had a security exception error which meant I do not have the right to write to the HLM.

Please is there a way out. I will be grateful of any help given. Thanx.

推荐答案

你在使用什么数据库?



这个片段是你在寻找什么?



What database are you using?

Is this snippet what you are looking for?

[DllImport("ODBCCP32.DLL")]
public static extern bool SQLConfigDataSource(int hwndParent, int fRequest, string lpszDriver, string lpszAttributes);
const  ODBC_ADD_DSN = 1;
const  ODBC_ADD_SYS_DSN = 4;

public object CreateOdbcDataSource()
{
    string driver = "Your Driver Name Here"; //Ex: Sql Server
    string odbcAttributes = "DSN=Sample" + Strings.Chr(0) + "Database=Your Database" + Strings.Chr(0) + "Description= This is a database" + Strings.Chr(0) + "Server=(local)" + Strings.Chr(0) + "Trusted_Connection=No" + Strings.Chr(0);

    SQLConfigDataSource(0, ODBC_ADD_SYS_DSN, driver, odbcAttributes);
}


这篇关于以编程方式添加ODBC数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆