Article Content for this site editing, creation. You can reprint, release, use, but please be sure to note the original source of the article in plaintext and the statement of http://www.opent.cn: Lang taosha
The method of this post will be updated continuously. The base class of this file to be referenced and operated by data
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;
Namespace EC
{
/// <Summary>
/// Basic class of EB universal and data interaction operations
/// </Summary>
Public class ebcommonobj: idisposable
{
Private bool _ alreadydispose = false;
Private dboperate DBO;
Private string SQL = NULL;
Private system. Data. dataset Ds;
# Region constructor and destructor
Public ebcommonobj ()
{
DBO = new dboperate ();
}
~ Ebcommonobj ()
{
DBO. Dispose ();
Dispose ();
}
Protected virtual void dispose (bool isdisposing)
{
If (_ alreadydispose) return;
If (isdisposing)
{
DBO. Dispose ();
}
_ Alreadydispose = true;
}
# Endregion
# Region idisposable Member
Public void dispose ()
{
Dispose (true );
GC. suppressfinalize (this );
}
# Endregion
# Region General Purpose: delete a record in the database
/// <Summary>
/// Delete a record in the database
/// </Summary>
/// <Param name = "TBL"> data table name </param>
/// <Param name = "inline"> Field name </param>
/// <Param name = "isint"> whether it is int type </param>
/// <Param name = "KV"> keyword value </param>
Public void commdelbyid (string TBL, string delimiter, bool isint, string key)
{
SQL = "delete from {0} Where {1} = ";
If (isint)
{
SQL + = "{3 }";
}
Else
{
SQL + = "'{3 }'";
}
DBO. executenonquery (string. Format (SQL, TBL, delimiter, isint, key ));
}
# Endregion
# Region generally reads a record from the database
/// <Summary>
/// Read a record from the database
/// </Summary>
/// <Param name = "TBL"> </param>
/// <Param name = "Callback"> </param>
/// <Param name = "isint"> </param>
/// <Param name = "key"> </param>
/// <Returns> </returns>
Public dataset commreadbyid (string TBL, string delimiter, bool isint, string key)
{
SQL = "select * from {0} Where {1} = ";
If (isint)
{
SQL + = "{3 }";
}
Else
{
SQL + = "'{3 }'";
}
DS = DBO. getdataset (string. Format (SQL, TBL, delimiter, isint, key ));
Return Ds;
}
# Endregion
# Region modify a record in the database to true or flase
/// <Summary>
/// Modify a record in the database to true or flase
/// </Summary>
/// <Param name = "TBL"> table </param>
/// <Param name = "regular"> primary key ID </param>
/// <Param name = "isint"> integer </param>
/// <Param name = "key"> Primary Key </param>
/// <Param name = "flg.pdf"> flase key </param>
/// <Param name = "flgkey"> key value </param>
Public void commupdatebyid (string TBL, string delimiter, bool isint, string key, string flgfld, int flgkey)
{
SQL = "Update {0} set {4 }={ 5} Where {1} = ";
If (isint)
{
SQL + = "{3 }";
}
Else
{
SQL + = "'{3 }'";
}
DBO. executenonquery (string. Format (SQL, TBL, delimiter, isint, key, flgfld, flgkey ));
}
# Endregion
# Region bind the dropdown list
/// <Summary>
/// Bind the dropdown list
/// </Summary>
/// <Param name = "TBL"> table name </param>
/// <Param name = "selvalue"> drop-down box value </param>
/// <Param name = "seltext"> content displayed in the drop-down list </param>
/// <Param name = "strwhere"> The where Condition Statement is null without the WHERE clause. </param>
/// <Param name = "Dr"> dropdownlist Control name </param>
Public void dropbind (string TBL, string selvalue, string seltext, string strwhere, system. Web. UI. webcontrols. dropdownlist Dr)
{
DS = getdrop (TBL, selvalue, seltext, strwhere );
Dr. datasource = Ds;
Dr. datatextfield = seltext;
Dr. datavaluefield = selvalue;
Dr. databind ();
DS. Clear ();
DS. Dispose ();
}
/// <Summary>
/// Read Table Data
/// </Summary>
/// <Param name = "TBL"> </param>
/// <Param name = "selvalue"> </param>
/// <Param name = "seltext"> </param>
/// <Param name = "strwhere"> condition </param>
/// <Returns> </returns>
Public dataset getdrop (string TBL, string selvalue, string seltext, string strwhere)
{
SQL = "select {1}, {2} from {0} where 1 = 1 and {3 }";
DS = DBO. getdataset (string. Format (SQL, TBL, selvalue, seltext, strwhere ));
Return Ds;
}
# Endregion
# Region determine whether data exists
/// <Summary>
/// Determine whether data exists: If data exists, true is returned; otherwise, Flash is returned.
/// </Summary>
/// <Param name = "TBL"> data table name </param>
/// <Param name = "inline"> Field name </param>
/// <Param name = "key"> keyword </param>
/// <Param name = "iskeyint"> whether it is of the numeric type: Yes: true; NO: false </param>
/// <Returns> true or false </returns>
Public bool ishavedate (string TBL, string delimiter, string key, bool iskeyint)
{
Bool REV = false;
If (iskeyint)
{
SQL = "select * from {0} Where {1} = {2 }";
}
Else
{
SQL = "select * from {0} Where {1} = '{2 }'";
}
DS = DBO. getdataset (string. Format (SQL, TBL, delimiter, key ));
If (Ds. Tables [0]. Rows. Count> 0)
{
REV = true;
}
Return Rev;
}
# Endregion
}
}
/####################################### #####
Copyright:
The content of this article is edited on this site. You can reprint, publish, and use it at will, but be sure to indicate the original source of the article and this statement.
Http://www.opent.cn by longtaosha
######################################## ####/
/*************************************** **************************************** ***
*
* Function Description: data operation base class that can execute inline SQL statements and stored procedures
* Author: Liu gongxun;
* Version: v0.1 (C #2.0); time:
*
**************************************** ****************************************/
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Namespace EC
{
/// <Summary>
/// Database connection and operation object class
/// </Summary>
Public class dbbase
{
Private bool _ alreadydispose = false;
Private system. Data. sqlclient. sqlconnection conn;
Private system. Data. sqlclient. sqlcommand com;
# region construction and region structure
Public dbbase ()
{< br> try
{< br> conn = new system. data. sqlclient. sqlconnection (configurationmanager. appsettings ["connectionstring"]);
Conn. open ();
COM = new system. data. sqlclient. sqlcommand ();
COM. connection = conn;
}< br> catch (exception ee)
{< br> throw new exception ("database connection error ");
}< BR >}< br> ~ Dbbase ()
{< br> dispose ();
}< br> protected virtual void dispose (bool isdisposing)
{< br> If (_ alreadydispose) return;
If (isdisposing)
{< br> // todo: release controlled resources here
If (com! = NULL)
{< br> com. Cancel ();
com. Dispose ();
}< br> If (Conn! = NULL)
{< br> try
{< br> Conn. close ();
Conn. dispose ();
}< br> catch (exception ee)
{< BR >}< br> finally
{< br> conn = NULL;
}< BR >}< br> // todo: uncontrolled resources are released here. Set the processed tag
_alreadydispose = true;
}< BR ># endregion
# region idisposable member
Public void dispose ()
{
Dispose (true );
GC. suppressfinalize (this );
}
# Endregion
# Region data basic operations
/// <Summary>
/// Executenonquery
/// </Summary>
/// <Param name = "sqlstring"> SQL statement </param>
/// <Returns> Number of affected rows returned </returns>
Public int executenonquery (string sqlstring)
{
Int ret = 0;
Com. commandtext = sqlstring;
Com. commandtype = commandtype. text;
Try
{
Ret = com. executenonquery ();
}
Catch (exception ee)
{
Throw new exception ("SQL:" + sqlstring + "<br/>" + ee. Message. tostring ());
}
Finally
{
Com. Cancel ();
}
Return ret;
}
/// <Summary>
/// Execute the insert statement to return the identity
/// </Summary>
/// <Param name = "sqlstring"> SQL statement </param>
/// <Returns >@@ identity </returns>
Public int execinsert (string sqlstring)
{
Int identity = 0;
// Only insert into statements can be executed
If (! Sqlstring. tolower (). Contains ("insert "))
{
Return-1;
}
Sqlstring + = "select @ identity ";
System. Data. dataset DS = new dataset ();
Try
{
System. Data. sqlclient. sqldataadapter da = new system. Data. sqlclient. sqldataadapter (sqlstring, Conn );
Da. Fill (DS );
Da. Dispose ();
}
Catch (exception ee)
{
Throw new exception ("SQL:" + sqlstring + "<br/>" + ee. Message. tostring ());
}
If (Ds. Tables [0]. Rows. Count> 0)
{
Identity = convert. toint32 (Ds. Tables [0]. Rows [0] [0]);
}
DS. Clear ();
DS. Dispose ();
Return identity;
}
/// <Summary>
/// Execute the SQL statement to return the record set
/// </Summary>
/// <Param name = "sqlstring"> SQL statement </param>
/// <Returns> dataset </returns>
Public dataset getdataset (string sqlstring)
{
System. Data. dataset DS = new dataset ();
Try
{
System. Data. sqlclient. sqldataadapter da = new system. Data. sqlclient. sqldataadapter (sqlstring, Conn );
Da. Fill (DS );
Da. Dispose ();
}
Catch (exception ee)
{
Throw new exception ("SQL:" + sqlstring + "<br/>" + ee. Message. tostring ());
}
Return Ds;
}
/// <Summary>
/// Execute the Stored Procedure (N parameters are returned)
/// </Summary>
/// <Param name = "procname"> process name </param>
/// <Param name = "hashtable"> input parameter table </param>
/// <Param name = "hashtable1"> output parameter table </param>
/// <Returns> return parameter table </returns>
Public System. Collections. hashtable execprocedure (string procname, system. Collections. hashtable, system. Collections. hashtable hashtable1)
{
System. Collections. hashtable hashtable2 = new system. Collections. hashtable ();
System. Collections. idictionaryenumerator ide = hashtable. getenumerator ();
System. Collections. idictionaryenumerator ide1 = hashtable1.getenumerator ();
Com. commandtype = commandtype. storedprocedure;
Com. commandtext = procname;
While (IDE. movenext ())
{
System. Data. sqlclient. sqlparameter P = new system. Data. sqlclient. sqlparameter (IDE. Key. tostring (), IDE. value );
Com. Parameters. Add (P );
}
While (ide1.movenext ())
{
System. Data. sqlclient. sqlparameter P = new system. Data. sqlclient. sqlparameter (ide1.key. tostring (), IDE. value );
Com. Parameters. Add (P );
}
try
{< br> com. executenonquery ();
ide1 = hashtable1.getenumerator ();
while (ide1.movenext ()
{< br> string K = ide1.key. tostring ();
hashtable2.add (K, Com. parameters [K]. value);
}< BR >}< br> catch (exception ee)
{< br> throw new exception (EE. message. tostring ();
}< br> finally
{< br> com. cancel ();
}< br> return hashtable2;
}