ExecuteDatable 报错是什么原因 5
2017-04-04 · 知道合伙人互联网行家
关注
展开全部
[csharp] view plain copy
public DataTable GetDataTable(string strCommandText)
{
this.oleDbCommand.CommandText = strCommandText;
DataTable dt = new DataTable();
try
{
DataRow dr;
//打开连接
this.oleDbConnection.Open();
//建立读取
OleDbDataReader odrReader = this.oleDbCommand.ExecuteReader();
//查询并显示数据
int size = odrReader.FieldCount;
for (int i = 0; i < size; i++)
{
DataColumn dc;
dc = new DataColumn(odrReader.GetName(i));
dc.DataType = odrReader.GetFieldType(i);//必须指定类型,否则自动转为string
dt.Columns.Add(dc);
}
while (odrReader.Read())
{
dr = dt.NewRow();
for (int i = 0; i < size; i++)
{
dr[odrReader.GetName(i)] = odrReader[odrReader.GetName(i)].ToString();
}
dt.Rows.Add(dr);
}
odrReader.Close();
return dt;
}
catch
{
throw new Exception();
}
finally
{
//关闭连接
this.oleDbConnection.Close();
}
}
public DataTable GetDataTable(string strCommandText)
{
this.oleDbCommand.CommandText = strCommandText;
DataTable dt = new DataTable();
try
{
DataRow dr;
//打开连接
this.oleDbConnection.Open();
//建立读取
OleDbDataReader odrReader = this.oleDbCommand.ExecuteReader();
//查询并显示数据
int size = odrReader.FieldCount;
for (int i = 0; i < size; i++)
{
DataColumn dc;
dc = new DataColumn(odrReader.GetName(i));
dc.DataType = odrReader.GetFieldType(i);//必须指定类型,否则自动转为string
dt.Columns.Add(dc);
}
while (odrReader.Read())
{
dr = dt.NewRow();
for (int i = 0; i < size; i++)
{
dr[odrReader.GetName(i)] = odrReader[odrReader.GetName(i)].ToString();
}
dt.Rows.Add(dr);
}
odrReader.Close();
return dt;
}
catch
{
throw new Exception();
}
finally
{
//关闭连接
this.oleDbConnection.Close();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询