日期:2014-05-17 浏览次数:20393 次
public static class DBHelper { private static SqlConnection connection; //private static SqlDataAdapter da; public static SqlConnection Connection { get { string connectionString = ConfigurationManager.ConnectionStrings["XCgo"].ConnectionString; if (connection == null) { connection = new SqlConnection(connectionString); connection.Open(); } else if (connection.State == System.Data.ConnectionState.Closed) { connection.Open(); } else if (connection.State == System.Data.ConnectionState.Broken) { connection.Close(); connection.Open(); } return connection; } } public static DataTable GetDataTable(string safeSql) { DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand(safeSql, Connection); SqlDataAdapter da = new SqlDataAdapter(cmd); cmd.Dispose(); da.Fill(ds);//异常出在这里,提示There is already an open DataReader associated with this Command which must be closed first. da.Dispose(); connection.Close(); return ds.Tables[0]; } }