asp.net
//访问数据库是每个程序员都想简化的工作,通过组件来完成,新手也变高手...
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();
}
}
}