SQLHelper For C #. Net,

Source: Internet
Author: User

SQLHelper For C #. Net,

Public class SqliteHelper
{
// Obtain the connection string
Private static readonly string str = ConfigurationManager. ConnectionStrings ["conStr"]. ConnectionString;
/// <Summary>
/// Added, deleted, and modified functions
/// </Summary>
/// <Sp name = "SQL"> SQL statement </sp>
/// <Sp name = "sp"> SQL parameters </sp>
/// <Returns> Number of affected rows </returns>
Public static int ExecuteNonQuery (string SQL, params SQLiteParameter [] sp)
{
Using (SQLiteConnection con = new SQLiteConnection (str ))
{
Using (SQLiteCommand cmd = new SQLiteCommand (SQL, con ))
{
Con. Open ();
If (param! = Null)
{
Cmd. Parameters. AddRange (sp );
}
Return cmd. ExecuteNonQuery ();
}
}
}
/// <Summary>
/// Query
/// </Summary>
/// <Sp name = "SQL"> SQL statement </sp>
/// <Sp name = "param"> parameter </sp>
/// <Returns> returns the first column of the First row </returns>
Public static object ExecuteScalar (string SQL, params SQLiteParameter [] sp)
{
Using (SQLiteConnection con = new SQLiteConnection (str ))
{
Using (SQLiteCommand cmd = new SQLiteCommand (SQL, con ))
{
Con. Open ();
If (param! = Null)
{
Cmd. Parameters. AddRange (sp );
}
Return cmd. ExecuteScalar ();
}
}
}
/// <Summary>
/// Query the table
/// </Summary>
/// <Sp name = "SQL"> SQL statement </sp>
/// <Sp name = "param"> parameter </sp>
/// <Returns> return table </returns>
Public static DataTable ExecuteTable (string SQL, params SQLiteParameter [] sp)
{
DataTable dt = new DataTable ();
Using (SQLiteDataAdapter sda = new SQLiteDataAdapter (SQL, str ))
{
If (param! = Null)
{
Sda. SelectCommand. Parameters. AddRange (sp );
}
Sda. Fill (dt );
}
Return dt;
}
/// <Summary>
/// Query
/// </Summary>
/// <Sp name = "SQL"> SQL statement </sp>
/// <Sp name = "param"> parameter </sp>
/// <Returns> data </returns>
Public static SQLiteDataReader ExecuteReader (string SQL, params SQLiteParameter [] sp)
{
SQLiteConnection con = new SQLiteConnection (str );
Using (SQLiteCommand cmd = new SQLiteCommand (SQL, con ))
{
If (param! = Null)
{
Cmd. Parameters. AddRange (sp );
}
Try
{
Con. Open ();
Return cmd. ExecuteReader (CommandBehavior. CloseConnection );
}
Catch (Exception ex)
{
Con. Close ();
Con. Dispose ();
Throw ex;
}
}
}

}


What is sqlhelper?

SqlHelper is A. NET Framework-based database operation component. The component contains the database operation method. Currently, SqlHelper has many versions, mainly the SqlHelper class released at the beginning of Microsoft, which is later included in the Enterprise Library open-source package. Another major version is dbhelper.org's open-source sqlhelper component, which features simplicity and high performance. It not only supports sqlserver, but also supports sqlserver, oracle, access, and Mysql databases. It is also an open-source project, free Download is provided. SqlHelper is used to simplify the repeated Writing of database connections (SqlConnection), SqlCommand, SqlDataReader, and so on. After SqlHelper is encapsulated, you only need to input some parameters to the method, such as the database connection string and SQL parameters, to access the database, which is very convenient. The SqlHelper class is used to encapsulate data access through a set of static methods. This class cannot be inherited or instantiated, so it is declared as a non-inherited class that contains a dedicated constructor. Each method implemented in the SqlHelper class provides a set of consistent overloading. This provides a good way to use the SqlHelper class to execute commands. It also provides necessary flexibility for developers to select a way to access data. Each method overload supports different method parameters, so developers can determine how to pass connection, transaction, and parameter information.

Web static sqlHelper multi-user access problems

First, it is not recommended to use a SqlConnection to access multiple users, especially when concurrency may exist,
You can write this in the class:
Static SqlConnection GetConnection (string constr) {return new SqlConnection (constr);} public static object ExecuteScalar (string SQL) {using (SqlConnection con = GetConnection ("xxx ")) {work} Don't worry too much about connection depletion unless your SQL statements are slow or your site traffic is huge.
You need to know that the daily pv of my site is tens of millions.


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.