Web.page base class, encapsulated database operation and session processing

zhaozj2021-02-17  34

You are good, and you will be able to develop a system with .NET in this happily. I have made some satisfied packages. Please give some opinions. If you don't have a lot of time, there is not much time, I have to use the source Published in the form of code, please forgive me. There are related codes and comments, please download it yourself (Goto)

/ *********************************** * filename: wddb.cs * target: Processing Access Database Problem * Author: baihao * Createdate: 03/02/19 * lastmodify: 03/04/09 * history: * * *************************** ************* * /

Using system; using system.data.sqlclient; using wonder.sysconsole;

Namespace Wonder.Web.Data {///

/// WDDB summary description. /// public class dbObject {

private const string CONNNECT_STRING = "data source = 172.16.36.222; initial catalog = RemoteEdu;" "persist security info = False; user id = sa; password = 1234567890;" "packet size = 4096"; private string m_sErrorMessage = null ;

Protected SqlConnection Connection; // Protects connection private string connectionsTRING; // Private connection string private const string def_table = "table1";

Private sqlcommand command = new SQLCOMMAND ();

///

/// a parameterized constructor, IT allows us to take a connection /// String as a constructor argument, automatically instantiarating /// a new connection. /// /// Connection String to the associated database public dbObject (string newConnectionString) {connectionString = newConnectionString; Connection = new SqlConnection (connectionString); command.Connection = Connection;

}

public DbObject () //: base (/ * (SQLConnString.IniReadValue ( "catalog") == "") CONNNECT_STRING: * / SQLConnString.GetConnStr ()?) {connectionString = CONNNECT_STRING; Connection = new SqlConnection (connectionString); command .Connection = connection; // // TODO: Add constructor logic //} ///

/// /////// /// /// public object sendstr (string str) {if (str == null) Return dbnull.Value; else return str;}

///

/// Transfer Date type parameter to SQLCommand /// /// public object senddate (DATETIME DT) {IF (dt.ticks == 0) Return DBNULL.VALUE; Else Return Dt;}

///

/// Transfer characteristic data to SQLCommand /// /// /// public Object Sendchar (CHAR C) {IF (c == '/ 0') Return DBNULL.VALUE; Else Return C;} /// /// Transfer GUID type data to SQLCommand /// // / /// public object sendguid (guid c) {if (c == guid.empty) Return dbnull.Value; else return c; /// /// acquisition error Description /// /// public string getLastError () {return m_SerrorMessage;}

///

/// Clear the properties of the ERR object. /// public void errclear () {m_serrorMessage = "";

///

/// Protected property that exposes the connection string /// to inheriting classes Read-Only /// protected string ConnectionString {get {return connectionString;}}.. /// /// protected string /// TO inheriting classes. Read-only. /// protected sqlcommand command {get {returnire;}}

///

/// Open /// public void openconnect () {if (connection.State! = connectionState.closed) Connection.Close (); connection.open ();

///

/// Turns the connection, mainly using ////////////////////////////////>.State! = ConnectionsTate.Closed) Connection.Close ();}

///

// / execute the specified sql statement /// (ex: merinfo where userid = 'baihao'; ")) /// ** Need to turn on / off ** /// < / summary> public void execnoopen (string ssql) {command.commandtext = ssql; command.commandtype = commandType.text; command.executenon query ()

///

/// Execute Query statement, return DataReader /// ** Since DataRead is still queried, no disconnection, please pay attention ** // (EX: (Query " From memberinfo ")))) /// Do not need to open the connection, but need to close /// /// Query statement /// a newly instantiated SqlDataReader Object Public SqlDataReader Query (SQLDATAREADER RETURNREADER; TRY {OpenConnect ();

Command.commandType = commandType.text; command.commandtext = ssql;

ReturnReader = Command.executeReader (); //connection.close ();} catch (exception e) {m_serrorMessage = E.MESSAGE; RETURN NULL;} Return ReturnReader;

///

/// execute the SQL statement and return the query result, return a DataSet /// (ex: FillDataSet ("S: FillDataSet (" Select * from memberinfo ") /// // /// / Close connection /// /// SQL statement to execute /// public dataset fixdataset (string ssql) {Return FillDataSet (SSQL, DEF_TABLE);} /// // / Execute SQL statement and return query results /// (ex: filldataset ("SELECT * from memberinfo", "memberinfo") /// does not need to open / Turn off /// /// SQL statement to be executed /// Name in DataSet /// PUBLIC DATASET FILDATASET (STRING SSQL, String Stable) {DataSet DataSet; Try {Dataset = New DataSet (); OpenConnect ();

Sqldataadapter sqlda = new sqldataadapter (); sqlda.selectcommand = command; command.commandtext = ssql; command.commandtype = commandType.Text;

Int nrows = sqlda.fill (Dataset, Stable);

IF (nROWS == 0) Dataset = null;} catch (sqlexception e) {m_serrorMessage = E.MESSAGE; RETURN NULL;

} Return DataSet;

///

