当前位置:首页 » 多媒体相关

我写了两个类


我自己写了两个类用于数据库的访问,这两个类是我觉得没必要为每个数据库的表写一个类。      
     
  这段代码可能可能会让您取笑,但希望您提出您的宝贵意见,或给个更好的这样的类的例子。  
   
  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);  
  }  
  }  
  }  
 

推荐阅读

  • “中国式”的B2B自救需转换商业模式 信息化频道 [详细内容]
  • 谨慎误入歧途 数码摄像机选购注意四点 [详细内容]
  • 诺基亚CDMA手机省电秘籍 [详细内容]
  • 去年北京电子商务交易额达1271亿元 信息化频道 [详细内容]
  • Photoshop吸管工具颜色取样技巧 [详细内容]
  • 关于WisMencoder的设置问题 [详细内容]
  • 三类企业:不同的长尾命运 信息化频道 [详细内容]
  • 网友回答:
    网友:soulroom

    鼓励

    网友:suhuoqiang

    hao

    网友:gweidian

    up

    网友:half_zhao

    学习一下。  
      我正在学习用visio建模,以前只注意成果而没有注意过程。

    网友:levinforum

    http://www.lostinet.com/files/sqlscope.rar  
     

    .

    讨论区

    Login