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;
/// <Summary>
/// Database Summary
/// </Summary>
Public Class Database
{
Private Sqlconnection conn;
Private Sqlcommand SC;
Private Sqldataadapter SA;
//Returns the current connection, which is used in transactions.
PublicSqlconnection Conn
{
Get{ReturnConn ;}
}
Public Database ()
{
// When object 1 is created, a connection is established.
This . Conn = New Sqlconnection (system. configuration. configurationmanager. receivettings. Get ( " Constr " ));
This . Conn. open ();
}
//Close database connection
PublicVoidDis_connect ()
{
If(Conn! = Null)
Conn. Close ();
}
// Query a database
Public Dataset query ( String CMD)
{
SC = New Sqlcommand (CMD, Conn ); // Create a Data command using the connection of the current object
SA = New Sqldataadapter (SC );
Dataset myds = New Dataset ();
Myds = New Dataset ();
SA. Fill (myds ); // Run the query command and save the query result in dataset.
Return Myds;
}
// Add record
Public Int Update ( String CMD)
{
SC = New Sqlcommand (CMD, Conn );
Int Re;
Try
{
Re = SC. executenonquery (); // Execute other commands (insert, update, delete)
}
Catch (Exception E)
{
Console. Write (E. Message );
Re = - 5 ; // Database Operation exception
}
Return Re;
}
/* Call the stored procedure of the Query Class
* Prcename: name of the stored procedure
* Parames []: parameter Array
*/
Public Dataset call_que_proce ( String Prcename, Params Sqlparameter [] parames)
{
SA = New Sqldataadapter ();
SC = New Sqlcommand ();
Dataset DS = New Dataset ();
SC. Connection=Conn;
SC. commandtext=Prcename;
SC. commandtype=Commandtype. storedprocedure;
SA. selectcommand=SC;
Foreach(Sqlparameter spInParames)
{
SA. selectcommand. Parameters. Add (SP );
}
SA. Fill (DS );
SC. Parameters. Clear ();
SA. Dispose ();
ReturnDS;
}
/* Call non-query stored procedures
* Prcename
*/
Public Int Call_noqeury_proce ( String Prcename,ParamsSqlparameter [] parames)
{
SC = New Sqlcommand ();
Int Result = 0 ;
SC. Connection = Conn;
SC. commandtext = Prcename;
SC. commandtype = Commandtype. storedprocedure;
SA. selectcommand = SC;
Foreach(Sqlparameter spInParames)
{
SA. selectcommand. Parameters. Add (SP );
}
result = SC. executenonquery ();
SC. parameters. clear ();
return result;
}< BR >}< br>