public DataTable ExportToDataTable(string sql) //返回DataSet对象
{
ConnectionDB();
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(sql, connection);
da.Fill(dt);
}
2.不使用fill
public DataTable ExportToDataTable(string sql) //返回DataSet对象
{
ConnectionDB();
DataTable dt = new DataTable();
MySqlCommand cmd = new MySqlCommand(sql, connection);
cmd.CommandTimeout = 200;
MySqlDataReader reader = cmd.ExecuteReader();
if(reader.HasRows)
{
int fieldCount = reader.FieldCount;
for(int i=0; i < fieldCount; i++)
{
dt.Columns.Add(reader.GetName(i), reader.GetFieldType(i));
}
dt.BeginLoadData();
object[] objValues = new object[fieldCount];
while (reader.Read())
{
reader.GetValues(objValues);
dt.LoadDataRow(objValues, true);
// dt.Load(reader);
}
reader.Close();
dt.EndLoadData();
return dt;
}
}