通过ODBCConnection连接时无效连接字符串属性

发布于 2025-02-08 21:18:19 字数 624 浏览 2 评论 0原文

我正在尝试构建一个C#WPF应用程序,该应用程序可以通过DSN连接到多种类型的数据库,并且我已经遇到了一个障碍。通常,连接信息(用户名/密码)在ODBC连接中完全陈述,但是当试图连接到SQL Server(用于测试目的)时,我会遇到一些问题。

如果我尝试

string connString = $"DSN={dsn};";
OdbcConnection cnn = new OdbcConnection(connString);
cnn.Open()

我会有错误

“用户登录失败”。

如果我更改连接字符串以专门陈述用户名和密码,例如

string connString = $"DSN={dsn}; UID = {username}; PW = {password};";

我收到相同的错误加上

无效连接字符串属性

在搜索不同的连接字符串示例后,我尝试了此不同版本,但我很困惑。看起来服务器正在返回一个错误,我将其传递给用户名“”而不是实际的登录名(CCLOGIN),并且连接字符串的格式错误,但我找不到格式的任何问题。 ..

I'm trying to build a C# WPF application that can connect to multiple types of databases via DSN and I've hit a snag. Normally the connection info (username/password) is entirely stated in the ODBC connection but when trying to connect to an SQL server (that I set up for test purposes) I've run into some problems.

If I try

string connString = 
quot;DSN={dsn};";
OdbcConnection cnn = new OdbcConnection(connString);
cnn.Open()

I get an error

"Login failed for user ''."

If I change the connection string to specifically state the username and password like

string connString = 
quot;DSN={dsn}; UID = {username}; PW = {password};";

I get the same error plus

Invalid connection string attribute

I've tried different versions of this after searching for different connection string examples but I'm stumped. It appears like the server is returning an error that I passed it the username '' rather than the actual login name (CCLogin), and also that the connection string is in the wrong format but I can't find any problems with the format...

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

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

发布评论

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

评论(1

混浊又暗下来 2025-02-15 21:18:19

用UID指定用户名是正确的,但是密码属性是PWD - 您缺少The TrafingD。这就是它所抱怨的。登录失败是临时的 - 它没有密码,但这是次要问题。您可能还会发现它对DSN -ODBC Favors Server = {server}; database = {database}不满意。通常,您还会还指定驱动程序,因此完整的ODBC连接字符串看起来像一个(假设您使用SQL Server):

driver = {sql Server}; server = Servername; database; database = db_name; uid = username ; pwd = password;

或,如果它是一个受信任的连接(使用与Windows登录相同的凭据)

driver = {sql Server}; server = servername; database; database = db_name; trusted connection; trust connection = yes; yes; <<<<<<< /code>

从任何一个合适的开始,假设它有效,如果您想进行实验以查找最低限度的内容,则可以从一次消除一个键值对开始,直到它破裂为止。不过,我建议您使用长格式。

如果您的服务器使用SQL Server以外的其他方式,请通过搜索“ ODBC数据源管理”来查找驱动程序名称。这将为您提供适当的应用程序,您使用的驱动程序应在此处列出。

ODBC的好处是老化且非常稳定,但是它对您的设置方式往往很挑剔。好消息是,一旦正确设置,它将在几十年内就不会浮出水面。

Specifying the username with UID is correct, but the password attribute is PWD - you're missing the trailing D. That's what it complains of. The login failure is extemporaneous - it has no password, but that's a secondary problem. You'll likely also find that it's unhappy with DSN - ODBC favors Server={Server};Database={database}. Typically you'll also specify a driver so a full ODBC connection string would look like one of (assuming you're using Sql Server):

Driver={Sql Server};Server=servername;Database=db_name;Uid=username;Pwd=password;

or, if it's a trusted connection (using the same credentials as your windows login)

Driver={Sql Server};Server=servername;Database=db_name;Trusted Connection=Yes;

Start with whichever of those is appropriate and, assuming it works, if you want to then experiment to find what the base MINIMUM is, you can start by eliminating one key-value pair at a time until it breaks. As a bit of advice, though, I'd suggest using the long-form.

If your server is using other than SQL Server, look for the driver name by searching for "ODBC Data Source Administration". That will give you the appropriate application and the driver you're using should be listed there.

ODBC has the benefit of being old and very stable, but it tends to be fussy about how you set it up. The good news is that once set up properly, it'll behave for decades without a blip.

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