急问题There is already an open DataReader associated with this Command which must b
做了一个小网站,有时候会出这个问题,知道应该是个常见的问题,但也不太清楚如何解决,请高手指点啊!
There is already an open DataReader associated with this Command which must be closed first.
调用底层代码:(还请高手指点不足之处,先谢谢了!)
public static DataTable GetDataTable(string contype, string sql, string tname)
{
DataTable dt = new DataTable();
using (SqlConnection conn = GetConnection(contype))
{
SqlCommand comm = new SqlCommand();
try
{
comm.Connection = conn;
comm.CommandText = sql;
comm.CommandTimeout = 100;
using (SqlDataAdapter da = new SqlDataAdapter(comm))
{
da.Fill(dt);
}
}
catch (Exception ee)
{
throw new Exception(ee.Message);
}
finally
{
comm.Dispose();
conn.Close();
conn.Dispose();
}
}
return dt;
}
/// <summary>
/// 通过 SqlParameter[] 得到表
/// </summary>
/// <param name="contype">数据库连接的名字</param>
/// <param name="procName">存储过程名称</param>
/// <param name="tname">表名</param>
/// <param name="paras">存储过程的值</param>
/// <returns>表</returns>
public static DataTable GetDataTable(string contype, string procName, string tname, SqlParameter[] paras)
{
using (SqlConnection conn = GetConnection(contype))
{
// SqlDataAdapter da = null;
DataTable dt = new DataTable();
SqlCommand comm = new SqlCommand();
try
{
comm.Connection = conn;
comm.CommandText = procName;
comm.CommandTimeout = 100;
if (paras != null)
{
comm.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter para in paras)
{
comm.Parameters.Add(para);
}
}
using (SqlDataAdapter da = new SqlDataAdapter(comm))
{
da.Fill(dt);
}
}
catch (Exception ex) { throw new Exception(ex.Message); }
finally
{
comm.Dispose();
conn.Close();
conn.Dispose();
}
return dt;
}
}