三层架构SqlHelper
程序员文章站
2022-06-22 23:41:45
三层架构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;usin...
三层架构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