如下调用封装类:
using System.Data.OracleClient;
string strSQL = "insert into test(id) values(:id)";
OracleParameterCollection Parameters = new OracleParameterCollection();
Parameters.Add("id",OracleType.Number).Value=10; //10为要插入库中的值
int RowsAffected = BaseFunctions.ExecuteOraSql(strSQL,Parameters);
在封装类中定义如下成员函数:
using System.Data.OracleClient;
public class BaseFunctions
{
............................
public static int ExecuteOraSql(string strSQL,OracleParameterCollection Parameters)
{
OracleConnection OraConn = new OracleConnection();
OraConn.ConnectionString=OracleConnString;
OracleCommand OraCmd= new OracleCommand(strSQL,OraConn);
foreach(OracleParameter op in Parameters)
{
if(op.OracleType == OracleType.Char)
{
OraCmd.Parameters.Add(op.ParameterName,OracleType.Char).Value = Convert.ToByte(op.Value);
}
else
{
if(op.OracleType == OracleType.VarChar)
{
op.Value = op.Value == null ? OracleString.Null:op.Value;
OraCmd.Parameters.Add(op.ParameterName,op.DbType).Value = op.Value;
}
else
OraCmd.Parameters.Add(op.ParameterName,op.OracleType).Value = op.Value;
}
}
try
{
OraCmd.Connection.Open();
int numRowsAffected = OraCmd.ExecuteNonQuery();
return numRowsAffected;
}
catch( System.Data.OracleClient.OracleException e)
{
throw new Exception(e.Message);
}
finally
{
OraCmd.Dispose();
OraCmd.Connection.Close();
}
}
.................................
}