熱點推薦:
您现在的位置: 電腦知識網 >> 操作系統 >> Windows系統管理 >> 正文

一個簡單的數據庫操作類

2013-11-11 21:54:01  來源: Windows系統管理 

  類代碼
  
  using System;
  using SystemData;
  using SystemDataSqlClient;
  
  
  namespace exchange
  {
   public class MyDataOp
   {
   private String StrSql;
   private String StrConn;
   private SqlConnection Conn;
   public MyDataOp(String ss)
   {
    StrSql = ss;
    StrConn = SystemConfigurationConfigurationSettingsAppSettings[ConnectionString];
   }
  
   public SqlDataReader CreateReader()
   {
    Conn=new SqlConnection(StrConn);
    SqlCommand Comm=new SqlCommand(StrSqlConn);
    ConnOpen();
    SqlDataReader MyReader = CommExecuteReader();
    return MyReader;
   }
   public DataSet CreateDataSet()
   {
    Conn=new SqlConnection(StrConn);
    ConnOpen();
    SqlDataAdapter Adpt=new SqlDataAdapter(StrSqlConn);
    DataSet Ds=new DataSet();
    AdptFill(Ds);
    return Ds;
   }
   public SqlCommand ExecuteCommand()
   {
    Conn=new SqlConnection(StrConn);
    SqlCommand Comm=new SqlCommand(StrSqlConn);
    ConnOpen();
    CommExecuteNonQuery();
    return Comm;
   }
   public void Close()
   {
    ConnClose();
   }
     }
  }
  
  
  
  基本用法
  
  namespace exchange
  {
   using System;
   using SystemWebUI;
   using SystemWebUIWebControls;
   using SystemData;
   using SystemDataSqlClient;
   using SystemWeb;
   using SystemSecurityCryptography;
  
  
   public class newjoin : SystemWebUIUserControl
   {
   
   protected DataList newjoinlist;
   public newjoin()
   {
    thisInit += new SystemEventHandler(Page_Init);
   }
  
   protected void Page_Load(object sender SystemEventArgs e)
   {
    if(!IsPostBack)
    {
    LoadData();
    }
       }
   
   protected void LoadData()
   {
    String StrSql = select top idleft(corpname) as corpname from crop order by id desc;
    MyDataOp mdo = new MyDataOp(StrSql);
    DataSet ds = mdoCreateDataSet();
    newjoinlistDataSource = dsTables[]DefaultView;
    newjoinlistDataBind();
    mdoClose();
   }
  
   private void Page_Init(object sender EventArgs e)
   {
    InitializeComponent();
   }
   private void InitializeComponent()
   {  
    thisLoad += new SystemEventHandler(thisPage_Load);
   }
   }
  }
From:http://tw.wingwit.com/Article/os/xtgl/201311/9476.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.