首先说一下为什么要写这个.NET实体类生成器。
在网上搜的代码生成器感觉不能满足自己的要求,因此我想写一个适合自己的。
如果是自己写,像这个是不需要自己手写。。。。。。哈哈,我就是这么懒,所以就决定自己写咯。
做成什么样,才是我想要的呢?
1.能够自己选择数据库
2.能够自己选择选中数据库的表
3.能够自己输入类名和类说明
4.能够自己选择是否有命名空间,若选择有命名空间,能够自己输入名字
5.双击能够修改字段、字段类型、说明。
但是怎么做呢?有几个问题。。。。。
1.怎么获取所有的数据库
2.怎么获取某个数据库下的所有表(用户表)
3.怎么获取数据库中表的所有字段、字段类型、说明
4.选择什么控件呈现表的所有字段、字段类型、说明
5.怎么生成实体类
6.对了,sqlserver的数据类型跟C#数据类型是不匹配的,它们的匹配规则是?
baidu,google后,问题都解决了。
1.怎么获取所有的数据库:
来自互联网:
select name as 'name' from master..sysdatabases order by name
2.怎么获取某个数据库下的所有表(用户表)
来自互联网:
use '数据库名' Select name from sysobjects where xtype= 'u '
3.怎么获取数据库中表的所有字段、字段类型、说明
来自互联网:
1 SELECT
2 ColumnName=C.name,
3 Type=T.name,
4 ColumnDesc=ISNULL(PFD.[value],N'')
5 FROM sys.columns C
6 INNER JOIN sys.objects O
7 ON C.[object_id]=O.[object_id]
8 AND O.type='U'
9 AND O.is_ms_shipped=0
10 INNER JOIN sys.types T
11 ON C.user_type_id=T.user_type_id
12 LEFT JOIN sys.default_constraints D
13 ON C.[object_id]=D.parent_object_id
14 AND C.column_id=D.parent_column_id
15 AND C.default_object_id=D.[object_id]
16 LEFT JOIN sys.extended_properties PFD
17 ON PFD.class=1
18 AND C.[object_id]=PFD.major_id
19 AND C.column_id=PFD.minor_id
20 -- AND PFD.name='Caption' -- 字段说明对应的描述名称(一个字段可以添加多个不同name的描述)
21 LEFT JOIN sys.extended_properties PTB
22 ON PTB.class=1
23 AND PTB.minor_id=0
24 AND C.[object_id]=PTB.major_id
25 -- AND PFD.name='Caption' -- 表说明对应的描述名称(一个表可以添加多个不同name的描述)
26
27 LEFT JOIN -- 索引及主键信息
28 (
29 SELECT
30 IDXC.[object_id],
31 IDXC.column_id,
32 Sort=CASE INDEXKEY_PROPERTY(IDXC.[object_id],IDXC.index_id,IDXC.index_column_id,'IsDescending')
33 WHEN 1 THEN 'DESC' WHEN 0 THEN 'ASC' ELSE '' END,
34 PrimaryKey=CASE WHEN IDX.is_primary_key=1 THEN N'√'ELSE N'' END,
35 IndexName=IDX.Name
36 FROM sys.indexes IDX
37 INNER JOIN sys.index_columns IDXC
38 ON IDX.[object_id]=IDXC.[object_id]
39 AND IDX.index_id=IDXC.index_id
40 LEFT JOIN sys.key_constraints KC
41 ON IDX.[object_id]=KC.[parent_object_id]
42 AND IDX.index_id=KC.unique_index_id
43 INNER JOIN -- 对于一个列包含多个索引的情况,只显示第1个索引信息
44 (
45 SELECT [object_id], Column_id, index_id=MIN(index_id)
46 FROM sys.index_columns
47 GROUP BY [object_id], Column_id
48 ) IDXCUQ
49 ON IDXC.[object_id]=IDXCUQ.[object_id]
50 AND IDXC.Column_id=IDXCUQ.Column_id
51 AND IDXC.index_id=IDXCUQ.index_id
52 ) IDX
53 ON C.[object_id]=IDX.[object_id]
54 AND C.column_id=IDX.column_id
55
56 WHERE O.name=N'{表名}' -- 如果只查询指定表,加上此条件
57 ORDER BY O.name,C.column_id
4.选择什么控件呈现表的所有字段、字段类型、说明
DataGridView、ListView都可以,我用了DataGridView
5.怎么生成实体类
用写入流
6.sqlserver的数据类型跟C#数据类型是不匹配的,它们的匹配规则是?
查阅了互联网上sqlserver与C#数据类型对应图,方法自己写的:
1 /// <summary>
2 /// C#数据类型匹配sqlserver数据类型
3 /// </summary>
4 /// <param name="str">sqlserver数据类型</param>
5 /// <returns></returns>
6 public string PiPei(string str) {
7 switch (str)
8 {
9 case "bigint":
10 return "Int64";
11 case "binary":
12 return "Byte[]";
13 case "bit":
14 return "bool";
15 case "char":
16 return "string";
17 case "date":
18 return "DateTime";
19 case "datetime":
20 return "DateTime";
21 case "datetime2":
22 return "DateTime2";
23 case "datetimeoffset":
24 return "DateTimeOffset";
25 case "decimal":
26 return "Decimal";
27 case "varbinary(max)":
28 return "Byte[]";
29 case "float":
30 return "Double";
31 case "image":
32 return "Byte[]";
33 case "int":
34 return "int";
35 case "money":
36 return "Decimal";
37 case "nchar":
38 return "string";
39 case "ntext":
40 return "string";
41 case "numeric":
42 return "Decimal";
43 case "nvarchar":
44 return "string";
45 case "real":
46 return "Single";
47 case "rowversion":
48 return "Byte[]";
49 case "smalldatetime":
50 return "DateTime";
51 case "smallint":
52 return "Int16";
53 case "smallmoney":
54 return "Decimal";
55 case "sql_variant":
56 return "Object*";
57 case "text":
58 return "string";
59 case "time":
60 return "TimeSpan";
61 case "timestamp":
62 return "Byte[]";
63 case "tinyint":
64 return "Byte";
65 case "uniqueidentifier":
66 return "Guid";
67 case "varbinary":
68 return "Byte[]";
69 case "varchar":
70 return "string";
71 case "xml":
72 return "Xml";
73 default:
74 return "string";
75 }
76 }
界面:
申明:该界面下半部分是模仿别人代码生成器布局的
后台代码:
1 /// <summary>
2 /// 创建相对应的.NET实体类
3 /// </summary>
4 /// <param name="className">类名</param>
5 /// <param name="classRemarks">类说明</param>
6 private void writeFile(string className, string classRemarks)
7 {
8 // 创建文件流
9 FileStream fs = new FileStream(sfdFile.FileName, FileMode.Create, FileAccess.Write);
10 // 创建写入流
11 StreamWriter sw = new StreamWriter(fs, Encoding.Default);
12
13 // 写入内容
14 sw.WriteLine("/* ");
15 sw.WriteLine(" * 作者:Oven");
16 sw.WriteLine(" * 创建时间:" + DateTime.Now.ToString());
17 if (classRemarks.Length != 0)
18 {
19 sw.WriteLine(" * 类说明:" + classRemarks);
20 }
21 sw.WriteLine(" */ ");
22 sw.WriteLine("using System;");
23 sw.WriteLine("using System.Collections;");
24 sw.WriteLine("using System.Collections.Generic;");
25
26 if (radYou.Checked && txtNameSpace.Text.Trim() != null && txtNameSpace.Text.Trim() != "")
27 {
28 sw.WriteLine("namespace " + txtNameSpace.Text.Trim());
29 sw.WriteLine("{");
30 }
31
32 if (classRemarks.Length != 0)
33 {
34 sw.WriteLine(" /// <summary>");
35 sw.WriteLine(" /// " + classRemarks);
36 sw.WriteLine(" /// </summary>");
37 }
38
39 sw.WriteLine(" public class " + className);
40 sw.WriteLine(" {");
41
42 foreach (DataGridViewRow row in dgvContent.Rows)
43 {
44 if (row.Cells[0].Value != null && row.Cells[1].Value != null)
45 {
46 string propName = row.Cells[0].Value.ToString(); // 获取属性名
47
48 string functionName = getFunName(propName);
49
50 string type = row.Cells[1].Value.ToString(); // 获取类型
51
52 sw.WriteLine(" private " + type + " " + propName + ";");
53
54 if (row.Cells[2].Value != null) // 判断备注是否为空.
55 {
56 sw.WriteLine(" /// <summary>");
57 sw.WriteLine(" /// " + row.Cells[2].Value.ToString());
58 sw.WriteLine(" /// </summary>");
59 }
60
61 sw.WriteLine(" public " + type + " " + functionName);
62 sw.WriteLine(" {");
63 sw.WriteLine(" get { return " + propName + "; }");
64 sw.WriteLine(" set { " + propName + " = value; }");
65 sw.WriteLine(" }");
66 sw.WriteLine("");
67 }
68 }
69
70 sw.WriteLine(" }");
71
72 if (radYou.Checked && txtNameSpace.Text.Trim() != null && txtNameSpace.Text.Trim() != "")
73 {
74 sw.WriteLine("}");
75 }
76
77 // 关闭流
78 sw.Close();
79 fs.Close();
80 }
81
82 /// <summary>
83 /// 把字符串首字母转换为大写
84 /// </summary>
85 /// <param name="propName">要转换的字符串</param>
86 /// <returns>反正首字母大写后的字符串</returns>
87 private static string getFunName(string propName)
88 {
89 // 替换属性名前的一个或者多个_ ,
90 propName = Regex.Replace(propName, "^_+", "");
91
92 // 把propName变成首字母为大写的字符串
93 string functionName = propName.Substring(0, 1).ToUpper() + propName.Substring(1);
94 return functionName;
95 }
96
97 private void radWu_CheckedChanged(object sender, EventArgs e)
98 {
99 txtNameSpace.Visible = false; // 命名空间文本框不可见
100 }
101
102 private void radYou_CheckedChanged(object sender, EventArgs e)
103 {
104 txtNameSpace.Visible = true; // 命名空间文本框可见
105 }
106
107 SqlConnection con;
108 private void btnConnect_Click(object sender, EventArgs e)
109 {
110 try
111 {
112 con = new SqlConnection(txtConnetionString.Text);
113 con.Open();
114 adapter = new SqlDataAdapter("select name as 'name' from master..sysdatabases order by name", con);
115 DataTable db = new DataTable();
116 adapter.Fill(db);
117 this.cbChooseDatabase.DataSource = db;
118 this.cbChooseDatabase.DisplayMember = "name";
119 MessageBox.Show("测试连接成功");
125 }
126 catch (Exception ex)
127 {
128 MessageBox.Show(ex.Message);
129 }
130 }
137 private void cbChooseTable_SelectedIndexChanged(object sender, EventArgs e)
138 {
139 string sql = string.Format(@"SELECT
140 ColumnName=C.name,
141 Type=T.name,
142 ColumnDesc=ISNULL(PFD.[value],N'')
143 FROM sys.columns C
144 INNER JOIN sys.objects O
145 ON C.[object_id]=O.[object_id]
146 AND O.type='U'
147 AND O.is_ms_shipped=0
148 INNER JOIN sys.types T
149 ON C.user_type_id=T.user_type_id
150 LEFT JOIN sys.default_constraints D
151 ON C.[object_id]=D.parent_object_id
152 AND C.column_id=D.parent_column_id
153 AND C.default_object_id=D.[object_id]
154 LEFT JOIN sys.extended_properties PFD
155 ON PFD.class=1
156 AND C.[object_id]=PFD.major_id
157 AND C.column_id=PFD.minor_id
158 -- AND PFD.name='Caption' -- 字段说明对应的描述名称(一个字段可以添加多个不同name的描述)
159 LEFT JOIN sys.extended_properties PTB
160 ON PTB.class=1
161 AND PTB.minor_id=0
162 AND C.[object_id]=PTB.major_id
163 -- AND PFD.name='Caption' -- 表说明对应的描述名称(一个表可以添加多个不同name的描述)
164
165 LEFT JOIN -- 索引及主键信息
166 (
167 SELECT
168 IDXC.[object_id],
169 IDXC.column_id,
170 Sort=CASE INDEXKEY_PROPERTY(IDXC.[object_id],IDXC.index_id,IDXC.index_column_id,'IsDescending')
171 WHEN 1 THEN 'DESC' WHEN 0 THEN 'ASC' ELSE '' END,
172 PrimaryKey=CASE WHEN IDX.is_primary_key=1 THEN N'√'ELSE N'' END,
173 IndexName=IDX.Name
174 FROM sys.indexes IDX
175 INNER JOIN sys.index_columns IDXC
176 ON IDX.[object_id]=IDXC.[object_id]
177 AND IDX.index_id=IDXC.index_id
178 LEFT JOIN sys.key_constraints KC
179 ON IDX.[object_id]=KC.[parent_object_id]
180 AND IDX.index_id=KC.unique_index_id
181 INNER JOIN -- 对于一个列包含多个索引的情况,只显示第1个索引信息
182 (
183 SELECT [object_id], Column_id, index_id=MIN(index_id)
184 FROM sys.index_columns
185 GROUP BY [object_id], Column_id
186 ) IDXCUQ
187 ON IDXC.[object_id]=IDXCUQ.[object_id]
188 AND IDXC.Column_id=IDXCUQ.Column_id
189 AND IDXC.index_id=IDXCUQ.index_id
190 ) IDX
191 ON C.[object_id]=IDX.[object_id]
192 AND C.column_id=IDX.column_id
193
194 WHERE O.name=N'{0}' -- 如果只查询指定表,加上此条件
195 ORDER BY O.name,C.column_id
196
197
198
199 ",this.cbChooseTable.Text);
200 adapter = new SqlDataAdapter(sql,con);
201 DataTable db = new DataTable();
202 adapter.Fill(db);
203 for (int i = 0; i < db.Rows.Count; i++)
204 {
205 db.Rows[i][1] = PiPei(db.Rows[i][1].ToString());
206 }
207 this.dgvContent.DataSource = db;
208 }
209
210 SqlDataAdapter adapter;
211 bool biaozhi = false;//标志
212 private void cbChooseDatabase_SelectedIndexChanged(object sender, EventArgs e)
213 {
214 if(biaozhi){
215 DataTable db = new DataTable();
216 adapter.SelectCommand.CommandText = "use " + this.cbChooseDatabase.Text + " Select name from sysobjects where xtype= 'u '";
217 adapter.Fill(db);
218 this.cbChooseTable.DataSource = db;
219 this.cbChooseTable.DisplayMember = "name";
220 DataTable db2 = new DataTable();
221 adapter.SelectCommand.CommandText = "use " + this.cbChooseDatabase.Text + " Select name from sysobjects where xtype= 'u '";
222 adapter.Fill(db2);
223 this.cbChooseTable.DataSource = db2;
224 this.cbChooseTable.DisplayMember = "name";
225 }
226 biaozhi = true;
227 }
228
229 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
230 {
231 if(con!=null && con.State == ConnectionState.Open){
232 con.Close();
233 }
234 }
235
236 /// <summary>
237 /// C#数据类型匹配sqlserver数据类型
238 /// </summary>
239 /// <param name="str">sqlserver数据类型</param>
240 /// <returns></returns>
241 public string PiPei(string str) {
242 switch (str)
243 {
244 case "bigint":
245 return "Int64";
246 case "binary":
247 return "Byte[]";
248 case "bit":
249 return "bool";
250 case "char":
251 return "string";
252 case "date":
253 return "DateTime";
254 case "datetime":
255 return "DateTime";
256 case "datetime2":
257 return "DateTime2";
258 case "datetimeoffset":
259 return "DateTimeOffset";
260 case "decimal":
261 return "Decimal";
262 case "varbinary(max)":
263 return "Byte[]";
264 case "float":
265 return "Double";
266 case "image":
267 return "Byte[]";
268 case "int":
269 return "int";
270 case "money":
271 return "Decimal";
272 case "nchar":
273 return "string";
274 case "ntext":
275 return "string";
276 case "numeric":
277 return "Decimal";
278 case "nvarchar":
279 return "string";
280 case "real":
281 return "Single";
282 case "rowversion":
283 return "Byte[]";
284 case "smalldatetime":
285 return "DateTime";
286 case "smallint":
287 return "Int16";
288 case "smallmoney":
289 return "Decimal";
290 case "sql_variant":
291 return "Object*";
292 case "text":
293 return "string";
294 case "time":
295 return "TimeSpan";
296 case "timestamp":
297 return "Byte[]";
298 case "tinyint":
299 return "Byte";
300 case "uniqueidentifier":
301 return "Guid";
302 case "varbinary":
303 return "Byte[]";
304 case "varchar":
305 return "string";
306 case "xml":
307 return "Xml";
308 default:
309 return "string";
310 }
311 }
工具下载:
http://115.com/file/be6hf643#ClassGenerate.exe
源码下载:
http://115.com/file/c2ukijgv#dotnet实体类生成器.zip