我用asp.net做了一个管理系统,在每次编译后,布署到IIS,第一次加载时,可能需要编译有点缓慢 ,然后我反复点菜单,链接到页面,偶尔随机会报这两个错ExecuteScalar 要求已打开且可用的连接。连接的当前状态为已关闭ExecuteScalar 要求已打开且可用的连接。连接的当前状态为已连接
我的代码如下
public int GetRecorders(string sql)
{
SqlConnection conn = Connection;
SqlCommand cmd = new SqlCommand("select count(*) from (" + sql + ") a", conn);
try
{
int t = Convert.ToInt32(cmd.ExecuteScalar());
return t;
}
catch
{
throw;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
}
为什么会有这样了,现在我的操作系统是win7 64位,数据库是Sql Server2008,电脑CPU是4核。会不会是多线程的问题?请问各位有何解决办法,因为报错是偶然的,调试时,又不一定会碰到这个错asp.net ado.net iis
我的代码如下
public int GetRecorders(string sql)
{
SqlConnection conn = Connection;
SqlCommand cmd = new SqlCommand("select count(*) from (" + sql + ") a", conn);
try
{
int t = Convert.ToInt32(cmd.ExecuteScalar());
return t;
}
catch
{
throw;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
}
为什么会有这样了,现在我的操作系统是win7 64位,数据库是Sql Server2008,电脑CPU是4核。会不会是多线程的问题?请问各位有何解决办法,因为报错是偶然的,调试时,又不一定会碰到这个错asp.net ado.net iis
解决方案 »
- 时间问题
- 【求助】交叉表的SQL查询
- Freetextbox不兼容updatePanel 求高手!
- 服务器端按钮快速跳转
- formview中,如何设置模版控件中某个TextBox不填时,插入null值,而不是""值
- 初学.net,请教一个关与命名空间的问题?
- 为什么和MasterPage 联系的页面中控件的触发事件,没有效果 (进来有分)
- 在哪里能下载到 计算方法教学网站 的原码 用asp编写的mmmm
- 如何用验证控件验证复选框的状态
- System.ComponentModel.Win32Exception: 拒绝访问
- 返回值为List的函数如何接收返回值
- 未能加载文件或程序集“System.Transactions”或它的某一个依赖项。试图加载格式不正确的程序。
SqlCommand cmd = new SqlCommand("select count(*) from (" + sql + ") a", conn);
这两句之间加上conn.open();把连接打开
{
object obj = new object();
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
using (OleDbCommand comm = new OleDbCommand())
{
conn.Open();
comm.Connection = conn;
comm.CommandType = CommandType.Text;
comm.CommandText = sqlStr;
obj = comm.ExecuteScalar();
}
}
return obj;
}
{
object obj = new object();
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
using (OleDbCommand comm = new OleDbCommand())
{
conn.Open();
comm.Connection = conn;
comm.CommandType = CommandType.Text;
comm.CommandText = sqlStr;
obj = comm.ExecuteScalar();
}
}
return obj;
} 顶楼上的,加个conn.open()就好
{
}
使用时打开连接,使用完了自动关闭