使用VB.NET创建ODBC DSN [英] Create ODBC DSN using VB.NET

查看:47
本文介绍了使用VB.NET创建ODBC DSN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为数据库创建ODBC DSN(每当用户设置应用程序时,它将包含在我的应用程序文件夹中).我想知道如何使用所有必需的参数创建ODBC,以便在运行安装程序时要做的第一件事就是将数据库连接到应用程序.我尝试使用默认的VB.NET安装向导和Installshield.

I want to create ODBC DSN for a database (which will be included in my application folder whenever a user setups the application). I want to know how to create ODBC with all the required parameters so that the first thing that is done when the setup is being run is connecting the database to the application. I have tried using default VB.NET setup wizard and Installshield.

推荐答案

我不知道通过安装程序或VB.NET创建DSN的任何方法,但是使用Windows实用程序可以创建DSN.

I don't know any method to create DSN via installer or with VB.NET but using Windows utilities you can create one.

页显示了如何使用命令行实用程序odbcconf在Windows中创建DSN,因此一旦创建了可以根据应用程序要求创建DSN的批处理脚本,就可以触发批处理执行,然后将安装程序将文件复制到用户计算机.因此,在执行脚本之后,应用程序安装将进一步移动以复制剩余文件,并且在安装完成并启动应用程序之后,由于现已创建DSN,因此您与DSN的连接将按预期工作.虽然我还没有使用InstallShield,但我在NSIS上的经验表明,通过安装程序执行批处理脚本肯定会在安装过程中弹出命令提示符窗口.

this page shows how to create DSN in Windows using command line utility odbcconf so once you create a batch script that can create DSN according to your application requirements, you can trigger the batch to get executed before your setup copies files to the users computer. So after the script is executed, application installation will move further to copy remaining files and after installation completes, and application starts, your connection to the DSN will work as expected, since DSN is now created. I haven't worked with InstallShield though, my experience with NSIS says that executing batch script via installer will surely bring up the command prompt window during installation, which you might not want.

避免显示命令提示符窗口的另一种方法是在应用程序首次建立数据库连接之前,先运行它的批处理脚本(以静默方式运行).您可以使用WScript.Shell对象执行批处理文件(.bat),而无需显示命令提示符窗口,如下所示.

Another way to avoid showing command prompt window is to execute your batch script (in silent mode) upon first run of your application, before it establishes any connection to database. You can execute your batch file (.bat) without showing command prompt window using WScript.Shell object as follows.

Dim batfile As String

batfile = "C:\Program Files\Your App\YouDSNCreationBatchFile.bat"

Set WshShell = CreateObject("WScript.Shell")

cmds = WshShell.RUN("""" & batfile & """", 0, True) 'All Quotes are necessary if file path contains folders with spaces in thier names

Set WshShell = Nothing

尽管odbcconf也可以直接在VB中用于创建DSN,但是如果您不想在连接之前调整应用程序代码以创建DSN,则使用批处理脚本将是一种故障安全的解决方案,但是,我猜想,您可能已在try块中连接了应用程序,因此,如果捕获由于缺少DSN可用性而引发的异常,则可以再次调用此批处理脚本来创建一个或提示用户使用该批处理脚本.

Though odbcconf can also be used in VB directly to create DSN, but if you don't want to tweak your application code to create DSN before it connects, using batch script will be a fail-safe solution, however, as I guess, you might have connected your application in try block, so if you catch exception which thrown due to lack of availability of DSN it required, this batch script can be called again to create one or prompt user for the same.

!!重要提示:正如MSDN页面所述,odbcconf实用程序很可能会从将来的Windows数据访问组件版本中删除,如果您长期使用此方法,则应重新考虑此方法.

!!IMPORTANT: As MSDN page says, odbcconf utility is likely to be removed from future versions of Windows Data Access Components, you should reconsider this method if you are using this for long-term.

这篇关于使用VB.NET创建ODBC DSN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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