asp.net[c#]的access数据库操作类
//网上很多都是操作sql ser 的,整理了一下,不是很完善,但不影响使用,呵呵……
//private string datapatch = configurationsettings.appsettings["acessconn"];//数据库地址
private string datapatch = "db/global.asa";//数据库地址
///
/// 取得dataset
///
/// 查询语句
///
public dataset getdataset(string commandtext)
{
string strconnection = "provider=microsoft.jet.oledb.4.0; " +
"data source=" + server.mappath(datapatch);
string strcommandtext = commandtext;
oledbconnection myconnection = new oledbconnection(strconnection);
myconnection.open();
oledbdataadapter myadpater = new oledbdataadapter(strcommandtext,myconnection);
oledbcommandbuilder mybuilder = new oledbcommandbuilder(myadpater);
dataset mydataset = new dataset();
myadpater.fill(mydataset);
myconnection.close();
return mydataset;
}
///
/// 取得表
///
/// 查询语句
///
public datatable getdatatable(string commandtext)
{
string strconnection = "provider=microsoft.jet.oledb.4.0; " +
"data source=" + server.mappath(datapatch);
string strcommandtext =commandtext;
oledbconnection myconnection = new oledbconnection(strconnection);
myconnection.open();
oledbdataadapter myadpater = new oledbdataadapter(strcommandtext,myconnection);
oledbcommandbuilder mybuilder = new oledbcommandbuilder(myadpater);
dataset mydataset = new dataset();
myadpater.fill(mydataset);
datatable mytable = mydataset.tables[0];
myconnection.close();
return mytable;
}
///
/// 取得某行的某列的值
///
/// 列的名称
/// 所属表名
/// 表的主键
/// 列所属的主键值
///
public string getdatacolum(string strcolumnname,string strtablename,string strcolumnkey,string strcolumnvalue)
{
string strconnection = "provider=microsoft.jet.oledb.4.0; " +
"data source=" + server.mappath(datapatch);
string strcommandtext = "select "+strcolumnname+","+strcolumnkey+" from "+strtablename;
oledbconnection myconnection = new oledbconnection(strconnection);
myconnection.open();
oledbdataadapter myadpater = new oledbdataadapter(strcommandtext,myconnection);
oledbcommandbuilder mybuilder = new oledbcommandbuilder(myadpater);
dataset mydataset = new dataset();
myadpater.missingschemaaction = missingschemaaction.addwithkey;
myadpater.fill(mydataset);
myconnection.close();
datatable mytable = mydataset.tables[0];
datarow mydr = mytable.rows.find(strcolumnvalue);
string mydc = mydr[strcolumnname].tostring();
return mydc;
}
///
/// 更新某个字段
///
/// 要更新字段名称
/// 要更新的值
/// 所属表名称
/// 表中key
/// 表中key的值
public void updatecolum(string strcolumnname,string strvalue,string strtablename,string strcolumnkey,string strcolumnvalue)
{
string strconnection = "provider=microsoft.jet.oledb.4.0; " +
"data source=" + server.mappath(datapatch);
string strcommandtext = "select "+strcolumnkey+","+strcolumnname+" from "+strtablename;
oledbconnection myconnection = new oledbconnection(strconnection);
myconnection.open();
oledbdataadapter myadpater = new oledbdataadapter(strcommandtext,myconnection);
oledbcommandbuilder mybuilder = new oledbcommandbuilder(myadpater);
dataset mydataset = new dataset();
myadpater.missingschemaaction = missingschemaaction.addwithkey;
myadpater.fill(mydataset,strtablename);
datarow drfindrow = mydataset.tables[strtablename].rows.find(strcolumnvalue);
drfindrow[strcolumnname] = strvalue;
myadpater.update(mydataset,strtablename);
myconnection.close();
}
///
/// 添加行
///
/// 此行中的字段集合
/// 此行中的字段集合的对应值
/// 所属表名称
/// 表中主键
public void addrow(string[]columns,string[]columnvalue,string strtablename,string strcolumnkey)
{
string strconnection = "provider=microsoft.jet.oledb.4.0; " +
"data source=" + server.mappath(datapatch);
string strcolumncount = "";
foreach(string thiscolunmname in columns)
{
strcolumncount = thiscolunmname+","+strcolumncount;
}
string strcommandtext = "select "+strcolumncount+strcolumnkey+" from "+strtablename;
oledbconnection myconnection = new oledbconnection(strconnection);
myconnection.open();
oledbdataadapter myadpater = new oledbdataadapter(strcommandtext,myconnection);
oledbcommandbuilder mybuilder = new oledbcommandbuilder(myadpater);
dataset mydataset = new dataset();
myadpater.fill(mydataset,strtablename);
datarow newrow = mydataset.tables[strtablename].newrow();
for(int i=0;i<columns.length;i++)
{
string strcolumnname = columns[i].tostring();
newrow[strcolumnname] = columnvalue[i].tostring();
}
mydataset.tables[strtablename].rows.add(newrow);
myadpater.update(mydataset,strtablename);
myconnection.close();
}
///
/// 更新数据行
///
/// 要更新的列集合
/// 要更新的列集合的对应值
/// 所属表名称
/// 表主键
/// 行所属的id
public void updaterow(string[]columns,string[]columnvalue,string strtablename,string strcolumnkey,string strcolumnvalue)
{
string strconnection = "provider=microsoft.jet.oledb.4.0; " +
"data source=" + server.mappath(datapatch);
string strcolumncount = "";
foreach(string thiscolunmname in columns)
{
strcolumncount = thiscolunmname+","+strcolumncount;
}
string strcommandtext = "select "+strcolumncount+strcolumnkey+" from "+strtablename;
oledbconnection myconnection = new oledbconnection(strconnection);
myconnection.open();
oledbdataadapter myadpater = new oledbdataadapter(strcommandtext,myconnection);
oledbcommandbuilder mybuilder = new oledbcommandbuilder(myadpater);
dataset mydataset = new dataset();
myadpater.missingschemaaction = missingschemaaction.addwithkey;
myadpater.fill(mydataset,strtablename);
datarow updaterow = mydataset.tables[strtablename].rows.find(strcolumnvalue);
for(int i=0;i<columns.length;i++)
{
string strcolumnname = columns[i].tostring();
updaterow[strcolumnname] = columnvalue[i].tostring();
}
myadpater.update(mydataset,strtablename);
myconnection.close();
}
///
/// 删除行
///
/// 所属表
/// 主键字段名
/// 此行主键值
public void delectrow(string strtablename,string strcolumnkey,string strcolumnvalue)
{
string strconnection = "provider=microsoft.jet.oledb.4.0; " +
"data source=" + server.mappath(datapatch);
string strcommandtext = "select "+strcolumnkey+" from "+strtablename;
oledbconnection myconnection = new oledbconnection(strconnection);
myconnection.open();
oledbdataadapter myadpater = new oledbdataadapter(strcommandtext,myconnection);
oledbcommandbuilder mybuilder = new oledbcommandbuilder(myadpater);
dataset mydataset = new dataset();
myadpater.missingschemaaction = missingschemaaction.addwithkey;
myadpater.fill(mydataset,strtablename);
datarow newrow = mydataset.tables[strtablename].rows.find(strcolumnvalue);
newrow.delete();
myadpater.update(mydataset,strtablename);
myconnection.close();
}
//----------------------------------------------------------------------------------
新闻热点
疑难解答
图片精选