. NET in DBHelper (Oracle edition)

Source: Internet
Author: User
Tags oracleconnection first row connectionstrings

The previous article said that the operation of SQL Server version, because now the company needs to use Oracle data, not before, through the online check to add some of their own actual verification affixed, mainly to facilitate their own view of the use, but also hope to help the need of friends;

The following is a connection string for the configuration file, provided that your Oracle configuration is OK

<connectionStrings>
<!--Oracle Connection string--
<add name= "Oracleconn" connectionstring= "Data source= (description= (address= (protocol=tcp) (HOST=127.0.0.1) (PORT =1521)) (Connect_data= (SERVICE_NAME=ORCL))); User Id=scott; Password=tiger; Unicode=true "providername=" System.Data.OracleClient "/>
</connectionStrings>
<appSettings>

This namespace is necessary using the System.Data.OracleClient; The birth is no, need to add a reference, you can be smart tips out

Public class Dboraclehelper
{
public static oraclecommand cmd = null,
public static OracleConnection conn = Null
public static string connstr = configurationmanager.connectionstrings["Oracleconn"]. ConnectionString;
Public Dboraclehelper ()
{}
#region Establish database Connection object
//<summary>
//Establish database connection
//</summary> ;
//<returns> Returns a connection to a database OracleConnection object </returns>
public static OracleConnection init ()
{
Try
{
conn = new OracleConnection (CONNSTR);
if (Conn. state! = ConnectionState.Open)
{
Conn. Open ();
}
}
catch (Exception e)
{
throw new Exception (e.message.tostring ());
}
Return conn;
}
#endregion

#region Set OracleCommand object
//<summary>
//Set OracleCommand object
//</summary>
//<par Am name= "cmd" >oraclecommand object </param>
//<param name= "cmdtext" > Command text </param>
//< param name= "cmdtype" > Command type </param>
//<param name= "cmdparms" > Parameter collection </param>
Private static void SetCommand (OracleCommand cmd,string cmdtext, CommandType cmdtype, oracleparameter[] cmdparms)
{
cmd. Connection = conn;
Cmd.commandtext = Cmdtext;
Cmd.commandtype = Cmdtype;
if (cmdparms! = null)
{
cmd. Parameters.addrange (cmdparms);
}
}
#endregion

#region Execute the appropriate SQL statement, return the corresponding DataSet object
//<summary>
///execute the appropriate SQL statement to return the corresponding DataSet object
///</summary
//<param name= "sqlstr" >sql statement </param>
//<returns> returns the corresponding DataSet object </returns>
public static DataSet GetDataSet (String sqlstr)
{
DataSet set = new DataSet ();
Try
{
init ();
OracleDataAdapter ADP = new OracleDataAdapter (SQLSTR, conn);
ADP. Fill (set);
Conn. Close ();
}
catch (Exception e)
{
throw new Exception (e.message.tostring ());
}
return set;
}
#endregion

#region Execute the appropriate SQL statement, return the corresponding DataSet object
//<summary>
///execute the appropriate SQL statement to return the corresponding DataSet object
///</summary
//<param name= "sqlstr" >sql statement </param>
//<param name= "tableName" > table name </param>
//<returns> returns the corresponding DataSet object </returns>
public static DataSet GetDataSet (String sqlstr,string TableName)
{
DataSet set = new DataSet ();
Try
{
init ();
OracleDataAdapter ADP = new OracleDataAdapter (SQLSTR, conn);
ADP. Fill (Set,tablename);
Conn. Close ();
}
catch (Exception e)
{
throw new Exception (e.message.tostring ());
}
return set;
}
#endregion

#region executes the SQL statement with no arguments, returns the number of rows affected
//<SUMMARY>
///execute without parameter SQL statement, returns the number of rows affected
//</SUMMARY>
// Lt;param name= "Cmdstr" > increase, delete, change the SQL statement </param>
//<returns> Returns the number of rows affected </returns>
Public static int ExecuteNonQuery (string cmdtext)
{
int count;
Try
{
init ();
cmd = new OracleCommand (CMDT EXT, conn);
Count = cmd. ExecuteNonQuery ();
Conn. Close ();
}
catch (Exception ex)
{
throw new Exception (ex. Message.tostring ());
}
return count;
}
#endregion

#region Execute a parameterized SQL statement or stored procedure that returns the number of rows affected
//<SUMMARY>
///execute a parameterized SQL statement or stored procedure, returning the number of rows affected
//</summary>
//<param name= "cmdtext" > SQL statement with parameters and stored procedure name </param>
//<param name= "cmdtype" > Command type </ Param>
//<param name= "cmdparms" > Parameter set </param>
///<returns> Returns the number of rows affected </returns>
public static int ExecuteNonQuery (string cmdtext, CommandType cmdtype, oracleparameter[] cmdparms)
{
int coun T
Try
{
init ();
cmd = new OracleCommand ();
SetCommand (cmd, Cmdtext, Cmdtype, cmdparms);
Count = cmd. ExecuteNonQuery ();
Cmd. Parameters.clear ();
Conn. Close ();
}
catch (Exception ex)
{
throw new Exception (ex. Message.tostring ());
}
return count;
}
#endregion

#region executes the SQL statement without arguments, returning a OracleDataReader object that reads data from the data source
<summary>
Executes the SQL statement without arguments, returning a OracleDataReader object that reads data from the data source
</summary>
<param name= "CMDSTR" > corresponding SQL statements </param>
<returns> returns a OracleDataReader object that reads data from a data source </returns>
public static OracleDataReader ExecuteReader (String cmdtext)
{
OracleDataReader reader;
Try
{
Init ();
cmd = new OracleCommand (CMDTEXT, conn);
reader = cmd. ExecuteReader (commandbehavior.closeconnection);

}
catch (Exception ex)
{
throw new Exception (ex. Message.tostring ());
}
return reader;
}
#endregion

The

#region executes an SQL statement or stored procedure with parameters, returning a OracleDataReader object that reads data from the data source
//<summary>
///Execute SQL statements or stored procedures with parameters. Returns a OracleDataReader object that reads data from a data source
//</summary>
//<param name= "Cmdtext" >sql statement or stored procedure name </param
//<param name= "cmdtype" > Command type </param>
//<param name= "cmdparms" > Parameter Collection </param>
///<returns> Returns a OracleDataReader object that reads data from the data source </returns>
public static OracleDataReader ExecuteReader (String cmdtext, CommandType cmdtype, oracleparameter[] cmdparms)
{
OracleDataReader reader;
Try
{
init ();
cmd = new OracleCommand ();
SetCommand (cmd, Cmdtext, Cmdtype, cmdparms);
reader = cmd. ExecuteReader (commandbehavior.closeconnection);
}
catch (Exception ex)
{
throw new Exception (ex. Message.tostring ());
}
return reader;
}
#endregion

The

#region executes the SQL statement with no arguments, returning the value of the first row column of the result set object
//<summary>
///execute without parameter SQL statement, return the value of the first row column of the result set object
///</ Summary>
//<param name= "CMDSTR" > corresponding SQL statement </param>
///<returns> Returns the value of the first row column of the result set object</ Returns>
public static Object ExecuteScalar (String cmdtext)
{
Object obj;
Try
{
init ();
cmd = new OracleCommand (CMDTEXT, conn);
obj = cmd. ExecuteScalar ();
Conn. Close ();
}
catch (Exception ex)
{
throw new Exception (ex. Message.tostring ());
}
return obj;
}
#endregion

#region executes a parameterized SQL statement or stored procedure that returns the value of the first row column of the result set object
<summary>
Executes a parameterized SQL statement or stored procedure that returns the value of the first row column of the result set object
</summary>
<param name= "Cmdtext" >sql statement or stored procedure name </param>
<param name= "cmdtype" > Command type </param>
<param name= "Cmdparms" > returns the value of the first header column of the result set object</param>
<returns></returns>
public static Object ExecuteScalar (String cmdtext, CommandType cmdtype, oracleparameter[] cmdparms)
{
Object obj;
Try
{
Init ();
cmd = new OracleCommand ();
SetCommand (cmd, Cmdtext, Cmdtype, cmdparms);
obj = cmd. ExecuteScalar ();
Conn. Close ();
}
catch (Exception ex)
{
throw new Exception (ex. Message.tostring ());
}
return obj;
}
#endregion



}

. NET in DBHelper (Oracle edition)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.