ADO.NET五大核心类

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SqlHelper
{
    
   public class SqlHelper
    {
        private static string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;

        /*ADO.NET 五大核心类
        1、SqlConnention 连接
        2、SqlCommend  执行命令
        3、SqlDadaApaper 适配器
        4、SqlDataReader 读取
        5、DataSet  数据集
        */
        public static DataTable GetData(string sql, params SqlParameter[] paras)
        {
            using (SqlConnection sqlconn = new SqlConnection(conStr))
            {
                SqlDataAdapter adapter = new SqlDataAdapter(sql, sqlconn);
                DataSet dataSet = new DataSet();
                adapter.Fill(dataSet);
                return dataSet.Tables[0];
            }
        }

        public static SqlDataReader GetData2(string sql, params SqlParameter[] paras)
        {
            SqlConnection sqlconn = new SqlConnection(conStr);

            using (SqlCommand cmd = new SqlCommand(sql, sqlconn))
            {
                sqlconn.Open();
                SqlDataReader dataReader = cmd.ExecuteReader();
                return dataReader;
            }           
        }

        public static int AddData(string sql, params SqlParameter[] paras)
        {
            using (SqlConnection sqlconn = new SqlConnection(conStr))
            {
                SqlCommand cmd = new SqlCommand(sql,sqlconn);
                if (paras != null) cmd.Parameters.AddRange(paras);
                sqlconn.Open();
                return cmd.ExecuteNonQuery();
            }
        }
    }
}

ADO.NET五大核心类_第1张图片相关链接:https://blog.csdn.net/lxd8731247769/article/details/50659596

你可能感兴趣的:(数据库)