In a project, you can use 2 kinds of data, such as Oracle, Ms_sqlserver also use, ms-sql data generic class in many tutorials have ready-made examples and code, but Oracle is relatively rare, but this project is just used, posted and shared with everyone.
This is where you use OraDbHelper.cs to do four of the usual data operations.
A executedatatable return to the memory datasheet
Two SqlDataReader direct reading data
Three ExecuteScalar to get a piece of data
Four ExecuteNonQuery to the database to perform additions and deletions to modify the operation
Environment: Client win XP is installed with Oracle 8.17 client
The database server is: Red Hat Enterprise Linux as installed Oracle database 10g
Copy Code code as follows:
Sql*plus:release 8.1.7.0.0-production on Saturday December 19 18:10:19 2009
(c) Copyright to Oracle Corporation. All rights reserved.
Connect to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0-production
With the partitioning, OLAP and Data Mining options
First OraDbHelper.cs
Copy Code code as follows:
Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.Data.SqlClient;
Using System.Data.OracleClient;
Namespace Xjy. DAL
{
<summary>
Oradbhelper Common classes for Oracle database operations
Shija of the author;
Date: 2009-12-18
</summary>
public class Oradbhelper
{
private string connectionString;
<summary>
To set a connection to a database string
</summary>
public string ConnectionString
{
set {connectionString = value;}
}
Public Oradbhelper (String connectionString)
{
this.connectionstring = connectionString;
}
#region executedatatable
<summary>
Executes a query and returns the result set
</summary>
<param name= "CommandText" > Query SQL Text command to execute </param>
<returns> return query result set </returns>
Public DataTable executedatatable (string commandtext)
{
Return executedatatable (CommandText, CommandType.Text, NULL);
}
<summary>
Executes a query and returns the results of the query
</summary>
<param name= "CommandText" > SQL statement to execute </param>
<param name= "CommandType" > type of query statement to execute, such as stored procedures or SQL text commands </param>
<returns> return query result set </returns>
Public DataTable executedatatable (string commandtext, CommandType commandtype)
{
Return Executedatatable (CommandText, CommandType, NULL);
}
<summary>
Executes a query and returns the results of the query
</summary>
<param name= "CommandText" > SQL statement to execute </param>
<param name= "CommandType" > type of query statement to execute, such as stored procedures or SQL text commands </param>
<param name= "parameters" >pl-sql statement or stored procedure parameter array </param>
<returns></returns>
Public DataTable executedatatable (string commandtext, CommandType commandtype, params oracleparameter[] parameters)
{
DataTable data = new DataTable ();//materialized DataTable for loading query result set
using (oracleconnection connection = new OracleConnection (connectionString))
{
using (OracleCommand command = new OracleCommand (commandtext, connection))
{
Command.commandtype = commandtype;//Set command commandtype to specified CommandType
If parameters are passed in at the same time, these parameters are added
if (Parameters!= null)
{
foreach (oracleparameter parameter in parameters)
{
Command. Parameters.Add (parameter);
}
}
Instantiate SqlDataAdapter by containing a SqlCommand instance of query sql
OracleDataAdapter adapter = new OracleDataAdapter (command);
Adapter. Fill DataTable with Fill (data);/
}
}
return data;
}
#endregion executedatatable
#region ExecuteReader
<summary>
Sends the CommandText to the Connection and generates a OracleDataReader.
</summary>
<param name= "CommandText" > Query SQL Text command to execute </param>
<returns></returns>
Public OracleDataReader ExecuteReader (string commandtext)
{
Return ExecuteReader (CommandText, CommandType.Text, NULL);
}
<summary>
Sends the CommandText to the Connection and generates a OracleDataReader.
</summary>
<param name= "CommandText" > SQL statement to execute </param>
<param name= "CommandType" > type of query statement to execute, such as stored procedures or SQL text commands </param>
<returns></returns>
Public OracleDataReader ExecuteReader (string commandtext, CommandType commandtype)
{
Return ExecuteReader (CommandText, CommandType, NULL);
}
<summary>
Sends the CommandText to the Connection and generates a OracleDataReader.
</summary>
<param name= "CommandText" > SQL statement to execute </param>
<param name= "CommandType" > type of query statement to execute, such as stored procedures or SQL text commands </param>
<param name= "parameters" >transact-sql statement or stored procedure parameter array </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 are passed in at the same time, these parameters are added
if (Parameters!= null)
{
foreach (oraclecommand parameter in parameters)
{
Command. Parameters.Add (parameter);
}
}
Connection. Open ();
The CommandBehavior.CloseConnection parameter indicates that the connection object associated with the reader object is closed when it is closed
return command. ExecuteReader (commandbehavior.closeconnection);
}
#endregion ExecuteReader
#region ExecuteScalar
<summary>
Retrieves a single value, such as an aggregate value, from the database.
</summary>
<param name= "CommandText" > Query pl-sql text command to execute </param>
<returns></returns>
Public Object ExecuteScalar (string commandtext)
{
Return ExecuteScalar (CommandText, CommandType.Text, NULL);
}
<summary>
Retrieves a single value, such as an aggregate value, from the database.
</summary>
<param name= "CommandText" > SQL statement to execute </param>
<param name= "CommandType" > type of query statement to execute, such as stored procedures or pl-sql text commands </param>
<returns></returns>
Public Object ExecuteScalar (string commandtext, CommandType commandtype)
{
Return ExecuteScalar (CommandText, CommandType, NULL);
}
<summary>
Retrieves a single value, such as an aggregate value, from the database.
</summary>
<param name= "CommandText" > SQL statement to execute </param>
<param name= "CommandType" > type of query statement to execute, such as stored procedures or SQL text commands </param>
<param name= "parameters" >pl-sql statement or stored procedure parameter array </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;//Set command commandtype to specified CommandType
If parameters are passed in at the same time, these parameters are added
if (Parameters!= null)
{
foreach (oracleparameter parameter in parameters)
{
Command. Parameters.Add (parameter);
}
}
Connection. Open ();//Opening database connection
result = command. ExecuteScalar ();
}
}
Return result;//returns the first row of the query result, ignoring other rows and columns
}
#endregion ExecuteScalar
#region ExecuteNonQuery
<summary>
Perform additions and deletions to the database
</summary>
<param name= "CommandText" > Query pl-sql text command to execute </param>
<returns></returns>
public int ExecuteNonQuery (string commandtext)
{
Return ExecuteNonQuery (CommandText, CommandType.Text, NULL);
}
<summary>
Perform additions and deletions to the database
</summary>
<param name= "CommandText" > Pl-sql statement to be executed </param>
<param name= "CommandType" > type of query statement to execute, such as stored procedures or pl-sql text commands </param>
<returns></returns>
public int ExecuteNonQuery (string commandtext, CommandType commandtype)
{
Return ExecuteNonQuery (CommandText, CommandType, NULL);
}
<summary>
Perform additions and deletions to the database
</summary>
<param name= "CommandText" > Pl-sql statement to be executed </param>
<param name= "CommandType" > type of query statement to execute, such as stored procedures or pl-sql text commands </param>
<param name= "parameters" >pl-sql statement or stored procedure parameter array </param>
<returns> returns the number of rows affected by the execution of the Operation </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;//Set command commandtype to specified CommandType
If parameters are passed in at the same time, these parameters are added
if (Parameters!= null)
{
foreach (oracleparameter parameter in parameters)
{
Command. Parameters.Add (parameter);
}
}
Connection. Open ();//Opening database connection
Count = command. ExecuteNonQuery ();
}
}
Return count;//returns the number of rows affected in the database after the execution of the delete or modify operation
}
#endregion ExecuteNonQuery
}
}
A test DataTable
Front Code
Copy Code code as follows:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> Untitled Page </title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:gridview id= "GridView1" runat= "Server" >
</asp:GridView>
</div>
</form>
</body>
Background code
Copy Code code as follows:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls; The
using XJY. DAL;
using System.Data.SqlClient;
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
Connectionstringsettings setting = system.configuration.configurationmanager.connectionstrings[" ConnectionString1 "];
String mystr = setting. ConnectionString;
Oradbhelper Myora = new Oradbhelper (MYSTR);
DataTable t1 = new DataTable ();
T1=myora. Executedatatable ("Select Art_no,descr from article where art_no<100");
Gridview1.datasource=t1;
Gridview1.databind ();
}
}
Results as shown
two Test SqlDataReader
The foreground code does not change
The background code is as follows:
Copy Code code as follows:
Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using XJY. DAL;
Using System.Data.SqlClient;
Using System.Data.OracleClient;
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
connectionstringsettings setting = system.configuration.configurationmanager.connectionstrings["ConnectionString1 "];
String mystr = setting. ConnectionString;
Oradbhelper Myora = new Oradbhelper (MYSTR);
OracleDataReader mydr = Myora. ExecuteReader ("Select Art_no,descr from article where art_no<100");
while (Mydr.read ())
{
Response.Write ("|" + mydr[0] + "|" + mydr[1]+ "
");
Response.Write ("____________________________________");
}
Mydr.close ();
Mydr.dispose ();
}
}
Results as shown
Three Test ExecuteScalar
Front Code
Copy Code code as follows:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> Untitled Page </title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:label id= "Label1" runat= "Server" text= "Label" ></asp:Label>
</div>
</form>
</body>
Background code
Copy Code code as follows:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls; The
using XJY. DAL;
using System.Data.SqlClient;
using System.Data.OracleClient;
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
Connectionstringsettings setting = system.configuration.configurationmanager.connectionstrings[" ConnectionString1 "];
String mystr = setting. ConnectionString;
Oradbhelper Myora = new Oradbhelper (MYSTR);
String Shuliang;
Shuliang = Myora. ExecuteScalar ("SELECT count (art_no) from article where art_no<100"). ToString ();
Label1.Text = Shuliang;
}
}
The result is as shown in figure:
Four Test ExecuteNonQuery
Foreground code:
Copy Code code as follows:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> Untitled Page </title>
<body>
<form id= "Form1" runat= "Server" >
<div> <asp:label id= "Label1" runat= "Server" text= "Label" ></asp:Label>
<asp:gridview id= "GridView1" runat= "Server" >
</asp:GridView>
<asp:label id= "Label2" runat= "Server" text= "Label" ></asp:Label>
<asp:gridview id= "GridView2" runat= "Server" >
</asp:GridView>
<asp:button id= "Button1" runat= "Server" onclick= "Button1_Click" text= "Recovery value"/>
</div>
</form>
</body>
Background code
Copy Code code as follows:
Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using XJY. DAL;
Using System.Data.SqlClient;
Using System.Data.OracleClient;
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
connectionstringsettings setting = system.configuration.configurationmanager.connectionstrings["ConnectionString1 "];
String mystr = setting. ConnectionString;
Oradbhelper Myora = new Oradbhelper (MYSTR);
DataTable T1 = new DataTable ();
Label1.Text = "value before Update";
T1 = Myora. Executedatatable ("Select Art_no,descr from article where art_no=8");
Gridview1.datasource = T1;
Gridview1.databind ();
if (Myora. ExecuteNonQuery ("Update article set descr= ' update describes test value ' where art_no=8") >0)
{
Label2.Text = "Updated value";
T1 = Myora. Executedatatable ("Select Art_no,descr from article where art_no=8");
Gridview2.datasource = T1;
Gridview2.databind ();
}
}
protected void Button1_Click (object sender, EventArgs e)
{
connectionstringsettings setting = system.configuration.configurationmanager.connectionstrings["ConnectionString1 "];
String mystr = setting. ConnectionString;
Oradbhelper Myora = new Oradbhelper (MYSTR);
DataTable T1 = new DataTable ();
if (Myora. ExecuteNonQuery ("Update article set descr= ' Poor poor 60ml/bottle ' where art_no=8") > 0)
{
Label2.Text = "Updated value";
T1 = Myora. Executedatatable ("Select Art_no,descr from article where art_no=8");
Gridview2.datasource = T1;
Gridview2.databind ();
}
}
}
Results: