三层架构中DAL层Sqlhelper怎样快速掌握?(常用)
2020-06-26 04:27
831 查看
首先把Model层和UI层的数据源一定要配置完成,按步骤会特别顺手。
1.我们来到Web.config中找到UI层配置的数据源的name:
<add name="ProDBConnectionString" connectionString="Data Source=.;Initial Catalog=ProDB;Integrated Security=True" providerName="System.Data.SqlClient" />
这个 name=“ProDBConnectionString”。
2.我们来看DAL层的SqlHelper:
public class SqlHelper { //固定连接字符串 public static string strcon = ConfigurationManager.ConnectionStrings["ProDBConnectionString"].ConnectionString; /// <summary> /// 查询 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static DataTable Cx(string sql) { DataTable dt=new DataTable(); SqlDataAdapter arAdapter=new SqlDataAdapter(sql,strcon); arAdapter.Fill(dt); return dt; } /// <summary> /// 增删改 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static int Zsg(string sql) { int num = 0; SqlConnection sqlcon=new SqlConnection(strcon); SqlCommand cmd=new SqlCommand(sql,sqlcon); try { sqlcon.Open(); num = cmd.ExecuteNonQuery(); } finally { if (sqlcon.State==ConnectionState.Open) { sqlcon.Close(); } } return num; } }
我们的命名写的很通俗易懂,大家理解的也会很快~
相关文章推荐
- 快速掌握一个语言最常用的50%
- 快速掌握一个语言最常用的50%
- 快速掌握一个语言最常用的50%
- 快速掌握一个语言最常用的50%
- 快速掌握一个语言最常用的50%【转2008】
- 快速掌握一个语言最常用的50%
- 快速掌握一个语言最常用的50%
- 三层架构-DAL层
- 快速掌握mongoDB(一)——mongoDB安装部署和常用shell命令
- 快速掌握一个语言最常用的50%
- 三层架构-DAL层
- 快速掌握一个语言最常用的50%(转)
- Borland DataSnap(MIDAS)三层架构中,常用事件及其触发顺序(转)
- 快速掌握一门语言最常用的50%
- 快速掌握Android三个常用自定义控件Toast AlertDialog Notification
- 转 怎样才算熟练掌握数据结构、常用算法?
- C#中三层架构的Sqlhelper代码
- 快速掌握正则表达式,掌握常用的就ok
- 快速掌握一门语言最常用的50%
- 快速掌握一个语言最常用的50%