欢迎光临小站,愿能为您提供帮助与启发,热爱分享、享受分享、乐于分享,让我们携手共同进步。
/// <summary>
/// 查询操作
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="success">返回成功与失败</param>
/// <returns>返回DataTable</returns>
public static DataTable operatingQuery(string sql, ref bool success)
{
DataTable dt = new DataTable();
try
{
DataRow dr;
//1、建立连接
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath + ";Jet OLEDB:Database Password=bbq;";
OleDbConnection odcConnection = new OleDbConnection(strConn);
//2、打开连接
odcConnection.Open();
//建立SQL查询
OleDbCommand odCommand = odcConnection.CreateCommand();
//3、输入查询语句
odCommand.CommandText = sql;
//建立读取
OleDbDataReader odrReader = odCommand.ExecuteReader();
//查询并显示数据
int size = odrReader.FieldCount;
for (int i = 0; i < size; i++)
{
DataColumn dc;
dc = new DataColumn(odrReader.GetName(i));
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();
odcConnection.Close();
success = true;
return dt;
}
catch
{
success = false;
return dt;
}
}
路过此地 雁过留声
感谢分享,收获很多,楼主辛苦