Common Data Access class (C #)

Source: Internet
Author: User

 

<Pre name = "code" class = "csharp"> using System;

Using System. Collections. Generic;

Using System. Linq;

Using System. Web;

Using System. Data;

Using System. Data. SqlClient;

Using System. Configuration;

 

Namespace SqlHelper

{

Public class SQLHelper

{

/// <Summary>

/// Obtain the data connection string

/// </Summary>

/// <Returns> </returns>

Public static string GetConnectionString ()

{

Return ConfigurationManager. ConnectionStrings ["ASPNET3_5_DataBase"]. ConnectionString;

}

 

/// <Summary>

/// Execute the query and return the result to the able

/// </Summary>

/// <Param name = "strSql"> query statement </param>

/// <Param name = "parameters"> possible parameters </param>

/// <Returns> returns a query result table </returns>

Public static DataTable ExecuteDataTable (string strSql, params SqlParameter [] parameters)

{

Using (SqlConnection conn = new SqlConnection (GetConnectionString ()))

{

Conn. Open ();

Using (SqlCommand cmd = new SqlCommand ())

{

Cmd. Connection = conn;

Cmd. CommandText = strSql;

Foreach (SqlParameter p in parameters)

{

Cmd. Parameters. Add (p );

}

DataSet ds = new DataSet ();

Using (SqlDataAdapter adapter = new SqlDataAdapter (cmd ))

{

Adapter. Fill (ds );

Return ds. Tables [0];

}

}

}

}

 

/// <Summary>

/// Add, delete, and modify data

/// </Summary>

/// <Param name = "strSql"> </param>

/// <Param name = "parameters"> </param>

Public static void ExecuteNonQuery (string strSql, params SqlParameter [] parameters)

{

Using (SqlConnection conn = new SqlConnection (GetConnectionString ()))

{

Conn. Open ();

Using (SqlCommand cmd = new SqlCommand ())

{

Cmd. Connection = conn;

Cmd. CommandText = strSql;

Foreach (SqlParameter p in parameters)

{

Cmd. Parameters. Add (p );

}

Cmd. ExecuteNonQuery ();

}

}

}

/// <Summary>

/// Execute the query and return the value of the first column in the first row of the result set

/// </Summary>

/// <Param name = "strSql"> </param>

/// <Param name = "parameters"> </param>

/// <Returns> </returns>

Public static object ExecuteScalar (string strSql, params SqlParameter [] parameters)

{

Using (SqlConnection conn = new SqlConnection (GetConnectionString ()))

{

Conn. Open ();

Using (SqlCommand cmd = new SqlCommand ())

{

Cmd. Connection = conn;

Cmd. CommandText = strSql;

Foreach (SqlParameter p in parameters)

{

Cmd. Parameters. Add (p );

}

Return cmd. ExecuteScalar ();

}

}

}

}

}

 

From hi_dzj's column

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.