Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use ADO.NET generic database access classes

Shulou Source: shulou.com Published: 2022-06-03 13:02:13 09月20日 Update

This article mainly shows you "how to use ADO.NET general database access class", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use ADO.NET general database access class" this article.

The details are 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=.;Initial Catalog=bankdb;User id=sa;Password=123;"; / / execute the method 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 } / / method for executing query (DataSet) 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) }} DataSet ds = new DataSet (); da.Fill (ds); return ds;} / execute the query to get a value of 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;} these are all the contents of the article "how to use ADO.NET Universal Database access Class". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Content data database articles methods learning help query easy to understand more organization knowledge editing industry information information channels channels research Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple NVidia Linux Microsoft Docker