.net简单的三层实现功能
在网上看到别人的代码,写下来留着看,欢迎各位指教,有没有工厂模式的学习资料啊,留下给小弟学习学习.
首先写个Model类,实现业务实体,我的比较简单,只有三个字段
View Code
1 public class LoginModel
2 {
3 #region
4
5 private int _AdminId;
6 private string _AdminUID;
7 private string _AdminPWD;
8
9 public string AdminPWD
10 {
11 get { return _AdminPWD; }
12 set { _AdminPWD = value; }
13 }
14
15 public int AdminId
16 {
17 get { return _AdminId; }
18 set { _AdminId = value; }
19 }
20
21
22 public string AdminUID
23 {
24 get { return _AdminUID; }
25 set { _AdminUID = value; }
26 }
27
28 #endregion
29 }
2.创建一个DBHelp类,实现SQLServer数据库访问
View Code
1 public class DBHelp
2 {
3 private static readonly string connectionString = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString.ToString();
4 private static readonly string providerName = "System.Data.SqlClient";
5
6 /// <summary>
7 /// GetConnection 用于获取连接数据库的 connection 对象
8 /// </summary>
9 /// <returns></returns>
10 private static DbConnection GetConnection()
11 {
12 DbProviderFactory _factory = DbProviderFactories.GetFactory(providerName);
13 DbConnection connection = _factory.CreateConnection();
14 connection.ConnectionString = connectionString;
15 return connection;
16 }
17
18 /// <summary>
19 /// GetCommand 获取命令参数 command 对象
20 /// </summary>
21 /// <param name="commandText"></param>
22 /// <param name="commandType"></param>
23 /// <param name="connection"></param>
24 /// <returns></returns>
25 private static DbCommand GetCommand(string commandText, CommandType commandType, DbConnection connection)
26 {
27 DbCommand command = connection.CreateCommand();
28 command.CommandText = commandText;
29 command.CommandType = commandType;
30 return command;
31 }
32
33 /// <summary>
34 /// GetCommand 方法重载
35 /// </summary>
36 /// <param name="commandText">sql语句</param>
37 /// <param name="connection"></param>
38 /// <returns></returns>
39 private static DbCommand GetCommand(string commandText, DbConnection connection)
40 {
41 DbCommand command = connection.CreateCommand();
42 command.CommandText = commandText;
43 command.CommandType = CommandType.Text;
44 return command;
45 }
46
47 /// <summary>
48 /// GetParameter 用于为命令设置参数
49 /// </summary>
50 /// <param name="paramName"></param>
51 /// <param name="paramValue"></param>
52 /// <param name="command"></param>
53 /// <returns></returns>
54 private static DbParameter GetParameter(string paramName, object paramValue, DbCommand command)
55 {
56 DbParameter parameter = command.CreateParameter();
57 parameter.ParameterName = paramName;
58 parameter.Value = paramValue;补充:Web开发 , ASP.Net ,