【发布时间】:2014-04-05 20:42:10
【问题描述】:
我在 Visual Studio 2013 中,在 Windows 7 操作系统上使用 SQL Server Express 2012。
使用 C# 代码,我可以连接到数据库并进行查询。例如:
using (SqlConnection sqlConnection = new SqlConnection("server=MYSERVER\\SQLEXPRESS; Trusted_Connection=yes; database=MyDatabase; connection timeout=30"))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT count(*) FROM tblData";
cmd.Connection = sqlConnection;
sqlConnection.Open();
int count = (int)cmd.ExecuteScalar();
Console.WriteLine(count); // I get the correct answer.
}
}
这行得通。我的问题是,如果我在 Visual Studio 中使用服务器资源管理器,我无法通过该路由连接到数据库。在添加连接中,MyServer 确实出现在服务器下拉列表中,但数据库下拉列表为空。当我单击测试连接或确定(将数据库留空或输入 MyDatabase)时,我收到错误:找不到服务器或无法访问。
所以我可以通过 C# 代码进行连接,但不能通过服务器资源管理器进行连接。我错过了什么?
谢谢。
【问题讨论】:
-
尝试用管理员运行VS..
-
谢谢。但我的问题是我应该输入 MYSERVER\SQLEXPRESS (raja) 而不是只使用 MYSERVER 的下拉选项。
标签: c# connection visual-studio-2013 sql-server-2012-express server-explorer