程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#編寫ASP.NET組件

C#編寫ASP.NET組件

編輯:關於C語言

//訪問數據庫是每個程序員都想簡化的工作,通過組件來完成,新手也變高手...

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.APPSettings["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();
        } 
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved