-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
350 lines (334 loc) · 11.7 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 实体生成器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DBService DB_one = new DBService();
private void Form1_Load(object sender, EventArgs e)
{
}
void BindDataBase(ComboBox c, List<string> list)
{
c.Items.Clear();
foreach (var item in list)
{
c.Items.Add(item);
}
}
private void button_one_conn_Click(object sender, EventArgs e)
{
DB_one.Connonection = string.Format("Data Source={0};Initial Catalog={1};User ID={2};PassWord={3};persist security info=True;Max Pool Size=60000;", textBox_ont_ip.Text, textBox_dataBaseName.Text, textBox_one_user.Text, textBox_one_pwd.Text);
BindDataBase(comboBox_one_dataBase, DB_one.GetAllDataBase());
}
private void comboBox_one_dataBase_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox_one_dataBase.SelectedIndex >= 0)
{
var str = GetStringMid(DB_one.Connonection, "Catalog=", ";");
DB_one.Connonection = DB_one.Connonection.Replace(str, comboBox_one_dataBase.Text);
IList <string> list = DB_one.GetTableNames(comboBox_one_dataBase.Text);
listView_one_tables.Items.Clear();
int index = 1;
foreach (var item in list)
{
listView_one_tables.Items.Add(new ListViewItem(new string[] {
index++.ToString(),
item
}));
}
}
}
private void listView_one_tables_ChangeUICues(object sender, UICuesEventArgs e)
{
}
private void listView_one_tables_Click(object sender, EventArgs e)
{
if (listView_one_tables.SelectedItems.Count == 1)
{
List<Field> list = DB_one.GetFileds(listView_one_tables.SelectedItems[0].SubItems[1].Text);
listView_one_tableStruct.Items.Clear();
int index = 1;
foreach (Field item in list)
{
listView_one_tableStruct.Items.Add(new ListViewItem(new string[] {
index++.ToString(),
item.Name,
item.Type
}));
}
}
}
private void button_toClass_Click(object sender, EventArgs e)
{
Entity();
}
private void Entity()
{
string tableName = listView_one_tables.SelectedItems[0].SubItems[1].Text;
StringBuilder sb = new StringBuilder();
sb.AppendLine("public class " + tableName);
sb.AppendLine("{");
foreach (ListViewItem item in listView_one_tableStruct.Items)
{
string typename = ChangeToCSharpType(item.SubItems[2].Text);
string name = item.SubItems[1].Text;
name = name.Substring(0, 1) + name.Substring(1);
sb.Append(" public " + typename + " " + name);
sb.Append(" {");
sb.Append("get;set;");
sb.AppendLine("}");
}
sb.AppendLine("}");
textBox_result.Text = sb.ToString();
}
private void WpfEntity()
{
string tableName = listView_one_tables.SelectedItems[0].SubItems[1].Text;
StringBuilder sb = new StringBuilder();
sb.AppendLine(" public class " + tableName);
sb.AppendLine("{");
foreach (ListViewItem item in listView_one_tableStruct.Items)
{
string typename = ChangeToCSharpType(item.SubItems[2].Text);
string name = item.SubItems[1].Text;
name = name.Substring(0, 1) + name.Substring(1);
sb.AppendLine("private " + typename + " _" + name + ";");
sb.AppendLine(" public " + typename + " " + name);
sb.AppendLine("{");
sb.AppendLine("get=>_" + name + ";");
sb.AppendLine("set=>Set(ref _" + name + ", value);");
sb.AppendLine("}");
}
sb.AppendLine("}");
textBox_result.Text = sb.ToString();
}
private void GoEntity(string type)
{
string tableName = listView_one_tables.SelectedItems[0].SubItems[1].Text;
StringBuilder sb = new StringBuilder();
sb.AppendLine("type " + tableName + " struct {");
StringBuilder cols = new StringBuilder();
foreach (ListViewItem item in listView_one_tableStruct.Items)
{
string typename = GoChangeToCSharpType(item.SubItems[2].Text);
string name = item.SubItems[1].Text;
name = name.Substring(0, 1).ToUpper() + name.Substring(1);
sb.AppendLine(name + " " + typename + " `" + type + ":\"" + name + ",omitempty\" json:\"" + name + "\"`");
cols.Append("&tableStruct."+name+",");
}
sb.AppendLine("}");
//sb.AppendLine("rows.Scan(" + cols .ToString().TrimEnd(',')+ ")");
textBox_result.Text = sb.ToString();
}
private string GoChangeToCSharpType(string type)
{
string reval = string.Empty;
switch (type.ToLower())
{
case "int":
reval = "int64";
break;
case "text":
reval = "sql.NullString";
break;
case "bigint":
reval = "int64";
break;
case "binary":
reval = "Byte[]";
break;
case "bit":
reval = "bool";
break;
case "char":
reval = "sql.NullString";
break;
case "datetime":
reval = "time.Time";
break;
case "decimal":
reval = "float64";
break;
case "float":
reval = "float64";
break;
case "money":
reval = "float64";
break;
case "nchar":
reval = "string";
break;
case "ntext":
reval = "string";
break;
case "numeric":
reval = "float64";
break;
case "nvarchar":
reval = "sql.NullString";
break;
case "smalldatetime":
reval = "time.Time";
break;
case "smallint":
reval = "int16";
break;
case "timestamp":
reval = "time.Time";
break;
case "tinyint":
reval = "byte";
break;
case "varchar":
reval = "sql.NullString";
break;
default:
reval = "sql.NullString";
break;
}
return reval;
}
/// <summary>
/// 数据库中与C#中的数据类型对照
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
private string ChangeToCSharpType(string type)
{
string reval = string.Empty;
switch (type.ToLower())
{
case "int":
reval = "int";
break;
case "text":
reval = "string";
break;
case "bigint":
reval = "long";
break;
case "binary":
reval = "Byte[]";
break;
case "bit":
reval = "bool";
break;
case "char":
reval = "string";
break;
case "datetime":
reval = "DateTime";
break;
case "decimal":
reval = "Decimal";
break;
case "float":
reval = "double";
break;
case "image":
reval = "Byte[]";
break;
case "money":
reval = "decimal";
break;
case "nchar":
reval = "string";
break;
case "ntext":
reval = "string";
break;
case "numeric":
reval = "decimal";
break;
case "nvarchar":
reval = "string";
break;
case "real":
reval = "Single";
break;
case "smalldatetime":
reval = "DateTime";
break;
case "smallint":
reval = "Int16";
break;
case "smallmoney":
reval = "decimal";
break;
case "timestamp":
reval = "DateTime";
break;
case "tinyint":
reval = "Byte";
break;
case "uniqueidentifier":
reval = "Guid";
break;
case "varbinary":
reval = "Byte[]";
break;
case "varchar":
reval = "string";
break;
case "Variant":
reval = "Object";
break;
default:
reval = "string";
break;
}
return reval;
}
private void textBox_result_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\x1')
{
((TextBox)sender).SelectAll();
e.Handled = true;
}
}
/// <summary>
/// 取中间值
/// </summary>
/// <param name="allStr"></param>
/// <param name="firstStr"></param>
/// <param name="lastStr"></param>
/// <returns></returns>
public static string GetStringMid(string allStr, string firstStr, string lastStr)
{
int index1 = allStr.IndexOf(firstStr);
int index2 = allStr.IndexOf(lastStr, index1 + firstStr.Length);
if (index1 < 0 || index2 < 0)
{
return "";
}
index1 = index1 + firstStr.Length;
index2 = index2 - index1;
if (index1 < 0 || index2 < 0)
{
return "";
}
return allStr.Substring(index1, index2);
}
private void button1_Click(object sender, EventArgs e)
{
GoEntity("bson");
}
private void button2_Click(object sender, EventArgs e)
{
GoEntity("db");
}
}
}