命令行代码生成工具
最近做项目时模仿petshop4的架构进行开发,但是数据库方面的基本操作比较麻烦,没有一个代码生成工具是非常烦琐的事。浪费了很多时间,很多很好的代码生成工具又太难学习(自己太笨了!),只好自己写了一个,很简单的东西,希望能对大家有些帮助!
使用方法
1.更改配置文件
2.运行工具,输入表名
说明:只生成适用于sqlserver的操作,具体请看下边的生成示例
下载地址:命令行代码生成工具CG
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnString" value="server=192.168.0.2;uid=dd;pwd=dd;database=rst"/>数据库连接串
<add key="SaveDir" value="E:\\Class\\"/>生成文件保存目录
</appSettings>
</configuration>
这里是生成的文件的例子
Model
1using System;
2namespace Model
3{
4 public class ModelCB_Receive
5 {
6 private int _receiveID;
7
8 private string _oID;
9
10 private string _pstatus;
11
12 private string _pstring;
13
14 private string _pmode;
15
16 private string _mD5Info;
17
18 private string _amount;
19
20 private string _moneyType;
21
22 private string _remark1;
23
24 private string _remark2;
25
26
27
28 public int ReceiveID
29 {
30 get
31 {
32 return this._receiveID;
33 }
34 }
35
36 public string OID
37 {
38 get
39 {
40 return this._oID;
41 }
42 set
43 {
44 this._oID=value;
45 }
46 }
47
48 public string Pstatus
49 {
50 get
51 {
52 return this._pstatus;
53 }
54 set
55 {
56 this._pstatus=value;
57 }
58 }
59
60 public string Pstring
61 {
62 get
63 {
64 return this._pstring;
65 }
66 set
67 {
68 this._pstring=value;
69 }
70 }
71
72 public string Pmode
73 {
74 get
75 {
76 return this._pmode;
77 }
78 set
79 {
80 this._pmode=value;
81 }
82 }
83
84 public string MD5Info
85 {
86 get
87 {
88 return this._mD5Info;
89 }
90 set
91 {
92 this._mD5Info=value;
93 }
94 }
95
96 public string Amount
97 {
98 get
99 {
100 return this._amount;
101 }
102 set
103 {
104 this._amount=value;
105 }
106 }
107
108 public string MoneyType
109 {
110 get
111 {
112 return this._moneyType;
113 }
114 set
115 {
116 this._moneyType=value;
117 }
118 }
119
120 public string Remark1
121 {
122 get
123 {
124 return this._remark1;
125 }
126 set
127 {
128 this._remark1=value;
129 }
130 }
131
132 public string Remark2
133 {
134 get
135 {
136 return this._remark2;
137 }
138 set
139 {
140 this._remark2=value;
141 }
142 }
143
144
145 public ModelCB_Receive()
146 {
147 }
148 public ModelCB_Receive(int receiveID,string oID,string pstatus,string pstring,string pmode,string mD5Info,string amount,string moneyType,string remark1,string remark2)
149 {
150 this._receiveID = receiveID;
151 this._oID = oID;
152 this._pstatus = pstatus;
153 this._pstring = pstring;
154 this._pmode = pmode;
155 this._mD5Info = mD5Info;
156 this._amount = amount;
157 this._moneyType = moneyType;
158 this._remark1 = remark1;
159 this._remark2 = remark2;
160 }
161 }
162}
163
164
165
166
167
1using System;
2namespace Model
3{
4 public class ModelCB_Receive
5 {
6 private int _receiveID;
7
8 private string _oID;
9
10 private string _pstatus;
11
12 private string _pstring;
13
14 private string _pmode;
15
16 private string _mD5Info;
17
18 private string _amount;
19
20 private string _moneyType;
21
22 private string _remark1;
23
24 private string _remark2;
25
26
27
28 public int ReceiveID
29 {
30 get
31 {
32 return this._receiveID;
33 }
34 }
35
36 public string OID
37 {
38 get
39 {
40 return this._oID;
41 }
42 set
43 {
44 this._oID=value;
45 }
46 }
47
48 public string Pstatus
49 {
50 get
51 {
52 return this._pstatus;
53 }
54 set
55 {
56 this._pstatus=value;
57 }
58 }
59
60 public string Pstring
61 {
62 get
63 {
64 return this._pstring;
65 }
66 set
67 {
68 this._pstring=value;
69 }
70 }
71
72 public string Pmode
73 {
74 get
75 {
76 return this._pmode;
77 }
78 set
79 {
80 this._pmode=value;
81 }
82 }
83
84 public string MD5Info
85 {
86 get
87 {
88 return this._mD5Info;
89 }
90 set
91 {
92 this._mD5Info=value;
93 }
94 }
95
96 public string Amount
97 {
98 get
99 {
100 return this._amount;
101 }
102 set
103 {
104 this._amount=value;
105 }
106 }
107
108 public string MoneyType
109 {
110 get
111 {
112 return this._moneyType;
113 }
114 set
115 {
116 this._moneyType=value;
117 }
118 }
119
120 public string Remark1
121 {
122 get
123 {
124 return this._remark1;
125 }
126 set
127 {
128 this._remark1=value;
129 }
130 }
131
132 public string Remark2
133 {
134 get
135 {
136 return this._remark2;
137 }
138 set
139 {
140 this._remark2=value;
141 }
142 }
143
144
145 public ModelCB_Receive()
146 {
147 }
148 public ModelCB_Receive(int receiveID,string oID,string pstatus,string pstring,string pmode,string mD5Info,string amount,string moneyType,string remark1,string remark2)
149 {
150 this._receiveID = receiveID;
151 this._oID = oID;
152 this._pstatus = pstatus;
153 this._pstring = pstring;
154 this._pmode = pmode;
155 this._mD5Info = mD5Info;
156 this._amount = amount;
157 this._moneyType = moneyType;
158 this._remark1 = remark1;
159 this._remark2 = remark2;
160 }
161 }
162}
163
164
165
166
167
IDAL接口
1using System;
2using System.Collections.Generic;
3using System.Text;
4using Model;
5namespace IDAL
6{
7 public interface ICB_Receive
8 {
9 Insert(ModelCB_Receive modelCB_Receive);
10 Delete(ModelCB_Receive modelCB_Receive);
11 Update(ModelCB_Receive modelCB_Receive);
12 ModelCB_Receive GetBy(int receiveID);
13 IList<ModelCB_Receive> GetAll();
14 }
15}
1using System;
2using System.Collections.Generic;
3using System.Text;
4using Model;
5namespace IDAL
6{
7 public interface ICB_Receive
8 {
9 Insert(ModelCB_Receive modelCB_Receive);
10 Delete(ModelCB_Receive modelCB_Receive);
11 Update(ModelCB_Receive modelCB_Receive);
12 ModelCB_Receive GetBy(int receiveID);
13 IList<ModelCB_Receive> GetAll();
14 }
15}
DataAccess工厂
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Configuration;
5using System.Reflection;
6using .IDAL;
7namespace DALFactory
8{
9 public class DataAccess
10 {
11 private static readonly string path = ConfigurationManager.AppSettings["DAL"];
12 public static IDAL.ICB_Receive CreateICB_Receive()
13 {
14 string className = path + ".DalCB_Receive";
15 return (IDAL.ICB_Receive)Assembly.Load(path).CreateInstance(className);
16 }
17 }
18}
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Configuration;
5using System.Reflection;
6using .IDAL;
7namespace DALFactory
8{
9 public class DataAccess
10 {
11 private static readonly string path = ConfigurationManager.AppSettings["DAL"];
12 public static IDAL.ICB_Receive CreateICB_Receive()
13 {
14 string className = path + ".DalCB_Receive";
15 return (IDAL.ICB_Receive)Assembly.Load(path).CreateInstance(className);
16 }
17 }
18}
Sqlserver接口实现
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Data;
5using System.Data.SqlClient;
6using IDAL;
7using Model;
8namespace SqlSeverDal
9{
10 public class DalCB_Receive : ICB_Receive
11 {
12 const string _parameReceiveID="@ReceiveID";
13 const string _parameOID="@OID";
14 const string _paramePstatus="@Pstatus";
15 const string _paramePstring="@Pstring";
16 const string _paramePmode="@Pmode";
17 const string _parameMD5Info="@MD5Info";
18 const string _parameAmount="@Amount";
19 const string _parameMoneyType="@MoneyType";
20 const string _parameRemark1="@Remark1";
21 const string _parameRemark2="@Remark2";
22 const string _sqlinsert = " insert into CB_Receive(OID , Pstatus , Pstring , Pmode , MD5Info , Amount , MoneyType , Remark1 , Remark2)values(@OID , @Pstatus , @Pstring , @Pmode , @MD5Info , @Amount , @MoneyType , @Remark1 , @Remark2)";
23 const string _sqlupdate = " update CB_Receive set OID=@OID , Pstatus=@Pstatus , Pstring=@Pstring , Pmode=@Pmode , MD5Info=@MD5Info , Amount=@Amount , MoneyType=@MoneyType , Remark1=@Remark1 , Remark2=@Remark2 where ReceiveID=@ReceiveID";
24 const string _sqldelete = "delete CB_Receive where ReceiveID=@ReceiveID";
25 public void Insert(ModelCB_Receive modelCB_Receive)
26 {
27 SqlParameter[] parame= new SqlParameter[9];
28 parame[0] = new SqlParameter(_parameOID, modelCB_Receive.OID);
29 parame[1] = new SqlParameter(_paramePstatus, modelCB_Receive.Pstatus);
30 parame[2] = new SqlParameter(_paramePstring, modelCB_Receive.Pstring);
31 parame[3] = new SqlParameter(_paramePmode, modelCB_Receive.Pmode);
32 parame[4] = new SqlParameter(_parameMD5Info, modelCB_Receive.MD5Info);
33 parame[5] = new SqlParameter(_parameAmount, modelCB_Receive.Amount);
34 parame[6] = new SqlParameter(_parameMoneyType, modelCB_Receive.MoneyType);
35 parame[7] = new SqlParameter(_parameRemark1, modelCB_Receive.Remark1);
36 parame[8] = new SqlParameter(_parameRemark2, modelCB_Receive.Remark2);
37 SqlHelper.ExecuteNonQuery(SqlHelper._connString, CommandType.Text, _sqlinsert, parame);
38 }
39 public void Delete(ModelCB_Receive modelCB_Receive)
40 {
41 SqlParameter[] parame= new SqlParameter[1];
42 parame[0] = new SqlParameter(_parameReceiveID, modelCB_Receive.ReceiveID);
43 SqlHelper.ExecuteNonQuery(SqlHelper._connString, CommandType.Text, _sqldelete, parame);
44 }
45 public ModelCB_Receive GetBy(int receiveID)
46 {
47 SqlParameter[] parame= new SqlParameter[1];
48 parame[0] = new SqlParameter(_parameReceiveID, receiveID);
49 using (SqlDataReader sdr = SqlHelper.ExecuteReader(SqlHelper._connString, CommandType.Text, "Select * from CB_Receive where ReceiveID=@ReceiveID"))
50 {
51 if(sdr.Read())
52 {
53 return MakeCB_ReceiveModel(sdr);
54 }
55 }
56 return null;
57 }
58 public void Update(ModelCB_Receive modelCB_Receive)
59 {
60 SqlParameter[] parame= new SqlParameter[10];
61 parame[0] = new SqlParameter(_parameReceiveID, modelCB_Receive.ReceiveID);
62 parame[1] = new SqlParameter(_parameOID, modelCB_Receive.OID);
63 parame[2] = new SqlParameter(_paramePstatus, modelCB_Receive.Pstatus);
64 parame[3] = new SqlParameter(_paramePstring, modelCB_Receive.Pstring);
65 parame[4] = new SqlParameter(_paramePmode, modelCB_Receive.Pmode);
66 parame[5] = new SqlParameter(_parameMD5Info, modelCB_Receive.MD5Info);
67 parame[6] = new SqlParameter(_parameAmount, modelCB_Receive.Amount);
68 parame[7] = new SqlParameter(_parameMoneyType, modelCB_Receive.MoneyType);
69 parame[8] = new SqlParameter(_parameRemark1, modelCB_Receive.Remark1);
70 parame[9] = new SqlParameter(_parameRemark2, modelCB_Receive.Remark2);
71 SqlHelper.ExecuteNonQuery(SqlHelper._connString, CommandType.Text, _sqlupdate, parame);
72 }
73 public IList<ModelCB_Receive> GetAll()
74 {
75 IList<ModelCB_Receive> MList = new List<ModelCB_Receive>();
76 using (SqlDataReader sdr = SqlHelper.ExecuteReader(SqlHelper._connString, CommandType.Text, "Select * from CB_Receive"))
77 {
78 while(sdr.Read())
79 {
80 MList.Add(MakeCB_ReceiveModel(sdr));
81 }
82 }
83 return MList;
84 }
85 private ModelCB_Receive MakeCB_ReceiveModel(SqlDataReader sdr)
86 {
87 return new ModelCB_Receive((int)sdr["ReceiveID"] , (string)sdr["OID"] , (string)sdr["Pstatus"] , (string)sdr["Pstring"] , (string)sdr["Pmode"] , (string)sdr["MD5Info"] , (string)sdr["Amount"] , (string)sdr["MoneyType"] , (string)sdr["Remark1"] , (string)sdr["Remark2"]);
88 }
89 }
90}
91
92
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Data;
5using System.Data.SqlClient;
6using IDAL;
7using Model;
8namespace SqlSeverDal
9{
10 public class DalCB_Receive : ICB_Receive
11 {
12 const string _parameReceiveID="@ReceiveID";
13 const string _parameOID="@OID";
14 const string _paramePstatus="@Pstatus";
15 const string _paramePstring="@Pstring";
16 const string _paramePmode="@Pmode";
17 const string _parameMD5Info="@MD5Info";
18 const string _parameAmount="@Amount";
19 const string _parameMoneyType="@MoneyType";
20 const string _parameRemark1="@Remark1";
21 const string _parameRemark2="@Remark2";
22 const string _sqlinsert = " insert into CB_Receive(OID , Pstatus , Pstring , Pmode , MD5Info , Amount , MoneyType , Remark1 , Remark2)values(@OID , @Pstatus , @Pstring , @Pmode , @MD5Info , @Amount , @MoneyType , @Remark1 , @Remark2)";
23 const string _sqlupdate = " update CB_Receive set OID=@OID , Pstatus=@Pstatus , Pstring=@Pstring , Pmode=@Pmode , MD5Info=@MD5Info , Amount=@Amount , MoneyType=@MoneyType , Remark1=@Remark1 , Remark2=@Remark2 where ReceiveID=@ReceiveID";
24 const string _sqldelete = "delete CB_Receive where ReceiveID=@ReceiveID";
25 public void Insert(ModelCB_Receive modelCB_Receive)
26 {
27 SqlParameter[] parame= new SqlParameter[9];
28 parame[0] = new SqlParameter(_parameOID, modelCB_Receive.OID);
29 parame[1] = new SqlParameter(_paramePstatus, modelCB_Receive.Pstatus);
30 parame[2] = new SqlParameter(_paramePstring, modelCB_Receive.Pstring);
31 parame[3] = new SqlParameter(_paramePmode, modelCB_Receive.Pmode);
32 parame[4] = new SqlParameter(_parameMD5Info, modelCB_Receive.MD5Info);
33 parame[5] = new SqlParameter(_parameAmount, modelCB_Receive.Amount);
34 parame[6] = new SqlParameter(_parameMoneyType, modelCB_Receive.MoneyType);
35 parame[7] = new SqlParameter(_parameRemark1, modelCB_Receive.Remark1);
36 parame[8] = new SqlParameter(_parameRemark2, modelCB_Receive.Remark2);
37 SqlHelper.ExecuteNonQuery(SqlHelper._connString, CommandType.Text, _sqlinsert, parame);
38 }
39 public void Delete(ModelCB_Receive modelCB_Receive)
40 {
41 SqlParameter[] parame= new SqlParameter[1];
42 parame[0] = new SqlParameter(_parameReceiveID, modelCB_Receive.ReceiveID);
43 SqlHelper.ExecuteNonQuery(SqlHelper._connString, CommandType.Text, _sqldelete, parame);
44 }
45 public ModelCB_Receive GetBy(int receiveID)
46 {
47 SqlParameter[] parame= new SqlParameter[1];
48 parame[0] = new SqlParameter(_parameReceiveID, receiveID);
49 using (SqlDataReader sdr = SqlHelper.ExecuteReader(SqlHelper._connString, CommandType.Text, "Select * from CB_Receive where ReceiveID=@ReceiveID"))
50 {
51 if(sdr.Read())
52 {
53 return MakeCB_ReceiveModel(sdr);
54 }
55 }
56 return null;
57 }
58 public void Update(ModelCB_Receive modelCB_Receive)
59 {
60 SqlParameter[] parame= new SqlParameter[10];
61 parame[0] = new SqlParameter(_parameReceiveID, modelCB_Receive.ReceiveID);
62 parame[1] = new SqlParameter(_parameOID, modelCB_Receive.OID);
63 parame[2] = new SqlParameter(_paramePstatus, modelCB_Receive.Pstatus);
64 parame[3] = new SqlParameter(_paramePstring, modelCB_Receive.Pstring);
65 parame[4] = new SqlParameter(_paramePmode, modelCB_Receive.Pmode);
66 parame[5] = new SqlParameter(_parameMD5Info, modelCB_Receive.MD5Info);
67 parame[6] = new SqlParameter(_parameAmount, modelCB_Receive.Amount);
68 parame[7] = new SqlParameter(_parameMoneyType, modelCB_Receive.MoneyType);
69 parame[8] = new SqlParameter(_parameRemark1, modelCB_Receive.Remark1);
70 parame[9] = new SqlParameter(_parameRemark2, modelCB_Receive.Remark2);
71 SqlHelper.ExecuteNonQuery(SqlHelper._connString, CommandType.Text, _sqlupdate, parame);
72 }
73 public IList<ModelCB_Receive> GetAll()
74 {
75 IList<ModelCB_Receive> MList = new List<ModelCB_Receive>();
76 using (SqlDataReader sdr = SqlHelper.ExecuteReader(SqlHelper._connString, CommandType.Text, "Select * from CB_Receive"))
77 {
78 while(sdr.Read())
79 {
80 MList.Add(MakeCB_ReceiveModel(sdr));
81 }
82 }
83 return MList;
84 }
85 private ModelCB_Receive MakeCB_ReceiveModel(SqlDataReader sdr)
86 {
87 return new ModelCB_Receive((int)sdr["ReceiveID"] , (string)sdr["OID"] , (string)sdr["Pstatus"] , (string)sdr["Pstring"] , (string)sdr["Pmode"] , (string)sdr["MD5Info"] , (string)sdr["Amount"] , (string)sdr["MoneyType"] , (string)sdr["Remark1"] , (string)sdr["Remark2"]);
88 }
89 }
90}
91
92
BLL逻辑层
1using System;
2using System.Collections.Generic;
3using System.Text;
4using IDAL;
5using Model;
6using DALFactory;
7namespace BLL
8{
9 public class BLLCB_Receive
10 {
11 private static readonly ICB_Receive dal = DALFactory.DataAccess.CreateICB_Receive();
12
13 public bool Insert(ModelCB_Receive modelCB_Receive, out string message)
14 {
15 try
16 {
17 dal.Insert(modelCB_Receive);
18 message = string.Empty;
19 return true;
20 }
21 catch(Exception ex)
22 {
23 message = ex.Message;
24 return false;
25 }
26 }
27
28 public bool Delete(ModelCB_Receive modelCB_Receive, out string message)
29 {
30 try
31 {
32 dal.Delete(modelCB_Receive);
33 message = string.Empty;
34 return true;
35 }
36 catch(Exception ex)
37 {
38 message = ex.Message;
39 return false;
40 }
41 }
42
43 public bool Update(ModelCB_Receive modelCB_Receive, out string message)
44 {
45 try
46 {
47 dal.Update(modelCB_Receive);
48 message = string.Empty;
49 return true;
50 }
51 catch(Exception ex)
52 {
53 message = ex.Message;
54 return false;
55 }
56 }
57 public ModelCB_Receive GetBy(int receiveID)
58 {
59 return dal.GetBy( receiveID);
60 }
61 public IList<ModelCB_Receive> GetAll()
62 {
63 return dal.GetAll();
64 }
65 }
66}
67
1using System;
2using System.Collections.Generic;
3using System.Text;
4using IDAL;
5using Model;
6using DALFactory;
7namespace BLL
8{
9 public class BLLCB_Receive
10 {
11 private static readonly ICB_Receive dal = DALFactory.DataAccess.CreateICB_Receive();
12
13 public bool Insert(ModelCB_Receive modelCB_Receive, out string message)
14 {
15 try
16 {
17 dal.Insert(modelCB_Receive);
18 message = string.Empty;
19 return true;
20 }
21 catch(Exception ex)
22 {
23 message = ex.Message;
24 return false;
25 }
26 }
27
28 public bool Delete(ModelCB_Receive modelCB_Receive, out string message)
29 {
30 try
31 {
32 dal.Delete(modelCB_Receive);
33 message = string.Empty;
34 return true;
35 }
36 catch(Exception ex)
37 {
38 message = ex.Message;
39 return false;
40 }
41 }
42
43 public bool Update(ModelCB_Receive modelCB_Receive, out string message)
44 {
45 try
46 {
47 dal.Update(modelCB_Receive);
48 message = string.Empty;
49 return true;
50 }
51 catch(Exception ex)
52 {
53 message = ex.Message;
54 return false;
55 }
56 }
57 public ModelCB_Receive GetBy(int receiveID)
58 {
59 return dal.GetBy( receiveID);
60 }
61 public IList<ModelCB_Receive> GetAll()
62 {
63 return dal.GetAll();
64 }
65 }
66}
67