当前位置: 移动技术网 > IT编程>数据库>MSSQL > 三层架构SqlHelper

三层架构SqlHelper

2020年07月17日  | 移动技术网IT编程  | 我要评论

三层架构DAL层SqlHelper

模板: windows->类库
引用: System.Configuration、System.Data、System.Data.SqlClient
分析: 增、删、改、查功能及对应的SQL语句

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

namespace DAL
{
    class SqlHelper
    {
        private static string strConn = ConfigurationManager.ConnectionStrings["DigitalProductShopConnectionString"].ConnectionString;

        public static DataTable Query(string sql)
        {
            DataTable table = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter(sql, strConn);
            adapter.Fill(table);
            return table;
        }

        public static int NonQuery(string sql)
        {
            int num = 0;
            SqlConnection sqlConn = new SqlConnection(strConn);
            SqlCommand sqlCmd = new SqlCommand(sql, sqlConn);
            try
            {
                sqlConn.Open();
                num = sqlCmd.ExecuteNonQuery();
            }
            finally
            {
                if (sqlConn.State == ConnectionState.Open)
                    sqlConn.Close();
            }
            return num;
        }
    }
}

查询功能: 调用Query方法,返回List或DataTable
删除功能: 调用NonQuery方法,返回int或bool
添加功能: 调用NonQuery方法,返回int或bool
修改功能:
1、查找对象→Select(主键):调用Query,返回对象或null。
2、更新属性→Update,调用NonQuery方法,返回int或bool

本文地址:https://blog.csdn.net/YuYuLingYan/article/details/107389387

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网