问题描述
我已经从服务器资源管理器向我的项目添加了数据库 - >数据连接 - >添加连接。它创建数据库没有任何错误但是当我尝试连接到数据库时,它给出了很长的错误 -
System.Data.dll中发生类型'System.Data.SqlClient.SqlException'的异常但未在用户代码中处理
其他信息:建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供者:命名管道提供程序,错误:40 - 无法打开与SQL Server的连接)
我无法理解有什么错误。请帮我解决这个问题。
以下是代码。我在连接的数据库中创建一个表。
I have added database to my project from server explorer -> Data connections -> Add connection. It creates database without any error but when I try to connect to the database, It gives very long error -
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code
Additional information: 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
I can't understand what is the error about. Please help me to solve this.
Following is the code. I am creating a table in the connected database.
namespace OnlineReportSystem.Admin
{
public partial class AdminRagistration : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\\Users\\Avani\\Documents\\\\\\mynewdb.mdf;Integrated Security=True;Connect Timeout=30;");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnNext1_Click(object sender, EventArgs e)
{
// Response.Write(TabContainer1.ActiveTabIndex.ToString());
TabContainer1.ActiveTab = TabContainer1.Tabs[TabContainer1.ActiveTabIndex + 1];
}
protected void btnCreateSchema_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Create table " + txtSchema.Text + ";";
cmd.CommandType = CommandType.Text;
try
{
using (con)
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Write("successfull");
}
}
catch (Exception ex)
{
Response.Write(ex);
}
}
}
}
推荐答案
这篇关于无法连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!