我在线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 :(得分: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 您的密码。
答案 1 :(得分:0)
确保您的连接字符串有效。我倾向于向学生推荐这个:
有更好的方法可以做到这一点,但我认为这相对简单。 HTH。
答案 2 :(得分: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();