2012-06-18 42 views
-2

我在myconn.Open()处得到一个错误;如何解决它。错误{“发生了网络相关或特定于实例的错误..”}

{"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:} 


SqlConnection myconn = new SqlConnection(); 
     String connection = "Data Source=SQLEXPRESS;AttachDbFilename=C:\\Users\\Winer\\Documents\\Visual Studio 2008\\WebSites1\\App_Data\\Database.mdf"; 
     myconn.ConnectionString = connection; 
     myconn.Open(); 

有人可以这么说吗?

+0

尝试\ SQLEXPRESS –

+0

错误在线\t \t 1无法识别的转义序列\ SQLEXPRESS – user1334247

+1

HTTP:// WWW。connectionstrings.com/是连接字符串的良好来源。 –

回答

4

这只是表示未找到服务器SQLEXPRESS。我认为你有一个错字错误。试试这个,如果它的工作原理:(Data Source=.\SQLEXPRESS;

String connection = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Winer\\Documents\\Visual Studio 2008\\WebSites1\\App_Data\\Database.mdf"; 

更新1:

.手段或相当于localhost

Data Source=.\SQLEXPRESS;相同Data Source=localhost\SQLEXPRESS;

,或者尝试

Data Source=YOURCOMPUTERNAME\SQLEXPRESS;

更新2

为什么不使用此连接字符串格式?

Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI; 

OR

Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword; 

WHERE

myServerAddress是您的服务器地址
MYDATABASE是你的数据库的名称
名为myUsername是用户名以及
myPassword您的密码。

More ConnectionString format on this link.

+0

错误发生在行\ SQLEXPRESS:。\t 行错误无法识别的转义序列\ SQLEXPRESS – user1334247

+0

需要一个双反斜线或”,否则编译器将寻找转义字符\ S之前的@如果您使用@,您可以删除文件路径中的双反斜杠。 – TheEvilPenguin

+0

尝试使用localhost或您的计算机的名称 –

0

确保您连接字符串是有效的。我倾向于推荐给学生:

  1. 在“服务器资源管理器”面板中,使用向导创建的 数据库的连接。
  2. 在新连接上的“数据连接”部分中,右键单击连接以显示上下文菜单。
  3. 选择'属性'。复制并粘贴现有字符串的 位置的“连接字符串”值。

有更好的方法来做到这一点,但我认为这是相对简单的。 HTH。

0
SqlConnection myconn = new SqlConnection();   
String connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Winer\Documents\Visual Studio 2008\WebSites1\App_Data\Database.mdf";   
myconn.ConnectionString = connection;   
myconn.Open(); 
相关问题