[英]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. 我注意到了几件事。
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.