/// Execute SQL statement and return query results /// (EX: FillDataSet (DS, "Select * from MemberInfo", "MemberInfo")) // Add Table // / Do not need to open / close connections /// /// already existing DataSet, add table /// To execute SQL statement /// Returned DataSet "Name" Name /// Public Bool FillDataSet (Ref DataSet DS, String SSQL, String sTable) {bool bRe = true; try {openConnect (); SqlDataAdapter sqlDA = new SqlDataAdapter (); sqlDA.SelectCommand = command; command.CommandText = sSql; command.CommandType = CommandType.Text;

Int nrows = sqlda.fill (DS, stable);

IF (nROWS == 0) BRE = false;} catch (sqlexception e) {m_serrorMessage = E.MESSAGE; RETURN FALSE;}

Return BRE;} ///

/// execute the query, and return the first column of the first row of the result, ignore other rows and columns. /// ("Se: getfirstcolumn (" Select UserName from Meo ')) /// No Turmmary> //// /// < / param> /// public object getfirstcolumn (string ssql) {Try {Object ORE; OpenConnect (); command.commandtype = commandType.text; command.commandtext = ssql;

ORE = command.executescalar (); closeconnect ();

Return Ore;} catch (exception e) {m_serrorMessage = E.MESSAGE; RETURN NULL;}}

///

/// Execute the query, and return the results of the first column of the integer type, ignore other rows and columns. /// ("S:" "Select Count (*) from MemberInfo") /// No need to open / close connections /// /// /// Returns -1, indicating unsuccessful, otherwise successfully public int executeScalar (String SSQL) {Try {Int IRE; OpenConnect (); command.commandtype = commandType.Text; command.commandtext = ssql IRE = (int) Command.executescalar (); CloseConnect ();

Return IRE;} catch (exception e) {m_serrorMessage = E.MESSAGE; RETURN-1;}}

///

// / / ("delete Memberinfo where userid = 'baihao';") //// /// /// public void exec (String SSQL) {Try {OpenConnect (); command.commandtype = commandType.Text; command.commandtext = ssql;

Command.executenonQuery (); CloseConnect ();} catch (exception e) {m_serrorMessage = E.MESSAGE;}}}

///

/// Package SqlDataReader, mainly processing dbnull /// public class wdread {

public const byte NULL_INT = 0; public const string NULL_STR = ""; public static DateTime NULL_DATE = new DateTime (1,1,1); public const char NULL_CHAR = '/ 0'; public static Guid NULL_GUID = Guid.Empty; / //

/// /// public sqldataareader m_read = null;

///

/// Structure /// public wdread (SqlDataReader Read) {m_read = read;} ​​/// /// read a record /// /// public bool read () {return m_read.read ();} /// /// get a character String type /// /// field name /// field value or null_str public string gstring (String item) {IF ( m_read.isdbnull (m_read.getordinal (item))) Return Null_Str; Else Return M_Read.getstring (m_read.getordinal (item));}

///

/// acquired integer type /// /// field name /// field value or null Public int Gint (String Item) {if (m_read.isdbnull (m_read.getordinal (item))) Return Null_INT; Else Return M_Read.GetInt32 (M_Read.Getordinal (item));}

Public INT16 GWORD (STRING ITEM) {if (m_read.isdbnull (m_read.getrdinal (item))) Return Null_INT; Else Return M_Read.GetInt16 (M_Read.GetRinal (Item));

///

/// acquisition date type /// /// field name /// field value or null_date Public DateTime gdate (String item) {if (m_read.isdbnull (m_read.getrdinal (item))) Return Null_Date; else return m_read.getdatetime (m_read.getordinal (item));}

///

/// acquired byte type /// /// field name /// field value or null_int Public Byte Gbyte (String Item) {IF (m_read.getbnull (m_read.getrdinal (item))) Return Null_INT; Else Return M_Read.getbyte (m_read.getordinal (item));} /// /// Boolean Type /// /// field name /// field value or null_int public bool gbool (string item) {IF ( m_read.isdbnull (m_read.getordinal (item))) Return False; Else Return M_Read.getBoolean (M_Read.Getordinal (Item)); // (1 == m_read.getbyte (m_read.getordinal (item));}

///

/// gets char type /// /// field name /// field value or null_int Public char gchar (String Item) {if (m_read.isdbnull (m_read.getordinal (item))) Return Null_Char; Else Return Convert.TOCHAR (M_Read [item]);}

///

/// gets decimal type /// /// field name /// field value or null_int Public Decimal GDEC (String Item) {if (m_read.isdbnull (m_read.getrdinal (item))) Return Null_INT; Else Return M_Read.getDecimal (m_read.getordinal (item));}

///

/// gets a GUID type /// field name /// field value or null_guid Public Guid Gguid (String Item) {if (m_read.isdbnull (m_read.getrdinal (item))) Return Null_GUID; Else {Return M_Read.getGUID (m_read.getordinal (item));}}

///

/// gets the specified READ string /// /// /// ///

///

/// gets the specified READ string /// /// /// /// public static string gstr (SqlDataReader Read, String Item) {if (read.iead ()) {if (read.isdbnull (item)) Return NULL; Else Return READ.GETSTSTRING (Read.Getordinal (Item));

} Else return null;}}}

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

New Post(0)