简体   繁体   English

无法使用OleDbConnection从C#连接到本地SQL Server 2014

[英]Can't connect to local SQL Server 2014 from C# using OleDbConnection

I see several answers to problems similar to mine, but I don't know enough to adapt those answers to my problem. 我看到了一些类似于我的问题的答案,但是我对这些问题的解决还不够了解。 So, with apologies for what is probably a duplicate question, here goes: 因此,对于可能是重复问题的道歉,请按以下步骤进行:

I'm trying to connect to a Microsoft SQL 2014 database on my local machine from a ASP.NET application. 我正在尝试从ASP.NET应用程序连接到本地计算机上的Microsoft SQL 2014数据库。 The code is: 代码是:

oCN = new OleDbConnection(connectionString);

With a connectionString of: 使用connectionString:

Provider=SQLNCLI11; Server=FLIPPY\SQLEXPRESS; Trusted_Connection=yes;
Database=FingerTipDisplay; User Id=<my user id>; Password=<my password>

oCN is as follows after the call to new OleDbConnection(): 调用新的OleDbConnection()之后,oCN如下:

-       oCN {System.Data.OleDb.OleDbConnection} System.Data.OleDb.OleDbConnection
        CanRaiseEvents  true    bool
        ConnectionString    "Provider=SQLNCLI11; Server=FLIPPY\\SQLEXPRESS; Trusted_Connection=yes; Database=FingerTipDisplay; User Id=<my user id>; Password=<my password>"    string
        ConnectionTimeout   15  int
        Container   null    System.ComponentModel.IContainer
        DataSource  ""  string
        Database    ""  string
        DbProviderFactory   null    System.Data.Common.DbProviderFactory
        DesignMode  false   bool
+       Events  {System.ComponentModel.EventHandlerList}    System.ComponentModel.EventHandlerList
        Provider    "SQLNCLI11" string
+       ServerVersion   'oCN.ServerVersion' threw an exception of type 'System.InvalidOperationException'   string {System.InvalidOperationException}
        Site    null    System.ComponentModel.ISite
        State   Closed  System.Data.ConnectionState
+       Static members      
+       Non-Public members      

I believe my SQL server is running correctly: 我相信我的SQL Server运行正常:

在此处输入图片说明

I can't get SQL Server Agent to start, and am not sure if that's causing my problem or not. 我无法启动SQL Server代理,并且不确定是否导致我的问题。 From other replies I've ensured TCP/IP is enabled: 从其他答复中,我确保已启用TCP / IP:

在此处输入图片说明

This is my database structure: 这是我的数据库结构:

在此处输入图片说明

and I think that the user name and password I'm connecting has the right permissions from the dbo schema: 而且我认为我连接的用户名和密码具有来自dbo模式的正确权限:

在此处输入图片说明

I've checked the SQL Server logs and don't see anything that looks like a failed login attempt, and I don't know where to look in the OleDbConnection object for feedback on why the connection failed. 我已经检查了SQL Server日志,没有看到任何看起来像失败的登录尝试,并且我不知道在OleDbConnection对象中寻找连接失败原因的反馈。 I'm working on someone else's code, so I'm reluctant to use SqlConnection() since I don't know the implications for the rest of the app. 我正在开发其他人的代码,因此我不愿意使用SqlConnection(),因为我不知道对应用程序其余部分的影响。

I'm guessing that the problem is in the connection string, but I don't know what to use for that. 我猜问题出在连接字符串中,但是我不知道该用什么。 I've tried SQLOLEDB as the provider, and I've tried using Initial Catalog instead of Database. 我尝试使用SQLOLEDB作为提供程序,并且尝试使用初始目录而不是数据库。

Thanks in advance for your help. 在此先感谢您的帮助。

Update: 更新:

Thanks for all the help so far. 感谢到目前为止的所有帮助。 oCN.Open() was throwing an OleDbException immediately, and it was: oCN.Open()立即抛出OleDbException,它是:

"Login failed for user 'riehlj2002@gmail.com'."

I made some changes to the connection string based on the advice below...this is what it looks like right now: 我根据下面的建议对连接字符串进行了一些更改……这是现在的样子:

Provider=SQLNCLI11; server=localhost; DataSource=localhost\SQLEXPRESS; Database=FingerTipDisplay; user id=<my user id>; password=<my password>

