Ado. NET Universal Database Access Class _ Practical skills

Source: Internet
Author: User

This example of this article for you to share the Ado.net Universal Database access classes for everyone to learn, the specific content as follows

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=.;i Nitial catalog=bankdb; User Id=sa;



    password=123; ";

      Methods to perform additions and deletions public static int Runnoquery (string cmdtext, CommandType cmdtype, params sqlparameter[] pars) {

      SqlConnection con = new SqlConnection (constring); Con.

      Open ();

      SqlCommand cmd = new SqlCommand (Cmdtext, con);

      Cmd.commandtype = Cmdtype; if (pars!= null && pars. Length > 0) {foreach (SqlParameter p in pars) {cmd.

        Parameters.Add (P); int rows = cmd.

      ExecuteNonQuery (); Con.

      Close ();

    return rows; //Execute Query (DataSet) method public static DataSet Runselect (String cmdtext, CommandType cmdtype, params sqlparameter[ ] Pars{SqlConnection con = new SqlConnection (constring);

      SqlDataAdapter da = new SqlDataAdapter (Cmdtext, con);

      Da.SelectCommand.CommandType = Cmdtype; if (pars!= null && pars. Length > 0) {foreach (SqlParameter p in pars) {da.

        SELECTCOMMAND.PARAMETERS.ADD (P);

      The DataSet ds = new DataSet (); Da.



      Fill (DS);

    return DS; //Execute query to get a value public static object Runonevalue (String cmdtext, CommandType cmdtype, params sqlparameter[] Pars

      {SqlConnection con = new SqlConnection (constring); Con.

      Open ();

      SqlCommand cmd = new SqlCommand (Cmdtext, con);

      Cmd.commandtype = Cmdtype; if (pars!= null && pars. Length > 0) {foreach (SqlParameter p in pars) {cmd.

        Parameters.Add (P); } Object obj = cmd.

      ExecuteScalar (); Con.

      Close ();

    return obj;

 } }

}
 

The above is the entire content of this article, I hope to help you learn.

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.