简体   繁体   English

DSN 到连接字符串?

[英]DSN to connectionstring?

We've got an ASP.NET website that uses a database that we want to be able to use a connectionstring to get to.我们有一个 ASP.NET 网站,它使用我们希望能够使用连接字符串访问的数据库。 We've successfully set up a DSN for connecting to this DB, but I can't seem to discover the correct magic to go with a connectionstring.我们已经成功地设置了一个 DSN 来连接到这个数据库,但我似乎无法使用连接字符串发现 go 的正确魔法。

Is there a straightforward way to translate the values from the DSN into a connectionstring?是否有直接的方法将 DSN 中的值转换为连接字符串? I know that from the UI, there isn't an obvious answer for this...each db vendor provides a different UI for creating a DSN based on what they require.我知道从用户界面来看,没有一个明显的答案......每个数据库供应商都提供不同的用户界面来根据他们的需要创建 DSN。 However, I was hoping that underneath the UI it might just be doing something like creating a connection string behind the scenes, and I could look at that to see what I'm doing wrong.但是,我希望在 UI 下它可能只是在做一些事情,比如在幕后创建一个连接字符串,我可以查看它以了解我做错了什么。 Any hope of this?这有希望吗? If so, any pointers on how to get the info I need?如果是这样,关于如何获取我需要的信息的任何指示?

(I've gone to connectionstrings.com to try to make sure my connection string is in the right format, but nothing seems to be working...which is why I'm trying this strange translate-from-dsn tact.) (我已经转到 connectionstrings.com 以尝试确保我的连接字符串格式正确,但似乎没有任何效果......这就是为什么我正在尝试这种奇怪的 translate-from-dsn 策略。)

EDIT: Something I must not have been clear on is that we do not want to have a DSN entry.编辑:我一定不清楚的是我们不想有 DSN 条目。 We have created one, and have used it for the time being, but we want to be able to get rid of it and use a connectionstring without a dsn.我们已经创建了一个,并暂时使用了它,但我们希望能够摆脱它并使用没有dsn 的连接字符串。

If you can use OLEDB, then you can create a UDL file .如果可以使用 OLEDB,则可以创建UDL 文件 Just create a new text document, test.udl and double click.只需创建一个新的文本文档, test.udl并双击。 Fill out the dialog, then open it back up with Notepad.填写对话框,然后用记事本打开它。 Voila - there's your connection string.瞧 - 这是您的连接字符串。

ODBC is a bit harder - you can either create a file DSN from ODBC Administrator or poke around the registry in HKLM\Software\ODBC\ODBC.INI\<DSN Name> for a system DSN. ODBC 有点难 - 您可以从 ODBC Administrator 创建一个文件 DSN,或者在HKLM\Software\ODBC\ODBC.INI\<DSN Name>中的注册表中查找系统 DSN。 You'll end up with some name/value pairs.您最终会得到一些名称/值对。 You should be able to translate those into a connection string.您应该能够将它们转换为连接字符串。 The \\Driver will list the actual DLL, so you'll need to get the provider name from HKLM\Software\ODBC\ODBC Data Sources\\<DSN Name> . \\Driver将列出实际的 DLL,因此您需要从HKLM\Software\ODBC\ODBC Data Sources\\<DSN Name>获取提供商名称。

If you can use the OLEDB Provider for ODBC, then you can use the UDL trick and have it build a connection string from an ODBC file DSN as well.如果您可以使用 ODBC 的 OLEDB 提供程序,那么您可以使用 UDL 技巧并让它从 ODBC 文件 DSN 构建连接字符串。 The ODBC connection string will be in Extended Properties of the UDL. ODBC 连接字符串将位于 UDL 的扩展属性中。

If you have created a DSN, then the DSN is the ConnectionString !如果您已经创建了 DSN,那么 DSN就是ConnectionString !

You can simply use DSN=<YourDSNName> and pass it to an OdbcConnection object.您可以简单地使用DSN=<YourDSNName>并将其传递给 OdbcConnection object。

For instance, using C#:例如,使用 C#:

string dsnName = "DSN=MyDSN";
using (OdbcConnection conn = new OdbcConnection(dsnName))
{
  conn.Open();
}

Alternatively, you can use the OdbcConnectionStringBuilder class and set its DSN property.或者,您可以使用OdbcConnectionStringBuilder class 并设置其DSN属性。

To expand on Mark Brackett's answer about the registry: for a 32-bit ODBC on a 64-bit Windows, the registry path is HKLM\Software\Wow6432Node\ODBC\ODBC.INI\扩展 Mark Brackett 关于注册表的回答:对于 64 位 Windows 上的 32 位 ODBC,注册表路径为 HKLM\Software\Wow6432Node\ODBC\ODBC.INI\

In my case it was sufficient to:就我而言,足以:

  • Remove the header删除 header
  • Replace all the new lines with semicolons用分号替换所有新行
  • Use braces as a group separator使用大括号作为组分隔符

Here's my DSN File as created by ODBC Data Source Administrator (3rd tab—File DSN)这是由 ODBC 数据源管理员创建的我的 DSN 文件(第 3 个选项卡 - 文件 DSN)

[ODBC]
DRIVER=MySQL ODBC 5.3 ANSI Driver
UID=MyUserName
PORT=3306
DATABASE=mydatabasename
SERVER=localhost

And here's what my connection string looked like:这是我的连接字符串的样子:

DRIVER={MySQL ODBC 5.3 ANSI Driver};UID=MyUserName;PORT=3306;DATABASE=mydatabasename;SERVER=localhost

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM