//訪問數據庫是每個程序員都想簡化的工作,通過組件來完成,新手也變高手...
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();
}
}
}