Common Database Operations in Asp.net

Source: Internet
Author: User
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Data. sqlclient;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

/// <Summary>
/// Summary of sqlconnection
/// </Summary>
Public class sqlconn
{
Public sqlconn ()
{
//
// Todo: add the constructor logic here
//
}
Public sqlconnection connstr; // connection string
# Region getstrconnection
/// <Summary>
/// Obtain the connection string
/// </Summary>
/// <Returns> </returns>
Public String getstrconnection ()
{
String constr;
Constr = system. configuration. configurationsettings. receivettings ["strsqlconnection"];
Return constr;
}
# Endregion

# Region openconn () Open the database
/// <Summary>
/// Open the database
/// </Summary>
Public void openconn ()
{
String constr;
Constr = getstrconnection ();
Connstr = new sqlconnection (constr );
Connstr. open ();
}
# Endregion

# Region closeconn () shut down the database
/// <Summary>
/// Closeconn closes the database
/// </Summary>
Public void closeconn ()
{
Connstr. Dispose ();
Connstr. Close ();
}
# Endregion

# Region execsql (string SQL) Execute SQL statements
/// <Summary>
/// Execute the SQL statement
/// </Summary>
/// <Param name = "SQL"> </param>
Public void execsql (string SQL)
{
Openconn ();
Sqlcommand cmd = new sqlcommand (SQL, connstr );
Cmd. executenonquery ();
Closeconn ();
}
# Endregion

# Region getdataset (string SQL) returns the DataSet object
/// <Summary>
/// Return the DataSet object
/// </Summary>
/// <Param name = "SQL"> </param>
/// <Returns> </returns>
Public dataset getdataset (string SQL)
{
Openconn ();
Sqldataadapter rs = new sqldataadapter (SQL, connstr );
Dataset DS = new dataset ();
Rs. Fill (DS );
Return Ds;
}
# Endregion

# Region getdataview (string SQL) returns the dataview object
/// <Summary>
/// Return the dataview object
/// </Summary>
/// <Param name = "SQL"> </param>
/// <Returns> </returns>
Public dataview getdataview (string SQL)
{
Dataset DS = new dataset ();
DS = getdataset (SQL );
Dataview DV = new dataview (Ds. Tables [0]);
Return DV;
}
# Endregion

# Region getdatareader (string SQL) returns the datareader object
/// <Summary>
/// Return the datareader object
/// </Summary>
/// <Param name = "SQL"> </param>
/// <Returns> </returns>
Public sqldatareader getdatareader (string SQL)
{
Openconn ();
Sqlcommand cmd = new sqlcommand (SQL, connstr );
Sqldatareader DR = cmd. executereader ();
Return Dr;
}
# Endregion

}

 

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.