在服务器资源管理器中添加连接时出错:“无法添加数据连接。ExecuteScalar 需要一个打开且可用的连接。”

8

我正在使用Visual Studio 2008,我的数据库是SQL Server 2000。

我想在VS的Server Explorer中添加一个连接。数据源是Microsoft SQL Server(SqlClient)。在输入所有信息并单击测试连接后,连接成功。

但是当我单击确定时,出现错误:

无法添加数据连接。ExecuteScalar需要已打开且可用的连接。连接的当前状态为关闭。


那个有效了...背后的故事是什么? - Steven
2
重启 VS 对我有帮助。 - roman m
3个回答

11

重新启动Visual Studio,然后重新启动您的计算机。


7
重新开始与首次启动,应该就足够了。 - roman m
对我来说,重新启动 VS 就足够了。 - Regfor
一样的情况。重新启动了VS,一切都正常了。谢谢! - Florin Mircea

0

你可以打开服务器资源管理器(视图 -> 服务器资源管理器)重新连接。

你可以删除当前的连接再次打开同一个连接。


0
对于那些正在使用 SQL 命令并出现错误提示 "无法添加数据连接。ExecuteScalar 要求已打开且可用的连接。连接当前状态为关闭。" 的人,请尝试以下操作:
      using (SqlConnection conn = new SqlConnection(connString))
        {
            using (SqlCommand comm = new SqlCommand())
            {
                // query to select all the rows whose column name is the same as id
                comm.CommandText = "SELECT COUNT(*) from tableName where colName like @val1";
                comm.Connection = conn;
                conn.Open();  // <---- adding this line fixed the error for me
                comm.Parameters.AddWithValue("@val1", id);
                // retrieve how many rows are returned after executing the query
                count = (int)comm.ExecuteScalar();  // < --- where the error originally occurred
            }
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接