日期:2014-05-19  浏览次数:20631 次

一个查询的怪异问题,connection关闭后还可以用Adapter填充吗?
public   String   executeScalar(String   sqlStr)
{
SqlCommand   cmd=new   SqlCommand(sqlStr,myCon);
cmd.CommandType=CommandType.Text;
return   cmd.ExecuteScalar().ToString();
}
public   DataSet   execute(String   sqlStr,int   pageSize,int   pageIndex,String   col)
{
SqlDataAdapter   da;
DataSet   dataSet=new   DataSet();

String   countSqlStr=sqlStr;
da=new   SqlDataAdapter(getSqlPage(sqlStr,pageSize,pageIndex,col),myCon);
da.Fill(dataSet);

return   dataSet;
}
public   bool   close()
{
try
{
myCon.Close();
return   true;
}
catch
{
return   false;
}
}
public   bool   open(SqlConnection   con)
{
try
{
myCon=con;
con.Open();
return   true;
}
catch
{
return   false;
}
}

上面是几个相关的函数,先调用open函数传入一个已open的sqlcon对象,然后可调用executeScalar和execute进行查询,调用close后调用executeScalar函数提示连接已关闭,但调用execute仍然可以查询出数据库的任何数据!请问是不是我的函数有问题!   怪异!

------解决方案--------------------
DataAdapter对象是不需要显式地建立连接的,直接使用连接字符串都可以。它会在需要时自动建立连接和断开连接的,你不用去管它。