110,538
社区成员
发帖
与我相关
我的任务
分享
class OleDbHelper
{
private OleDbConnection conn;
private OleDbDataAdapter oda = new OleDbDataAdapter();
private OleDbCommand cmd;
private DataSet ds = new DataSet();
public OleDbHelper(string FilePath, string FileName)
{
string strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
strconn += FilePath +@"\"+ FileName;
conn = new OleDbConnection(strconn);
}
public DataSet getData(string strSQL)
{
oda = new OleDbDataAdapter(strSQL, conn);
oda.Fill(ds);
return ds;
}
public bool setData(string strSQL)
{
conn.Open();
cmd = new OleDbCommand(strSQL, conn);
cmd.ExecuteNonQuery();
conn.Close();
return true;
}
//批量更新数据
public bool setDs(DataTable dt)
{
OleDbCommandBuilder builder = new OleDbCommandBuilder(oda);
oda.UpdateCommand = builder.GetUpdateCommand();
if (myds.HasChanges())
{
try
{
oda.Update(dt);
}
catch (Exception ex)
{
throw ex;
//return false;
}
return true;
}
else
{
return false;
}
}
}
....
......
前台页面加载数据及报表
//加载报表
private DataSet ds;
private ReportDocument rd;
private readonly string sqlTail = "SELECT * FROM Goods";
private void LoadData()
{
ds = dbhelper.getData(strSql);
rd = new ReportDocument();
rd.Load(reportPath);
rd.SetDataSource(ds);
this.crystalReportViewer1.ReportSource = rd;
this.crystalReportViewer1.RefreshReport();
}
//用这个方式试试
// 读取mdb数据
public static c ReadAllData(string tableName, string mdbPath)
{
DataTable dt = new DataTable();
try
{
DataRow dr;
//1、建立连接
string strConn
= @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath + "; OleDbConnection odcConnection = new OleDbConnection(strConn);
//2、打开连接
odcConnection.Open();
//建立SQL查询
OleDbCommand odCommand = odcConnection.CreateCommand();
//3、输入查询语句
odCommand.CommandText = "select * from " + tableName;
//建立读取
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();
return dt;
}
catch
{
return dt;
}
}
DataTable myrpt=ReadAllData(tableName,mdbPath);
CryView.ReportSource = myrpt;