Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ADO.NET generic database access classes

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report