为什么我无法让 CDatabase 对象理解我的数据源名称?

发布于 2024-07-16 17:50:31 字数 959 浏览 4 评论 0原文

我正在使用 MFC 类 CDatabase。 为了建立与 SQL Server 的连接,我使用连接字符串调用 OpenEx()。 我的问题是该对象似乎无法解释字符串的 DSN 部分。 连接字符串如下所示:

 ODBC;DSN=mySystemDSN;UID=myUsername;WSID=myMachineName;DATABASE=myDatabaseName;Trusted_Connection=Yes

这应该是正确的,因为我是通过使用 OpenEx( NULL )、手动选择数据源然后调用 GetConnect() 获得的。

但这个字符串似乎没有包含足够的信息:OpenEx() 总是弹出一个对话框,要求提供更多信息。 我从这个对话中选择什么似乎并不重要 - 我可以选择一个与完全不同的数据库关联的 DSN,并且一切仍然有效(在这种情况下调用 GetConnect() 表明它正在使用除 DSN 之外的我的连接字符串)部分,这是从其他数据源借来的)。

我需要我的应用程序能够自动连接到数据库 - 对话框是一个破坏者。 我尝试过无 DSN 的连接,结果类似。 这是怎么回事?我能做什么?

编辑回答 Neil Butterworth 的问题:

我在 ODBC 数据源管理器中创建 DSN 时提供的信息如下:

驱动程序:SQL Server
名称:mySystemDSN
服务器:myMachineName
身份验证类型(可以是 Windows 或 SQL Server):Windows
复选框“连接到 SQL Server 以获取其他配置选项的默认设置。”:已勾选
复选框“将默认数据库更改为”:勾选并从下拉菜单中选择 myDatabaseName
复选框“使用 ANSI 带引号的标识符”:勾选
复选框“使用 ANSI 空值、填充和警告”:勾选
复选框“对字符数据执行翻译”:勾选

I'm using the MFC class CDatabase. To establish a connection to SQL Server, I'm calling OpenEx() with a connection string. My problem is that the object seems unable to interpret the DSN part of the string. The connection string looks like this:

 ODBC;DSN=mySystemDSN;UID=myUsername;WSID=myMachineName;DATABASE=myDatabaseName;Trusted_Connection=Yes

That ought to be right because I got it by using OpenEx( NULL ), choosing the data source manually and then calling GetConnect().

But this string doesn't seem to contain enough information: OpenEx() always pops up a dialogue asking for more. It doesn't seem to matter what I choose from this dialogue - I can pick a DSN associated with a completely different database and things still work (a call to GetConnect() in that situation shows that it's using my connection string except for the DSN part, which is borrowed from the other data source).

I need my application to be able to connect to the database automatically - dialogue boxes are a deal-breaker. I've tried a DSN-less connection with similar results. What's going on here, and what can I do about it?

edit in answer to Neil Butterworth's question:

The information I provided when I created the DSN in the ODBC Data Source Administrator was as follows:

driver: SQL Server
name: mySystemDSN
server: myMachineName
authentication type (can be Windows or SQL Server): Windows
checkbox "Connect to SQL Server to obtain default settings for the additional configuration options.": ticked
checkbox "Change the default database to": ticked and myDatabaseName chosen from drop-down menu
checkbox "Use ANSI quoted identifiers": ticked
checkbox "Use ANSI nulls, paddings and warnings": ticked
checkbox "Perform translation for character data": ticked

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

碍人泪离人颜 2024-07-23 17:50:31

您似乎缺少密码和服务器的主机名。 我想有趣的问题是您在创建 DSN 的对话框中输入了哪些信息?

编辑:您可能还想看看此网站,其中有很多连接字符串示例。

编辑2:我会删除“ODBC;” 在字符串的开头。 如果这不起作用,我会将 DSN 中的身份验证类型更改为 SQL Server 并提供用户 ID 和用户名。 明确密码,只是为了检查身份验证不是问题。 然后我可能会放弃:-)

You seem to be missing a password and the hostname of the server. I suppose the interesting question is what information did you enter in the dialog to create the DSN?

Edit: You may also want to take a look at this site, which has a lot of connection string examples.

Edit2: I would remove the "ODBC;" at the start of the string. If that doesn't work, I'd change the authentication type in the DSN to SQL server and supply a user id & password explicitly, just to check that authentication is not the problem. And then I'd probably give up :-)

雪若未夕 2024-07-23 17:50:31

感谢 Neil Butterworth,我在此处找到了一个有效的答案:

“Driver={SQL Native Client};Server= myMachineName;数据库=myDatabaseName;Trusted_Connection=是;"

我仍然很困惑为什么在连接工作时调用 GetConnect() 不会产生完美的 DSN 字符串,但现在我已经有了一个无 DSN 的解决方案,我不在乎了!

Thanks to Neil Butterworth, I found a working answer here:

"Driver={SQL Native Client};Server=myMachineName;Database=myDatabaseName;Trusted_Connection=yes;"

I'm still baffled as to why calling GetConnect() when the connection is working doesn't produce a perfect DSN string, but now that I've got a DSN-less solution I don't care as much!

悲念泪 2024-07-23 17:50:31

一些选项:

  • 它是系统 DSN 还是用户 DSN? 也就是说,DSN 对执行用户可见吗?
  • 尝试删除 UID=myUsername
  • 尝试使用 Driver=ODBC 而不是 ODBC

为什么您使用 DSN 而不是更传统的自包含连接字符串? 如Driver=SQLOLEBD;WSID=myMachineName;DATABASE=myDatabaseName;Trusted_Connection=Yes;

Some options:

  • Is it a system or user DSN? That is, is the DSN visible to the executing user?
  • Try removing the UID=myUsername
  • Try Driver=ODBC instead of ODBC

Why are you using a DSN and not a more conventional, self contained connection string? Such as Driver=SQLOLEBD;WSID=myMachineName;DATABASE=myDatabaseName;Trusted_Connection=Yes;

泪是无色的血 2024-07-23 17:50:31

为了创建连接字符串,我使用 www.connectionstrings.com。 非常适合在所有变体和数据库引擎上创建连接字符串

for creating connections strings i use www.connectionstrings.com. Fantastic for creating connection strings on all variants and database enginges

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文