Now it doesn't throw the exception right away, but it still throws it. 现在它不会立即引发异常,但仍会引发异常。 This is the exception I get: 这是我得到的例外:

{"Login timeout expired\r\nA network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.\r\nNamed Pipes Provider: Could not open a connection to SQL Server [2]. \r\nInvalid connection string attribute"}

A few things I notice. 我注意到了几件事。

  • First, if I change server to localhost\\SQLEXPRESS I get an immediate exception telling me that I have an invalid connection string attribute, so the advice in this link doesn't work for me. 首先,如果将服务器更改为localhost \\ SQLEXPRESS,则会立即收到异常消息,告诉我我的连接字符串属性无效,因此此链接中的建议对我不起作用。
  • Second, it doesn't seem to matter whether I use localhost or my machine name...it does the same thing. 其次,我使用本地主机还是机器名称似乎都没有关系……它执行相同的操作。
  • Third, I was surprised to see something in there about the named pipes protocol. 第三,我很惊讶地看到关于命名管道协议的内容。 I went into the SQL Server Configuration Manager and enabled that protocol...it didn't make a difference. 我进入了SQL Server配置管理器并启用了该协议...这没什么不同。
  • Fourth, it doesn't make a difference whether I specify DataSource or not in terms of the exception, but intuitively it seems like I have to specify the server instance somewhere so I've left it in. 第四,就异常而言,是否指定DataSource都没有影响,但是直觉上看来,我必须在某个地方指定服务器实例,所以我将其保留了下来。
  • Fifth, if I change the provider to SQLOLEDB I get a different exception: {"[DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied.\\r\\nInvalid connection string attribute"}, so I think I'm on the right track with SQLNCLI11. 第五,如果将提供程序更改为SQLOLEDB,则会收到另一个异常:{“ [DBNETLIB] [ConnectionOpen(Connect())。] SQL Server不存在或访问被拒绝。\\ r \\ n无效的连接字符串属性”},所以我认为我在使用SQLNCLI11方面步入正轨。
  • Sixth, in the OleDbConnection object the DataSource and Database properties are both empty strings despite their being specified in the connection string. 第六,尽管在连接字符串中指定了数据源和数据库属性,但在OleDbConnection对象中它们都是空字符串。
  • Finally, the very last part of the exception I'm getting now talks about an invalid connection string attribute, but I removed each one in turn and either got the same exception or got another one that I've already described. 最后,我要获得的异常的最后一部分现在讨论的是无效的连接字符串属性,但是我依次删除了每个异常,或者得到了相同的异常,或者得到了已经描述过的另一个异常。

Again, thanks for the help. 再一次感谢你的帮助。

You are using Trusted_Connection=yes but specifying a username and password. 您正在使用Trusted_Connection=yes但是指定了用户名和密码。 It's either one or the other, I don't think you can do both in the same connection string (not sure if that'd raise any errors, but the supplied user and password would be at least ignored, for sure). 可能是两者之一,我认为您不能在同一个连接字符串中都做这两个(不确定是否会引发任何错误,但是可以肯定,所提供的用户名和密码至少会被忽略)。

In your case, since you are using a user and a password, you'd need to set Trusted_Connection to no (or false ), or just not set it (it should be false by default) 在您的情况下,由于您使用的是用户名和密码,因此需要将Trusted_Connection设置为no (或false ),或者只是不进行设置(默认情况下应为false

OK, I found the problem. 好的,我发现了问题。 My original connection string wasn't finding the database, so I got no additional information in the server logs. 我原来的连接字符串找不到数据库,因此服务器日志中没有其他信息。 I changed the connection string to: 我将连接字符串更改为:

Provider=SQLNCLI11; server=localhost\SQLEXPRESS; Database=FingerTipDisplay; user id=<my user id>; password=<my user id>

And then I found in the server log that it was configured to use Windows authentication only. 然后,我在服务器日志中发现它已配置为仅使用Windows身份验证。 I used this link to allow SQL server authentication, and all is now well. 我使用链接允许SQL Server身份验证,现在一切都很好。 Your answers got me going in the right direction...thanks. 您的回答使我朝着正确的方向前进...谢谢。

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

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