C#编写ASP.NET组件

时间:2008-10-07 12:02:56  来源:第二电脑网上收集  作者:

  第二电脑网导读:.Data.OracleClient;namespace NetAdo{    public class NetDB    {        private string ConnStr="";  private OracleConnection OraConn;  public NetDB()  {        ...
  正文:

//访问数据库是每个程序员都想简化的工作,通过组件来完成,新手也变高手...

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.OracleClient;

namespace NetAdo
{
    public class NetDB
    {
        private string ConnStr="";
  private OracleConnection OraConn;

  public NetDB()
  {
            ConnStr = ConfigurationManager.Apphotoshop/ target=_blank class=infotextkey>psettings["Oracle9iConnStr"];
   this.OraConn=new OracleConnection();
   if(ConnStr==null)
   {
     throw new Exception("ConnectionString is Null");
   }
   this.OraConn.ConnectionString=ConnStr;
  }

  public string ConnString
  {
   get
   {
    return ConnStr;
   }
  }


  private void ConnectionPrepare(bool ifBegin)
  {
   //检查连接字符串
   if(OraConn.ConnectionString==null)
   {
    throw new Exception("OleDbConnection's ConnectionString is null,execute Init()");
               
   }
   //根据参数执行相关操作
   if(ifBegin==true)
   {
    if(OraConn.State==ConnectionState.Closed)
    {
     OraConn.Open();
    }
   }
   else
   {
    if(OraConn.State==ConnectionState.Open)
    {
     OraConn.Close();
    }
   }
  }


  public DataSet RunSqlReturnDS(string sqlString)
  {
   DataSet ds=new DataSet();
   try
   {
    ConnectionPrepare(true);
    OracleCommand Cmd=OraConn.CreateCommand();
    Cmd.CommandText=sqlString;
    OracleDataAdapter adapter=new OracleDataAdapter(Cmd);
    adapter.Fill(ds);
   }
   catch(Exception ex)
   {
    throw new Exception(ex.Message);           
   }
   finally
   {
    ConnectionPrepare(false);
   }
   return ds;
  }

  public int RunSql(string sqlString)
  {
   int RowCount=0;
   try
   {
    ConnectionPrepare(true);
    OracleCommand Cmd=OraConn.CreateCommand();
    Cmd.CommandText=sqlString;
    RowCount=(int)Cmd.ExecuteNonQuery();
   }
   catch(Exception ex)
   {
    throw new Exception(ex.Message);           
   }
   finally
   {
    ConnectionPrepare(false);
   }
   return RowCount;
  }
    }
}
 

//页面调用
using NetAdo;

public partial class _Default : System.Web.UI.Page
{
    NetDB nd = new NetDB();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Response.Write(nd.ConnString.ToString() + "<br>");

            DataSet objectSet = new DataSet();
            objectSet = nd.RunSqlReturnDS("select * from tblszman where rownum<=10");
            Response.Write(objectSet.Tables[0].Rows.Count.ToString() + "<br>");
            int s = nd.RunSql("drop table temptu");
            Response.Write(s.ToString() + "<br>");
        }
        catch (Exception ex)
        {
            Response.Write("错误信息:" + ex.Message);
            Response.End();
        } 
    }
}

"C#编写ASP.NET组件"由第二电脑网原创提供,转载请注明:http://www.002pc.com/master/College/Programming/ASP/2008-10-07/2880.html


关键字:

关于《C#编写ASP.NET组件》文章的评论

站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td