发布网友 发布时间:2022-04-08 18:56
共4个回答
懂视网 时间:2022-04-08 23:17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using Oracle.DataAccess.Client;
namespace DAL
{
public class DBHelper
{
//public static string ConnectionString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;//方式一,数据库链接串写在配置文件中
public static string ConnectionString = "Data Source=ORCL;User ID=bonus;Password=bonus123";//方式二,直接写在cs文件中
#region 对于所有表 ExecuteNonQuery() 直接对数据进行更新操作
public static int ExecuteNonQuery(string sql)
{
return ExecuteNonQuery(sql, null, CommandType.Text);
}
public static int ExecuteNonQuery(string sql, OracleParameter[] para)
{
return ExecuteNonQuery(sql, para, CommandType.Text);
}
public static int ExecuteNonQuery(string sql, CommandType cmdType)
{
return ExecuteNonQuery(sql, null, cmdType);
}
public static int ExecuteNonQuery(string sql, OracleParameter[] para, CommandType cmdType)
{
int rows = -1;
using (OracleConnection conn = new OracleConnection(ConnectionString))
{
OracleCommand cmd = new OracleCommand(sql, conn);
if (para != null)
{
cmd.Parameters.AddRange(para);
}
cmd.CommandType = cmdType;
conn.Open();
rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
conn.Close();
}
return rows;
}
#endregion
#region 对于所有表 ExecuteReader 返回结果集
public static OracleDataReader ExecuteReader(string sql)
{
return ExecuteReader(sql, null, CommandType.Text);
}
public static OracleDataReader ExecuteReader(string sql, OracleParameter[] para)
{
return ExecuteReader(sql, para, CommandType.Text);
}
public static OracleDataReader ExecuteReader(string sql, CommandType cmdType)
{
return ExecuteReader(sql, null, cmdType);
}
public static OracleDataReader ExecuteReader(string sql, OracleParameter[] para, CommandType cmdType)
{
OracleCommand cmd = null;
OracleConnection conn = new OracleConnection(ConnectionString); ;
cmd = new OracleCommand(sql, conn);
if (para != null)
{
cmd.Parameters.AddRange(para);
}
cmd.CommandType = cmdType;
conn.Open();
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
#endregion
#region 对于所有表 ExecuteDataTable 返回数据表
public static DataTable ExecuteDataTable(string sql)
{
return ExecuteDataTable(sql, null, CommandType.Text);
}
public static DataTable ExecuteDataTable(string sql, OracleParameter[] para)
{
return ExecuteDataTable(sql, para, CommandType.Text);
}
public static DataTable ExecuteDataTable(string sql, CommandType cmdType)
{
return ExecuteDataTable(sql, null, cmdType);
}
public static DataTable ExecuteDataTable(string sql, OracleParameter[] para, CommandType cmdType)
{
try
{
DataTable myTable = new DataTable();
using (OracleConnection conn = new OracleConnection(ConnectionString))
{
OracleCommand cmd = new OracleCommand(sql, conn);
if (para != null)
{
cmd.Parameters.Add(para);
}
cmd.CommandType = cmdType;
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(myTable);
}
return myTable;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 对于所有表 ExecuteScalar 返回数据表
public static object ExecuteScalar(string sql)
{
return ExecuteScalar(sql, null, CommandType.Text);
}
public static object ExecuteScalar(string sql, OracleParameter[] para)
{
return ExecuteScalar(sql, para, CommandType.Text);
}
public static object ExecuteScalar(string sql, CommandType cmdType)
{
return ExecuteScalar(sql, null, cmdType);
}
public static object ExecuteScalar(string sql, OracleParameter[] para, CommandType cmdType)
{
object res = null;
using (OracleConnection conn = new OracleConnection(ConnectionString))
{
OracleCommand cmd = new OracleCommand(sql, conn);
if (para != null)
{
cmd.Parameters.AddRange(para);
}
cmd.CommandType = cmdType;
conn.Open();
res = cmd.ExecuteScalar();
cmd.Parameters.Clear();
conn.Close();
}
return res;
}
#endregion
#region 存储过程操作
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>OracleDataReader返回数据集</returns>
public static OracleDataReader RunProcedure(string storedProcName, IDataParameter[] parameters)
{
OracleConnection connection = new OracleConnection(ConnectionString);
OracleDataReader returnReader;
connection.Open();
OracleCommand command = BuildQueryCommand(connection, storedProcName, parameters);
command.CommandType = CommandType.StoredProcedure;
returnReader = command.ExecuteReader();
return returnReader;
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <param name="tableName">DataSet结果中的表名</param>
/// <returns>DataSet</returns>
public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName)
{
using (OracleConnection connection = new OracleConnection(ConnectionString))
{
DataSet dataSet = new DataSet();
connection.Open();
OracleDataAdapter sqlDA = new OracleDataAdapter();
sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
sqlDA.Fill(dataSet, tableName);
connection.Close();
return dataSet;
}
}
/// <summary>
/// 构建 OracleCommand 对象(用来返回一个结果集,而不是一个整数值)
/// </summary>
/// <param name="connection">数据库连接</param>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>OracleCommand</returns>
private static OracleCommand BuildQueryCommand(OracleConnection connection, string storedProcName, IDataParameter[] parameters)
{
OracleCommand command = new OracleCommand(storedProcName, connection);
command.CommandType = CommandType.StoredProcedure;
foreach (OracleParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
return command;
}
/// <summary>
/// 创建 OracleCommand 对象实例(存储过程返回输出参数)
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>OracleCommand 对象实例</returns>
/// 调用此方法的例子
/* OracleParameter[] parameter = {
new OracleParameter("starttime",OracleDbType.Varchar2,8),
new OracleParameter("endtime",OracleDbType.Varchar2,8),
new OracleParameter("days",OracleDbType.Decimal,3)
};
parameter[0].Direction = ParameterDirection.Input;
parameter[1].Direction = ParameterDirection.Input;
parameter[2].Direction = ParameterDirection.Output;
//new OracleParameter("t_sql", OracleType.Cursor)};
parameter[0].Value = starttime;
parameter[1].Value = endtime;
// parameter[2].Direction = System.Data.ParameterDirection.Output;
command = DBHelper.RunProcedureCommand("pro_comovertimedays", parameter);
//接收函数返回值
over_days = Convert.ToString(parameter[2].Value);//res
*/
public static OracleCommand RunProcedureCommand(string storedProcName, IDataParameter[] parameters)
{
using (OracleConnection connection = new OracleConnection(ConnectionString))
{
OracleCommand command = new OracleCommand(storedProcName, connection);
command.CommandType = CommandType.StoredProcedure;
foreach (OracleParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
connection.Open();
command.ExecuteNonQuery();
connection.Close();
return command;
}
}
#endregion
}
}
本文出自 “11075795” 博客,谢绝转载!
Oracle数据库驱动类DBHelper
标签:oracle数据库驱动类dbhelper
热心网友 时间:2022-04-08 20:25
选C热心网友 时间:2022-04-08 21:43
java操作数据库,没找到驱动一般有两个原因:
没有导入jar包,比如连接Oracle数据库,就需要导入Oracle官方提供的驱动包。
加载驱动的时候,Class.forName(String str)中的str写错了。
热心网友 时间:2022-04-08 23:18
C.ClassNotFoundException