A common database class

Source: Internet
Author: User
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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.