ADO. NET generic database category, general database ado.net

Source: Internet
Author: User

ADO. NET generic database category, general database ado.net
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Threading. Tasks;

Using System. Data;
Using System. Data. SqlClient;

Namespace Test
{
Public class DBHelper
{
Public static string ConString = "Data Source =.; Initial Catalog = bankdb; User id = sa; Password = 123 ;";

// Execute the add, delete, and modify Method
Public static int RunNoQuery (string parameter text, CommandType parameter type, params SqlParameter [] pars)
{
SqlConnection con = new SqlConnection (ConString );
Con. Open ();
SqlCommand cmd = new SqlCommand (plain text, con );
Cmd. CommandType = primitive type;
If (pars! = Null & pars. Length> 0)
{
Foreach (SqlParameter p in pars)
{
Cmd. Parameters. Add (p );
}
}
Int rows = cmd. ExecuteNonQuery ();
Con. Close ();
Return rows;
}

// Method for executing the query (DataSet)
Public static DataSet RunSelect (string parameter text, CommandType parameter type, params SqlParameter [] pars)
{
SqlConnection con = new SqlConnection (ConString );

SqlDataAdapter da = new SqlDataAdapter (plain text, con );
Da. SelectCommand. CommandType = repeated type;
If (pars! = Null & pars. Length> 0)
{
Foreach (SqlParameter p in pars)
{
Da. SelectCommand. Parameters. Add (p );
}
}
DataSet ds = new DataSet ();
Da. Fill (ds );

Return ds;
}

// Run the query to obtain a value.
Public static object RunOneValue (string plain text, CommandType primitive type, params SqlParameter [] pars)
{
SqlConnection con = new SqlConnection (ConString );
Con. Open ();
SqlCommand cmd = new SqlCommand (plain text, con );
Cmd. CommandType = primitive type;
If (pars! = Null & pars. Length> 0)
{
Foreach (SqlParameter p in pars)
{
Cmd. Parameters. Add (p );
}
}
Object obj = cmd. ExecuteScalar ();
Con. Close ();
Return obj;
}
}
}

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.