Data Access Layer - Extension to DataTable

zhaozj2021-02-16  47

The operation of the database is actually very simple.

DataTable DT = New CustomDataTable ("MyTable1");

DataRow Dr = DT.GetNewRow (TRUE); DR [0] = "Sadfas";

DT.UPDATA (); gridbind ();

Operate with this lightly

///

/// customize the DataTable, encapsulation operation of the database, only the select statement processing /// public class CustomDataTable: DataTable {private SQLServer sqlServer; SqlDataAdapter sqlDataAdapter; // private CustomDataTable () { } private void Init (string SQLString) {sqlServer = SQLServer.Instance (); sqlDataAdapter = new SqlDataAdapter (SQLString, sqlServer.Connection); SqlCommandBuilder custCB = new SqlCommandBuilder (sqlDataAdapter); sqlServer.Connection.Open (); try {sqlDataAdapter. Fillschema (this, Schematype.Source); SqlDataAdapter.Fill (this);} finally {sqlser.connection.close ();}}

///

/// Constructor /// /// SELECT statement or table name public customDataTable (string sqlstring) {string SQL = SQLSTRING. Trim (). TouPper (); if (SQL.StartSwith ("SELECT")) {init (SQL);} else {init ("Select * from" SQL); this.tablename = TableName;}}

///

/// Constructor /// /// Table name /// condition Public CustomDataTable (String Tablename, String Condition) {String SQL = "SELECT * FROM" TABLENAME; if (Condition! = Null && Condition! = "&& condition.trim ()! =" "SQL =" Where " Condition; init (sql); this.tablename = Tablename;

///

/// Submit /// public void UpData () {try {sqlServer.Connection.Open (); sqlServer.BeginTransaction (); sqlDataAdapter.SelectCommand.Transaction = sqlServer.SqlTran; sqlDataAdapter .Update (this); sqlserver.committransaction ();} catch {sqlserver.rollbacktransaction (); throw;} finally {sqlserver.connection.close ();}} /// /// / /// flag, True means new row to the table, FALSE ANTVEE /// New Row public DataRow GetNewRow (Bool Flagadd) {DATAROW DR = this.NewRow (); if (flagadd) this.rows.add (dr); Return DR;} /// // / Add new row /// /// New row public void address {this.rows.add (dr);}

///

/// Get a row /// /// index /// line reference Public DataRow GetRow (int index) {return this.rows [index];

///

/// Get routine /// condition /// row set public dataroW [ ] GetRows (String Condition) {Return this.Select (Condition, Null, DataViewRowState.currentrows);

///

/// Delete one line /// /// public void deleterow (int index) {this.rows [index] .delete }

///

/// Delete row set /// condition public void deleterows (string condition) {Foreach (DataRow Dr in GetRows Dr.Delete ();} /// /// Remove all records in the table ////////mmary> public void delete () {Foreach (DataRow Dr in this.select (null, null, DataViewRowState.currentrows)) Dr.Delete ();} /// /// Settings primary key /// /// key public void setPrimaryKey Params string [] keys) {datacolumn [] DC = new datacolumn [keys.Length]; for (int i = 0; i

}

转载请注明原文地址:https://www.9cbs.com/read-21314.html

New Post(0)