I saw a "a simple database operation class" very feeling, but I often develop something is sometimes based on SQL Server, sometimes based on Oracle, so I often use OLE DB. Today, my heart is speaking, put me The database operation class is shared with you. I hope everyone will correct it.
Class code:
Using system; using system.data.oledb;
A summary description of Namespace dbclass {/// /// db_class. /// summary> public class Db_Class {public OleDbConnection Conn; // Constructors public Db_Class () {Conn = new OleDbConnection ( "Provider = SQLOLEDB; Server = (local); Pwd = 123456; UID = sa; Database = Test ");} // Open the data source link Public OLIDBCONNECTION DB_CONN () {conn.open (); return conn;} // Returns the DataReader dataset, the following SQL can dynamically generate public OLEDBDATAREADER DB_CREATEREADER (String SQL) {db_conn ); OleDbCommand cmd = new OleDbCommand (SQL, Conn); OleDbDataReader Rs = cmd.ExecuteReader (); return Rs; this.close ();} // return the DataReader data set, the following SQL procedure is stored public OleDbDataReader Db_CommandReader (string SQL) {Db_Conn (); OleDbCommand cmd = new OleDbCommand (SQL, Conn); cmd.CommandType = CommandType.StoredProcedure; OleDbDataReader Rs = cmd.ExecuteReader (); return Rs; this.close ();} // return the DataSet Data set public oledbdataset db_created, {db_conn (); OLEDBCommand cmd = new oledbcommand (SQL, conn); OLEDBDataAdapter Adpt = New OLEDBDataAdapter (cmd, conn); DataSet DS = New dataset (); adpt.fill (DS, "newTable"); return ds; this.close ();} // Return to data DataReader dataset, do not need to return data modification, delete You can use this function PUBLIC BOOL DB_EXECUTENONQUERY ( String sql) {db_conn (); OLEDBCommand cmd = new oledbcommand (SQL, Conn); try {cmd.executenonQuery (); return true;} catch {return false;} this.close ();} // Close data link PUBLIC The method of use of Void Close () {() {Conn ();}}} is as follows:
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls Using system.web.ui.htmlcontrols; Namespace dbclass {/// /// WebForm1 summary description. /// summary> public class WebForm1: System.Web.UI.Page {protected System.Web.UI.WebControls.DataGrid DataGrid1; protected System.Web.UI.WebControls.Button Button1; private void Page_Load (object sender, System.eventargs e) {// Place the user code here to initialize the page // String SQL = "Select * from sysfiles"; db_class db_class = new db_class (); DataGrid1.datasource = db_class.db_commandreader ("sp_tables"); // Use SQLServer's stored procedure. DataGrid1.databind ();
#Region Web Form Designer Generated Code Override Protected Void OnNit (Eventargs E) {// // Codegen: This call is required for the ASP.NET Web Form Designer. // InitializeComponent (); base.onit (e);} /// /// Designer Supports the required method - Do not use the code editor to modify the // / this method. /// summary> private void InitializeComponent () {this.Button1.Click = new System.EventHandler (this.Button1_Click); this.DataGrid1.SelectedIndexChanged = new System.EventHandler (this.DataGrid1_SelectedIndexChanged); this.Load = new system.eventhandler (this.page_load);
} #Endregion}}}
Oh, I first published a article on 9CBS. If there is any bad place, please refer to you, welcome to discuss with you. QQ: 171476439 Email: SOHO_SUPORT@163.com