|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
归根到底,Java跨平台可以,但是要重新编写代码,否则还分什么J2EE/J2SE/J2ME呢!oracle/***********************************************************************
*Module:OraHelper.cs
*Author:hellopj
*Purpose:DefinitionoftheClassOraHelper
***********************************************************************/usingSystem;
usingSystem.Data;
usingOracle.DataAccess.Client;
usingOracle.DataAccess.Types;
usingSystem.Collections;
usingSystem.Configuration;
namespaceOraHelper
{
publicabstractclassOraHelper
{
publicstaticreadonlystringCONN_STRING_NON_DTC=ConfigurationSettings.AppSettings["ConnStr"].Trim();
publicstaticOracleConnectionconn=newOracleConnection(CONN_STRING_NON_DTC);
publicstaticvoidOpenConnection()
{
if(conn.State!=ConnectionState.Open)
conn.Open();
}
publicstaticvoidCloseConnection()
{
if(conn.State==ConnectionState.Open)
conn.Close();
}
publicstaticDataSetgetDataSet(stringcmdText)
{
OracleDataAdapterAdapter=newOracleDataAdapter(cmdText,conn);
DataSetData=newDataSet();
Adapter.Fill(Data);
returnData;
}
publicstaticOracleDataReaderExecuteReader(stringcmdText)
{
OracleCommandcmd=newOracleCommand();
cmd.Connection=conn;
cmd.CommandText=cmdText;
OracleDataReaderrdr=cmd.ExecuteReader();
cmd.Parameters.Clear();
returnrdr;
}
publicstaticintExecuteNonQuery(stringcmdText)
{
try
{
OracleCommandcomm=newOracleCommand(cmdText,conn);
comm.ExecuteNonQuery();
return1;
}
catch
{
return0;
}
}
publicstaticvoidExecuteNonQuery(stringcmdText,OracleConnectionconn)
{
try
{
OracleCommandcomm=newOracleCommand(cmdText,conn);
comm.ExecuteNonQuery();
}
catch
{
}
}
publicstaticstringExecuteScalar(stringcmdText)
{
stringstatus="";
try
{
OracleCommandcomm=newOracleCommand(cmdText,conn);
status=comm.ExecuteScalar().ToString();
returnstatus;
}
catch(Exceptionerr)
{
//return"-1";
returnerr.Message.ToString();
}
}
privatestaticHashtableparmCache=Hashtable.Synchronized(newHashtable());
publicstaticintExecuteNonQuery(CommandTypecmdType,stringcmdText,paramsOracleParameter[]cmdParms)
{
OracleCommandcmd=newOracleCommand();
cmd.Connection=conn;
cmd.Parameters.Clear();
cmd.CommandText=cmdText;
foreach(OracleParameterparmincmdParms)
cmd.Parameters.Add(parm);
intval=cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
returnval;
}
publicstaticintExecuteNonQuery(OracleTransactiontrans,CommandTypecmdType,stringcmdText,paramsOracleParameter[]cmdParms)
{
OracleCommandcmd=newOracleCommand();
PrepareCommand(cmd,trans.Connection,trans,cmdType,cmdText,cmdParms);
intval=cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
returnval;
}
publicstaticOracleDataReaderExecuteReader(CommandTypecmdType,stringcmdText,paramsOracleParameter[]cmdParms)
{
OracleCommandcmd=newOracleCommand();
try
{
PrepareCommand(cmd,conn,null,cmdType,cmdText,cmdParms);
OracleDataReaderrdr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
returnrdr;
}
catch(Exceptione)
{
conn.Close();
throwe;
}
}
publicstaticobjectExecuteScalar(stringconnString,CommandTypecmdType,stringcmdText,paramsOracleParameter[]cmdParms)
{
OracleCommandcmd=newOracleCommand();
PrepareCommand(cmd,conn,null,cmdType,cmdText,cmdParms);
objectval=cmd.ExecuteScalar();
cmd.Parameters.Clear();
returnval;
}
publicstaticobjectExecuteScalar(CommandTypecmdType,stringcmdText,paramsOracleParameter[]cmdParms)
{
OracleCommandcmd=newOracleCommand();
PrepareCommand(cmd,conn,null,cmdType,cmdText,cmdParms);
objectval=cmd.ExecuteScalar();
cmd.Parameters.Clear();
returnval;
}
publicstaticintgetScalar(CommandTypecmdType,stringcmdText,paramsOracleParameter[]cmdParms)
{
OracleCommandcmd=newOracleCommand();
PrepareCommand(cmd,conn,null,cmdType,cmdText,cmdParms);
objectval=cmd.ExecuteScalar();
cmd.Parameters.Clear();
intnVal=Convert.ToInt32(val);
returnnVal;
}
publicstaticvoidCacheParameters(stringcacheKey,paramsOracleParameter[]cmdParms)
{
parmCache[cacheKey]=cmdParms;
}
publicstaticOracleParameter[]GetCachedParameters(stringcacheKey)
{
OracleParameter[]cachedParms=(OracleParameter[])parmCache[cacheKey];
if(cachedParms==null)
returnnull;
OracleParameter[]clonedParms=newOracleParameter[cachedParms.Length];
for(inti=0,j=cachedParms.Length;i<j;i++)
clonedParms[i]=(OracleParameter)((ICloneable)cachedParms[i]).Clone();
returnclonedParms;
}
privatestaticvoidPrepareCommand(OracleCommandcmd,OracleConnectionconn,OracleTransactiontrans,CommandTypecmdType,stringcmdText,OracleParameter[]cmdParms)
{
if(conn.State!=ConnectionState.Open)
conn.Open();
cmd.Connection=conn;
cmd.CommandText=cmdText;
cmd.CommandType=cmdType;
//if(trans!=null)
//cmd.Transaction=trans;
if(cmdParms!=null)
{
foreach(OracleParameterparmincmdParms)
cmd.Parameters.Add(parm);
}
}
publicstaticintGetID(stringSeqName)//失掉序列的nextval
{
OracleCommandcmd=newOracleCommand();
using(OracleConnectionconn=newOracleConnection(CONN_STRING_NON_DTC))
{
stringSqlText="select"+SeqName+".nextvalfromdual";
PrepareCommand(cmd,conn,null,CommandType.Text,SqlText,null);
intval=Convert.ToInt32(cmd.ExecuteScalar());
cmd.Parameters.Clear();
returnval;
}
}
}
}
我有个同学,他是搞Java的,他给我说“Java不是效率低,而是速度慢。”,我不是搞Java的,我实在想不透这句话的含义,难道执行速度不就是效率低吗?难道执行速度慢还成效率高了? |
|