首先需要一个Oracle操作类,OraDbHelper.cs,代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.OracleClient; /// <summary> ///OraDbHelper 的摘要说明 /// </summary> public class OraDbHelper { private string connectionString; /// <summary> /// 设置数据库字符串的连接 /// </summary> public string ConnectionString { set { connectionString = value; } } public OraDbHelper(string connectionString) { this.connectionString = connectionString; } #region ExecuteDataTable /// <summary> /// 执行一个查询,并返回结果集 /// </summary> /// <param name="commandText">要执行的查询SQL文本命令</param> /// <returns>返回查询结果集</returns> public DataTable ExecuteDataTable(string commandText) { return ExecuteDataTable(commandText, CommandType.Text, null); } /// <summary> /// 执行一个查询,并返回查询结果 /// </summary> /// <param name="commandText">要执行的SQL语句</param> /// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param> /// <returns>返回查询结果集</returns> public DataTable ExecuteDataTable(string commandText, CommandType commandType) { return ExecuteDataTable(commandText, commandType, null); } /// <summary> /// 执行一个查询,并返回查询结果 /// </summary> /// <param name="commandText">要执行的SQL语句</param> /// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param> /// <param name="parameters">PL-SQL 语句或存储过程的参数数组</param> /// <returns></returns> public DataTable ExecuteDataTable(string commandText, CommandType commandType, params OracleParameter[] parameters) { DataTable data = new DataTable();//实例化DataTable,用于装载查询结果集 using (OracleConnection connection = new OracleConnection(connectionString)) { using (OracleCommand command = new OracleCommand(commandText, connection)) { command.CommandType = commandType;//设置command的CommandType为指定的CommandType //如果同时传入了参数,则添加这些参数 if (parameters != null) { foreach (OracleParameter parameter in parameters) { command.Parameters.Add(parameter); } } //通过包含查询SQL的SqlCommand实例来实例化SqlDataAdapter OracleDataAdapter adapter = new OracleDataAdapter(command); adapter.Fill(data);//填充DataTable } } return data; } #endregion ExecuteDataTable #region ExecuteReader /// <summary> /// 将 CommandText 发送到 Connection 并生成一个 OracleDataReader。 /// </summary> /// <param name="commandText">要执行的查询SQL文本命令</param> /// <returns></returns> public OracleDataReader ExecuteReader(string commandText) { return ExecuteReader(commandText, CommandType.Text, null); } /// <summary> /// 将 CommandText 发送到 Connection 并生成一个 OracleDataReader。 /// </summary> /// <param name="commandText">要执行的SQL语句</param> /// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param> /// <returns></returns> public OracleDataReader ExecuteReader(string commandText, CommandType commandType) { return ExecuteReader(commandText, commandType, null); } /// <summary> /// 将 CommandText 发送到 Connection 并生成一个 OracleDataReader。 /// </summary> /// <param name="commandText">要执行的SQL语句</param> /// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param> /// <param name="parameters">Transact-SQL 语句或存储过程的参数数组</param> /// <returns></returns> public OracleDataReader ExecuteReader(string commandText, CommandType commandType, OracleCommand[] parameters) { OracleConnection connection = new OracleConnection(connectionString); OracleCommand command = new OracleCommand(commandText, connection); //如果同时传入了参数,则添加这些参数 if (parameters != null) { foreach (OracleCommand parameter in parameters) { command.Parameters.Add(parameter); } } connection.Open(); //CommandBehavior.CloseConnection参数指示关闭Reader对象时关闭与其关联的Connection对象 return command.ExecuteReader(CommandBehavior.CloseConnection); } #endregion ExecuteReader #region ExecuteScalar /// <summary> /// 从数据库中检索单个值(例如一个聚合值)。 /// </summary> /// <param name="commandText">要执行的查询PL-SQL文本命令</param> /// <returns></returns> public Object ExecuteScalar(string commandText) { return ExecuteScalar(commandText, CommandType.Text, null); } /// <summary> /// 从数据库中检索单个值(例如一个聚合值)。 /// </summary> /// <param name="commandText">要执行的SQL语句</param> /// <param name="commandType">要执行的查询语句的类型,如存储过程或者PL-SQL文本命令</param> /// <returns></returns> public Object ExecuteScalar(string commandText, CommandType commandType) { return ExecuteScalar(commandText, commandType, null); } /// <summary> /// 从数据库中检索单个值(例如一个聚合值)。 /// </summary> /// <param name="commandText">要执行的SQL语句</param> /// <param name="commandType">要执行的查询语句的类型,如存储过程或者SQL文本命令</param> /// <param name="parameters">PL-SQL 语句或存储过程的参数数组</param> /// <returns></returns> public Object ExecuteScalar(string commandText, CommandType commandType, OracleParameter[] parameters) { object result = null; using (OracleConnection connection = new OracleConnection(connectionString)) { using (OracleCommand command = new OracleCommand(commandText, connection)) { command.CommandType = commandType;//设置command的CommandType为指定的CommandType //如果同时传入了参数,则添加这些参数 if (parameters != null) { foreach (OracleParameter parameter in parameters) { command.Parameters.Add(parameter); } } connection.Open();//打开数据库连接 result = command.ExecuteScalar(); } } return result;//返回查询结果的第一行第一列,忽略其它行和列 } #endregion ExecuteScalar #region ExecuteNonQuery /// <summary> /// 对数据库执行增删改操作 /// </summary> /// <param name="commandText">要执行的查询pl-sql文本命令</param> /// <returns></returns> public int ExecuteNonQuery(string commandText) { return ExecuteNonQuery(commandText, CommandType.Text, null); } /// <summary> /// 对数据库执行增删改操作 /// </summary> /// <param name="commandText">要执行的pl-sql语句</param> /// <param name="commandType">要执行的查询语句的类型,如存储过程或者pl-sql文本命令</param> /// <returns></returns> public int ExecuteNonQuery(string commandText, CommandType commandType) { return ExecuteNonQuery(commandText, commandType, null); } /// <summary> /// 对数据库执行增删改操作 /// </summary> /// <param name="commandText">要执行的pl-sql语句</param> /// <param name="commandType">要执行的查询语句的类型,如存储过程或者pl-sql文本命令</param> /// <param name="parameters">pl-sql 语句或存储过程的参数数组</param> /// <returns>返回执行操作受影响的行数</returns> public int ExecuteNonQuery(string commandText, CommandType commandType, OracleParameter[] parameters) { int count = 0; using (OracleConnection connection = new OracleConnection(connectionString)) { using (OracleCommand command = new OracleCommand(commandText, connection)) { command.CommandType = commandType;//设置command的CommandType为指定的CommandType //如果同时传入了参数,则添加这些参数 if (parameters != null) { foreach (OracleParameter parameter in parameters) { command.Parameters.Add(parameter); } } connection.Open();//打开数据库连接 count = command.ExecuteNonQuery(); } } return count;//返回执行增删改操作之后,数据库中受影响的行数 } #endregion ExecuteNonQuery }
cs代码中调用代码如下:
OraDbHelper dbHelper = new OraDbHelper(System.Configuration.ConfigurationManager.ConnectionStrings["OracleConnectionString"].ConnectionString.ToString()); string sql = "insert into SYIT_POSTINFORM " + " ( ZYXTYPE,GRADENM,TITLE,FBRNM,FBTM,YXTM,CONTS,FROMSR) values "; sql += "( '" + sclass + "'"; sql += ", '" + gwxx[1] + "'"; sql += ", '" + gwxx[0] + "'"; sql += ",' " + gwxx[2] + "'"; sql += ", to_date('" + gwxx[3] + "','yyyy/MM/DD')"; sql += ", to_date('" + gwxx[5] + "','yyyy/MM/DD')"; sql += ", :lgContent "; sql += ", '" + gwxx[4] + "'"; sql += " )"; OracleParameter[] parameters = { new OracleParameter("lgContent",OracleType.Clob) }; parameters[0].Value = gwxx[6]; dbHelper.ExecuteNonQuery(sql, CommandType.Text, parameters);