我写了两个类
我自己写了两个类用于数据库的访问,这两个类是我觉得没必要为每个数据库的表写一个类。
这段代码可能可能会让您取笑,但希望您提出您的宝贵意见,或给个更好的这样的类的例子。
using system;
using system.data;
using system.data.sqlclient;
using system.collections;
namespace dblib
{
/// <summary>
///
/// </summary>
public class db
{
public static string connectstring =
"data source=.;initial catalog=zjs;persist security info=false;user id=sa;workstation id=tulip-river;packet size=4096";
public db()
{
//
// todo: 在此处添加构造函数逻辑
//
}
internal static sqlconnection getdbconnect()
{
return new sqlconnection(connectstring);
}
public static datatable getdatatable(string sql)
{
datatable dt = null;
sqlconnection cn = getdbconnect();
cn.open();
try
{
sqlcommand scmd = new sqlcommand(sql,getdbconnect());
sqldataadapter sda = new sqldataadapter();
sda.selectcommand = scmd;
dataset ds = new dataset();
sda.fill(ds,"temp");
dt = ds.tables["temp"];
}
catch(exception ee)
{
throw ee;
}
finally
{
cn.close();
}
return dt;
}
public static object getvalue(string sql)
{
object o = null;
sqlconnection cn = getdbconnect();
cn.open();
try
{
sqlcommand scmd = new sqlcommand(sql,cn);
o = scmd.executescalar();
}
catch(exception ee)
{
throw ee;
}
finally
{
cn.close();
}
return o;
}
public static void executeproc(string procname,params sqlparameter[] paras)
{
sqlconnection cn = getdbconnect();
cn.open();
try
{
sqlcommand scmd = new sqlcommand(procname,cn);
scmd.commandtype = commandtype.storedprocedure;
foreach(sqlparameter p in paras)
{
scmd.parameters.add(p);
}
scmd.executenonquery();
}
catch(exception ee)
{
throw ee;
}
finally
{
cn.close();
}
}
public static void executeupdate(string sql)
{
sqlconnection cn = getdbconnect();
cn.open();
try
{
sqlcommand scmd = new sqlcommand(sql,cn);
scmd.executenonquery();
}
catch(exception ee)
{
throw ee;
}
finally
{
cn.close();
}
}
}
}
////////////////////////////////////////////
using system;
using system.data;
namespace dblib
{
/// <summary>
/// tableaccessor 的摘要说明。
/// </summary>
public class tableaccessor
{
public tableaccessor(string tablename , string idfieldname)
{
this.tablename = tablename;
this.idfieldname = idfieldname;
id = 0;
}
protected long id;
protected string tablename;
protected string idfieldname;
public long id
{
get
{
return id;
}
set
{
id = value;
}
}
public string tablename
{
get
{
return tablename;
}
set
{
tablename = value;
}
}
public string idfieldname
{
get
{
return idfieldname;
}
set
{
idfieldname = value;
}
}
public object getvalue(string fieldname)
{
return db.getvalue("select " + fieldname + " from [" + tablename + "]" + " where " + idfieldname + "=" + id );
}
public object getvalue(string fieldname , string condition)
{
return db.getvalue("select " + fieldname + " from [" + tablename + "]" + " where " + condition );
}
public object getvalue(string fieldname , long id)
{
return db.getvalue("select " + fieldname + " from [" + tablename + "]" + " where " + idfieldname + "=" + id );
}
public datarow getdatarow()
{
datarow dr = null;
if(db.getdatatable("select * from " + tablename +" where " + idfieldname + "=" + id ).rows.count==0)
{
throw new exception("没有符合条件的记录。");
}
dr = db.getdatatable("select * from " + tablename +" where " + idfieldname + "=" + id ).rows[0];
return dr;
}
public system.data.datarow getdatarow(long id)
{
datarow dr = null;
if(db.getdatatable("select * from " + tablename +" where " + idfieldname + "=" + id ).rows.count==0)
{
throw new exception("没有符合条件的记录。");
}
dr = db.getdatatable("select * from " + tablename +" where " + idfieldname + "=" + id ).rows[0];
return dr;
}
public datatable getdatatable()
{
datatable dt = null;
dt = db.getdatatable("select * from [" + tablename + "]" );
return dt;
}
public datatable getdatatable(string condition)
{
datatable dt = null;
dt = db.getdatatable("select * from [" + tablename + "] " + condition );
return dt;
}
public datatable getdatatable(long id)
{
datatable dt = null;
dt = db.getdatatable("select * from [" + tablename +"] where " + idfieldname + "=" + id );
return dt;
}
public void update(string updatefieldandvaluelist)
{
db.executeupdate("update [" + tablename + "] set " + updatefieldandvaluelist + " where " + idfieldname + "=" + id);
}
public void update(string updatefieldandvaluelist , long id)
{
db.executeupdate("update [" + tablename + "] set " + updatefieldandvaluelist + " where " + idfieldname + "=" + id);
}
public void update(string updatefieldandvaluelist , string condition)
{
db.executeupdate("update [" + tablename + "] set " + updatefieldandvaluelist + " where " + condition);
}
public void insert(string insertfieldlist , string newvaluelist)
{
db.executeupdate("insert into [" + tablename + "] (" + insertfieldlist + ") values ( " + newvaluelist + " )");
}
public void delete(long id)
{
db.executeupdate("delete " + tablename + "] where " + idfieldname + "=" + id);
}
public void delete()
{
db.executeupdate("delete " + tablename + "] where " + idfieldname + "=" + id);
}
public void delete(string condition)
{
db.executeupdate("delete " + tablename + "] where " + condition);
}
}
}
推荐阅读
鼓励
hao
up
学习一下。
我正在学习用visio建模,以前只注意成果而没有注意过程。
http://www.lostinet.com/files/sqlscope.rar


讨论区