新近写了个通用的数据库访问类(c#)
2024-07-21 02:17:58
供稿:网友
新近写了个通用的数据库访问类(c#);支持oledb/sql/oracle/odbc数据连接,
提供sysbase,db2 的接口支持,
如果有什么错误、意见、建议。请联系我[email protected]
http://mail.gongyi.gov.cn/netdisk/[email protected]&id=2712a90f0de399014bfbea6e697381a3&filename=l7mk1/cvtxlqre8ucmfy&language=gb
经过不完全测试:
测试代码:
using system;
using system.collections;
using system.io;
using nunit.framework;
using mypdo.dataaccess;
namespace dataaccesstest
{
/// <summary>
/// class1 的摘要说明。
/// </summary>
[testfixture]
public class dataaccesstest
{
public dataaccesstest()
{
//
// todo: 在此处添加构造函数逻辑
//
}
private mypdo.dataaccess.dataaccessor dba;
[setup]
public void setup()
{
dba = new dataaccessor();
dba.connectstring = "provider=sqloledb;data source=10.10.25.63;initial catalog=master;user id=zha;password=123;";
dba.connecttype = mypdo.connecttype.oledbdatadriver;
dba.open();
assert.istrue(dba.isopen,"dba closed");
}
[test]
public void executenonquerytest()
{
hashtable hstable = new hashtable();
hstable.add("a","select * from myfiles");
dba.
assert.istrue(dba.isopen,"dba closed");
if (dba.isopen == true)
{
dba.executenonquery(hstable);
}
}
[test]
public void getdatasettest()
{
system.data.dataset ds = new system.data.dataset();
assert.istrue(dba.isopen,"dba closed");
if (dba.isopen == true)
{
dba.commandtext = "select * from myfiles";
ds = dba.getdataset();
}
}
[test]
public void getdatatabletest()
{
assert.istrue(dba.isopen,"dba closed");
if (dba.isopen == true)
{
dba.commandtext = "select * from myfiles";
system.data.datatable dts = dba.getdatatable();
}
}
[test]
public void getdatareadertest()
{
assert.istrue(dba.isopen,"dba closed");
if (dba.isopen == true)
{
dba.commandtext = "select * from myfiles";
system.data.idatareader dtreader = dba.getdatareader();
dtreader.close();
}
}
[test]
public void executecommandtest()
{
assert.istrue(dba.isopen,"dba closed");
if (dba.isopen == true)
{
dba.commandtext = "select * from myfiles";
dba.executecommand(false);
}
}
[test]
public void setcommandparameterstest()
{
system.data.oledb.oledbparameter parms = new system.data.oledb.oledbparameter();
parms.parametername = "@path";
parms.oledbtype = system.data.oledb.oledbtype.varchar;
parms.size = 255;
parms.sourcecolumn = "path";
parms.value = "222";
system.data.oledb.oledbparameter parms1 = new system.data.oledb.oledbparameter();
parms1.parametername = "@fname";
parms1.oledbtype = system.data.oledb.oledbtype.varchar;
parms1.size = 255;
parms1.sourcecolumn = "fname";
parms1.value = "1";
mypdo.dataaccess.dataaccessor dba = new dataaccessor();
dba.connectstring = "provider=sqloledb;data source=10.10.25.63;initial catalog=master;user id=zha;password=123;";
dba.open();
if (dba.isopen == true)
{
dba.commandtext = "select * from myfiles where path = ? and fname = ?";
dba.setcommandparameter(parms,false);
dba.setcommandparameter(parms1,false);
system.data.dataset ds = dba.getdataset();
assert.areequal(1,ds.tables[0].rows.count);
dba.close();
}
}
[test]
public void allusetest()
{
this.executecommandtest();
this.executenonquerytest();
this.getdatareadertest();
this.getdatareadertestwithparamblob();
this.getdatareadertestwithparamstring();
this.getdatasettest();
this.getdatatabletest();
this.setcommandparameterstest();
}
[test]
public void getdatareadertestwithparamstring()
{
system.data.oledb.oledbparameter parms = new system.data.oledb.oledbparameter();
parms.parametername = "@path";
//parms.oledbtype = system.data.oledb.oledbtype.varchar;
//parms.size = 255;
//parms.sourcecolumn = "path";
parms.value = "eeee";
system.data.oledb.oledbparameter parms1 = new system.data.oledb.oledbparameter();
parms1.parametername = "@fname";
//parms1.oledbtype = system.data.oledb.oledbtype.varchar;
//parms1.size = 255;
//parms1.sourcecolumn = "fname";
parms1.value = "1";
assert.istrue(dba.isopen,"dba closed");
{
dba.commandtext = "select * from myfiles where path = ? and fname = ?";
dba.setcommandparameter(parms,false);
dba.setcommandparameter(parms1,false);
system.data.idatareader dtreader = dba.getdatareader();
dtreader.read();
string strpath = dtreader.getstring(0);
assert.areequal("eeee",strpath);
dtreader.close();
}
}
[test]
public void getdatareadertestwithparamblob()
{
system.data.oledb.oledbparameter parms = new system.data.oledb.oledbparameter();
parms.parametername = "@path";
//parms.oledbtype = system.data.oledb.oledbtype.numeric;
//parms.size = 8;
//parms.sourcecolumn = "c";
parms.value = 999;
system.data.oledb.oledbparameter parms1 = new system.data.oledb.oledbparameter();
parms1.parametername = "@fname";
//parms1.oledbtype = system.data.oledb.oledbtype.binary;
//parms1.size = 16;
//parms1.sourcecolumn = "a";
parms1.value = getphoto(@"c:/a.jpg");
//parms1.value = "111111111111111111111";
//system.data.oledb.oledbparameter parms2 = new system.data.oledb.oledbparameter()
assert.istrue(dba.isopen,"dba closed");
{
dba.commandtext = "insert into test(c,a) values(convert(money,?),?) ";
//dba.commandtext = "insert into myfiles(path,fname) values(?,?)";
dba.setcommandparameter(parms,false);
dba.setcommandparameter(parms1,false);
dba.executecommand(true);
}
}
public static byte[] getphoto(string filepath)
{
filestream fs = new filestream(filepath, filemode.open, fileaccess.read);
binaryreader br = new binaryreader(fs);
byte[] photo = br.readbytes((int)fs.length);
br.close();
fs.close();
return photo;
}
[test]
public void executestoredproceduretestonevalue()
{
if (dba.isopen == true)
{
dba.commandtext = "exec get_2 '222'";
object j = dba.executestoredprocedure(mypdo.storedproceduretype.onevalue);
string s = (string)j;
assert.areequal("222",s);
}
}
[test]
public void executestoredproceduretestnone()
{
if (dba.isopen == true)
{
dba.commandtext = "exec get_2 '222'";
object s = dba.executestoredprocedure(mypdo.storedproceduretype.none);
assert.areequal(null,s);
}
else
assert.areequal(null,"1");
}
[test]
public void executestoredproceduretestdataset()
{
if (dba.isopen == true)
{
dba.commandtext = "exec get_2 'ddddd'";
object s = dba.executestoredprocedure(mypdo.storedproceduretype.dataset);
system.data.dataset ds = (system.data.dataset)s;
if(ds.tables.count > 0)
{
system.data.datatable dt = new system.data.datatable();
dt = ds.tables[0];
int str = dt.rows.count;
assert.areequal(3,str);
}
else
assert.areequal(null,"s");
}
else
assert.areequal(null,"1");
}
}
}
作者blog:http://blog.csdn.net/yu_nan88/