c#连接oracle数据库底层方法
using oracle.manageddataaccess.client;
using system;
using system.collections;
using system.collections.generic;
using system.data;
using system.data.oledb;
using system.linq;
using system.text;
using system.web;
/// <summary>
/// oracleconnection 的摘要说明
/// </summary>
public class oracleconn
{
public oracleconn()
{
//
// todo: 在此处添加构造函数逻辑
//
}
static string connstring = dbconnection.connstring;
public int getconn()
{
oracleconnection conn = new oracleconnection(connstring);
int result = 0;
try
{
conn.open();
result = 1;
}
catch (exception ex)
{
throw (ex);
}
finally
{
conn.close();
}
return result;
}
/// <summary>
/// 查询数据
/// </summary>
/// <returns></returns>
public datatable gettabledata()
{
datatable dt = new datatable(); //创建数据库表
using (oracleconnection con = new oracleconnection(connstring))
{
con.open(); //打开数据库链接
oraclecommand sqlcom = new oraclecommand(); //声明并创建数据库命令集
stringbuilder sqlstr = new stringbuilder(); //声明sql语句
sqlstr.append("select * from testtable order by id "); //获取sql语句
sqlcom.commandtext = sqlstr.tostring(); //为sqlcommand对象指定sql语句
sqlcom.connection = con; //为sqlcommand对象指定链接对象
oracledataadapter sqlda = new oracledataadapter(sqlcom); //声明数据库适配器
oraclecommandbuilder sqlbuilder = new oraclecommandbuilder(sqlda);
sqlda.fill(dt); //填充表
}
return dt;
}
/// <summary>
/// 查询数据
/// </summary>
/// <returns></returns>
public oraclecommand getdata()
{
//使用using语句进行数据库连接
using (oracleconnection sqlcon = new oracleconnection(connstring))
{
sqlcon.open(); //打开数据库连接
oraclecommand sqlcom = new oraclecommand(); //创建数据库命令对象
sqlcom.commandtext = "select * from testtable"; //为命令对象指定执行语句
sqlcom.connection = sqlcon; //为命令对象指定连接对象
oracledatareader reader = sqlcom.executereader();
while (reader.read())
{
reader.getint32(0);
}
sqlcon.close();
return sqlcom;
}
}
/// <summary>
/// 删除数据
/// </summary>
/// <param name="intid"></param>
public void deletedata(int intid)
{
using (oracleconnection con = new oracleconnection(connstring))
{
con.open(); //打开数据库连接
oraclecommand sqlcmd = new oraclecommand(); //创建数据库命令对象
sqlcmd.commandtext = "delete from testtable where id=@id"; //为命令对象指定执行语句
sqlcmd.connection = con; //为命令对象指定连接对象
//创建参数集合,并向sqlcom中添加参数集合
oracleparameter sqlparam = new oracleparameter("@id", intid);
sqlcmd.parameters.add(sqlparam);
sqlcmd.executenonquery(); //指定更新语句
}
}
/// <summary>
/// 修改数据
/// </summary>
/// <param name="intid"></param>
/// <param name="strtext"></param>
public void updatedata(int intid, string strtext)
{
using (oracleconnection con = new oracleconnection(connstring))
{
con.open(); //打开数据库连接
oraclecommand sqlcmd = new oraclecommand(); //创建数据库命令对象
sqlcmd.commandtext = "update testtable set name=:str where id=:id"; //为命令对象指定执行语句
sqlcmd.connection = con; //为命令对象指定连接对象
//创建参数集合,并向sqlcom中添加参数集合
oracleparameter[] sqlparam = { new oracleparameter(":str", strtext), new oracleparameter(":id", intid) };
sqlcmd.parameters.addrange(sqlparam);
sqlcmd.executenonquery(); //指定更新语句
con.close();
}
}
/// <summary>
/// 添加操作方法
/// </summary>
/// <param name="name"></param>
/// <param name="address"></param>
/// <param name="tel"></param>
/// <param name="remark"></param>
/// <returns></returns>
public int adddata(string name, string address, string tel, string remark)
{
oracleconnection conn = new oracleconnection(connstring);
try
{
conn.open();
oraclecommand cmd = new oraclecommand();
cmd.connection = conn;
cmd.commandtype = commandtype.text;
string sql = "insert into testtable (id,name,address,tel,remark)values (testtable_id_seq.nextval,'" + name + "','" + address + "','" + tel + "','" + remark + "')";
cmd.commandtext = sql;
int retval = cmd.executenonquery();
cmd.parameters.clear();
return retval;
}
catch (exception ex)
{
throw ex;
}
finally
{
conn.close();
}
}
/// <summary>
/// 添加操作方法
/// </summary>
/// <param name="ht"></param>
public void insertdata(hashtable ht)
{
//int id = convert.toint32(ht[":id"].tostring());
oracleconnection conn = new oracleconnection(connstring);
conn.open();
oraclecommand cmd = new oraclecommand();
cmd.connection = conn;
cmd.commandtype = commandtype.text;
string sql = "insert into testtable (id,name,address,tel,remark)values (testtable_id_seq.nextval,:name,:address,:tel,:remark)";
cmd.commandtext = sql;
oracleparameter[] param =
{
//new oracleparameter(":id",oracledbtype.int32),
new oracleparameter(":name",oracledbtype.varchar2),
new oracleparameter(":address",oracledbtype.varchar2),
new oracleparameter(":tel",oracledbtype.varchar2),
new oracleparameter(":remark",oracledbtype.varchar2)
};
//param[0].value = id;
param[0].value = ht[":name"].tostring();
param[1].value = ht[":address"].tostring();
param[2].value = ht[":tel"].tostring();
param[3].value = ht[":remark"].tostring();
foreach (oracleparameter p in param)
{
if (p != null)
{
if ((p.direction == parameterdirection.inputoutput ||
p.direction == parameterdirection.input) &&
(p.value == null))
{
p.value = dbnull.value;
}
cmd.parameters.add(p);
}
}
int retval = cmd.executenonquery();
cmd.parameters.clear();
conn.close();
}
/// <summary>
/// 查询表数据(单表)
/// </summary>
/// <param name="sql">sql语句</param>
/// <returns></returns>
public static dataset selectdata(string sql)
{
dataset ds = new dataset();
try
{
using (oracleconnection conn = new oracleconnection(connstring))
{
//conn.open();
oraclecommand cmd = new oraclecommand(sql, conn);
oracledataadapter adapter = new oracledataadapter(cmd);
adapter.fill(ds);
return ds;
}
}
catch (exception e)
{
console.write(e);
}
return null;
}
//增改删
/// <summary>
/// 执行sql 语句
/// </summary>
/// <param name="sql"></param>
/// <returns>返回影响的行数</returns>
public static boolean addupdeldata(string sql)
{
try
{
using (oracleconnection conn = new oracleconnection(connstring))
{
conn.open();
oraclecommand cmd = new oraclecommand(sql, conn);
int row = cmd.executenonquery();
conn.close();
if (row > 0)
{
return true;
}
}
}
catch (exception e)
{
console.write(e);
}
return false;
}
public static boolean exetransaction(list<string> sqltext)
{
using (oracleconnection conn = new oracleconnection(connstring))
{
conn.open();
oracletransaction tran = conn.begintransaction();
try
{
oraclecommand cmd = new oraclecommand();
cmd.transaction = tran;
cmd.connection = conn;
foreach (string item in sqltext)
{
cmd.commandtext = item;
cmd.executenonquery();
}
tran.commit();
return true;
}
catch (exception et)
{
tran.rollback();
return false;
}
finally
{
conn.close();
}
}
}
}
上一篇: CDR中如何等比例缩放图形?CDR图形等比缩放的两种方法
下一篇: MySQL-主从复制