public class Dbhelp in ASP.net class (C #)

Source: Internet
Author: User
Tags static class
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Data;
Using System.Data.SqlClient;

Using System.Configuration;
        namespace Mybookshop.dal {public static class DBHelper {//sql database connection, note: Mybookshop is a connection string that needs to be changed according to the actual situation of the project
        private static SqlConnection connection; public static SqlConnection Connection {get {string connectionString = configurationmanager.connectionstrings["Mybookshop"].
                ConnectionString;
                    if (connection = = null) {connection = new SqlConnection (connectionString); Connection.
                Open (); else if (connection. state = = System.Data.ConnectionState.Closed) {connection.
                Open (); else if (connection. state = = System.Data.ConnectionState.Broken) {connection.
    Close ();                Connection.
                Open ();
            return connection;
            }///execute the increment, delete, and modify operation without parameter data records, and return the result value after execution public static int ExecuteCommand (string safesql) {
            SqlCommand cmd = new SqlCommand (Safesql, Connection); int result = cmd.
            ExecuteNonQuery ();
        return result; }//execute the increment, delete, and change operation of the data record with parameter, return the result value after execution public static int ExecuteCommand (String sql, params sqlparameter[] Value
            s) {SqlCommand cmd = new SqlCommand (sql, Connection); Cmd.
            Parameters.addrange (values); return CMD.
        ExecuteNonQuery ();
            The first row of the result set after executing without a parameter SQL statement is returned, but the result must be the int public static int getscalar (string safesql) {
            SqlCommand cmd = new SqlCommand (Safesql, Connection); int result = Convert.ToInt32 (cmd.
            ExecuteScalar ());
        return result; //Returns the first row of the result set after executing with a parameter SQL statement, but the result must be int public static int getscalar (String sql, params sqlparameter[] values) {SqlCommand cmd = new SqlCommand (SQL,
            Connection); Cmd.
            Parameters.addrange (values); int result = Convert.ToInt32 (cmd.
            ExecuteScalar ());
        return result; //Returns the reader public static SqlDataReader Getreader (String safesql) {sqlcom After executing the SQL statement with no parameters
            Mand cmd = new SqlCommand (Safesql, Connection); SqlDataReader reader = cmd.
            ExecuteReader ();
        return reader;
        //Returns the reader public static SqlDataReader getreader (String sql, params sqlparameter[] values) after executing the SQL statement with parameters
            {SqlCommand cmd = new SqlCommand (sql, Connection); Cmd.
            Parameters.addrange (values); SqlDataReader reader = cmd.
            ExecuteReader ();
        return reader;  //Returns the result data table after executing the SQL statement without parameters public static DataTable GetDataSet (String safesql) {DataSet ds = new DAtaset ();
            SqlCommand cmd = new SqlCommand (Safesql, Connection);
            SqlDataAdapter da = new SqlDataAdapter (cmd); Da.
            Fill (DS); Return DS.
        Tables[0];
        //Returns the result data table after execution with a parameter SQL statement public static DataTable GetDataSet (String sql, params sqlparameter[] values)
            {DataSet ds = new DataSet ();
            SqlCommand cmd = new SqlCommand (sql, Connection); Cmd.
            Parameters.addrange (values);
            SqlDataAdapter da = new SqlDataAdapter (cmd); Da.
            Fill (DS); Return DS.
        Tables[0];
 }

    }
}

Related Article

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.