//访问数据库是每个程序员都想简化的工作,通过组件来完成,新手也变高手...
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();
}
}
}
新闻热点
疑难解答
图片精选