执行命令时无法连接到 SQL Server
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(
"Server=ROHIT-PC\\ROHIT \\Database=Dorknozzle;" +
"Integrated Security=True");
SqlCommand comm = new SqlCommand(
"SELECT EmployeeID, Username FROM Employees", conn);
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
while(reader.Read())
{
employeesLabel.Text += reader["Username"] + "<br />";
}
reader.Close();
reader.Close();
conn.Close();
}
}
}
我在 conn.open();
处收到错误
消息
与网络相关或特定于实例的 建立时发生错误 连接到 SQL Server。服务器 未找到或无法访问。 验证实例名称是否为 正确并且 SQL Server 是 配置为允许远程 连接。 (提供商:SQL 网络 接口,错误:26 - 错误定位 指定的服务器/实例)..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(
"Server=ROHIT-PC\\ROHIT \\Database=Dorknozzle;" +
"Integrated Security=True");
SqlCommand comm = new SqlCommand(
"SELECT EmployeeID, Username FROM Employees", conn);
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
while(reader.Read())
{
employeesLabel.Text += reader["Username"] + "<br />";
}
reader.Close();
reader.Close();
conn.Close();
}
}
}
I'm getting error at conn.open();
saying
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)..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信
Server=ROHIT-PC\\ROHIT \\Database=Dorknozzle;
应该读成类似的内容(假设服务器的名称是
ROHIT-PC
并且服务器的名称SQL Server 实例是ROHIT
)I believe
Server=ROHIT-PC\\ROHIT \\Database=Dorknozzle;
should read something like(assuming the name of the server is
ROHIT-PC
and the name of the SQL Server instance isROHIT
)问题一定出在您的连接字符串中。
编辑:这个
“Server=ROHIT-PC\\ROHIT \\Database=Dorknozzle;”
我认为应该是:
The problem must be in your connection string.
Edit: This
"Server=ROHIT-PC\\ROHIT \\Database=Dorknozzle;"
should be I think:
错误消息告诉您需要知道的一切:找不到服务器或无法访问服务器。诀窍是。为什么您的应用程序找不到它?您能否提供更多信息。
你能 ping 通服务器吗?服务器开了吗?与服务器的其他连接是否正常工作?可以连接SSMS吗?连接字符串的格式是否正确?如果您使用 Visual Studio 进行编程,您可以使用服务器浏览器找到服务器吗? (如果是这样,您可以使用它来生成格式正确的连接字符串。)此错误也可能是由于服务器太忙于处理主要内容并导致超时错误而引起的。您可以看到服务器上的活动吗?是不是要疯了?
从您发布的内容中我们只知道您请求了服务器但没有得到回复。但我们确实没有足够的信息来告诉您原因。
The error message tells you everything you need to know: The server was not found or was not accessible. The trick is. Why can't your application find it? Can you provide any more information.
Can you ping the server? Is the server on? Are other connections to the server working? Can you connect with SSMS? Is the connection string properly formatted? If your programming with Visual Studio can you find the server with the Server Browser? (If so, you can use it to generate a properly formatted connection string.) This error could also be caused because the server is too busy handling something major and causing a timeout error. Can you see the activity on the server? Is it going crazy?
All we know from what you've posted is that you asked for a server and didn't get a response. But we don't really have enough information to tell you why.