ASP.NET beginner question: How to get rid of this error?

巧了我就是萌 提交于 2019-12-24 08:59:00

问题


I am trying to create a simple login page in ASP.NET C# but am getting some error.

I googled about the error, and tried all the solutions but to no avail. What am I doing wrong? The error is:

"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"

I think it's something to do with my connection string which is as follows:

 <add name="cn"
      connectionString="server=HCL;DataBase=GS;Integrated Security=True" />

"HCL" is another machine connected to my PC through the LAN and this database, "GS", resides on it. Is the problem with the value I am giving in "Server" attribute of the above code? If not what else may I be doing wrong?

I can tell all solutions I tried. Let me know if that's required.


回答1:


I don't think "Server" is a valid property for a connection string. Try "Data Source" instead. "Integrated Security" should also be set to SSPI:

<add name="cn" connectionString="Data Source=HCL;Initial Catalog=GS;Integrated Security=SSPI"/>

UPDATE: I just noticed that you have also used "Database". This should be "Initial Catalog".

UPDATE2: There is a neat trick for creating connection strings by using .udl files. If you create an empty file called "something.udl" and double click it, Windows will open a nice dialog for defining connections. To create a connection string for a SQL Server, choose "Microsoft OLEDB Provider for SQL Server" on the "Provider" tab and then fill in your server name, login credentials, and database name on the "Connection" tab. Finish by testing the connection and click "OK".

After the dialog is closed, you can drag the .udl file into Notepad or Visual Studio and you will see that the .udl file actually contains a connection string ready for you to use (note that if you wish to use the connection string with SqlConnection in .NET you must remove the "Provider=SQLOLEDB.1" part of the string).




回答2:


The error is definitely connectivity. Usually in firewall rules, remote connections disabled or just general connectivity issues.

However using integrated security is probably your issue. If you are in a domain/workgroup this could work,w ith properly set permissions, but if not then you probably need to pass your credentials. Integrated security will pass the credentials of your logged in account and is the normal method if the database in on the same PC as the application.

<add name="cn" connectionString="Data Source=HCL;Initial Catalog=GS;User Id=YOURSQLUSERNAME;Password=YOURSQLPASSWORD;"/>

See http://www.connectionstrings.com/ for more connection string examples.




回答3:


Try adding "Integrated Security=SSPI;" or specify your UID, Pwd for connecting to the server. You might be getting struck at the authentication.




回答4:


  • Open management studio and connect with SQL Server using SQL Server Authentication. Enter userId and password to connect. Use that userid and password in your connection string.

    connectionString="Data Source=HCL;Initial Catalog=GS;User Id=UserId;Password=password;

  • User Id -> UserId you want to login using. like 'sa'

  • Password -> password of you user.

If you get error Login failed for user" 'yourusername' then this link will help. This is to enable user sa.

SQL Server 2005 "Login failed for user" sa

EDIT:

connectionString="Data Source=HCL;Initial Catalog=GS;Persist Security Info=True;User ID=sa;Password=pass"

This link might help

sql-network-interfaces-error-26-error-locating-server-instance-specified




回答5:


I think you should try trusted_connection = true instead of integrated security in your connectionstring.

See here: http://connectionstrings.com/sql-server-2005

Second reason could be that you have several instances of your database which you have to write into the connectionstring.



来源:https://stackoverflow.com/questions/3665084/asp-net-beginner-question-how-to-get-rid-of-this-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!