-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
144 lines (120 loc) · 5.21 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
using System;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using Sql2005Server.SchemaProvider;
using Sql2005Server;
using SchemaObjects;
using View = SchemaObjects.View;
namespace DataObjectGen
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (SavePath.Text.Trim().Length < 1)
{
MessageBox.Show("Please fill the save path");
return;
}
if (DatabaseName.Text.Trim().Length < 1)
{
MessageBox.Show("Please fill the database name field");
return;
}
string expath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
SQL2005SchemaProvider sm = new SQL2005SchemaProvider();
sm.Connect(@"localhost");
System.Collections.ArrayList dbs = sm.DataBases();
foreach (string dbname in dbs)
{
System.Diagnostics.Debug.WriteLine(dbname);
}
DataBase db = new DataBase();
db.Name = DatabaseName.Text;
SchemaObjects.Tables tbls = sm.Tables(db);
SchemaObjects.Views views = sm.Views(db);
TableToClassTemplate codegen = new TableToClassTemplate(); //generate the table class.
TableToPartialClassTemplate codegen1 = new TableToPartialClassTemplate(); /// generate a partial empty class to add logic in it.
DataTypedSetTemplate typedset = new DataTypedSetTemplate(); // generate a typed dataset for every table .
TableStoredProcedures tbsp = new TableStoredProcedures();
string strClassPrefix = ClassPrefix.Text;
string strNameSpace = NameSpacetxt.Text;
string path = Path.Combine(SavePath.Text, db.Name);
DirectoryInfo df = new DirectoryInfo(path);
if (!df.Exists)
df.Create();
FileInfo oinfo = new FileInfo(Path.Combine( Path.Combine(SavePath.Text, db.Name), "dbStoredProcedures" + @".sql"));
FileStream sqlFile;
if (oinfo.Exists)
sqlFile = oinfo.Open(FileMode.Truncate, FileAccess.ReadWrite);
else
sqlFile = oinfo.Create();
using (StreamWriter sw = new StreamWriter(sqlFile))
{
tbsp.Streamwriter = sw;
foreach (Table tb in db.Tables)
{
codegen.OutputDir = Path.Combine(SavePath.Text, db.Name);
codegen1.OutputDir = codegen.OutputDir;
typedset.OutputDir = codegen1.OutputDir;
tbsp.OutputDir = codegen.OutputDir;
GeneratePages gp=new GeneratePages();
gp.OutputDir = tbsp.OutputDir;
gp.Process(tb,strNameSpace,strClassPrefix,true);
tbsp.Process(tb, strNameSpace, strClassPrefix, true);
codegen.Process(tb, strNameSpace, strClassPrefix, true);
typedset.Process(tb, strNameSpace, strClassPrefix, true);
codegen1.Process(tb, strNameSpace, strClassPrefix, true);
typedset.SaveToFile();
codegen1.SaveToFile();
codegen.SaveToFile();
tbsp.SaveToFile();
}
foreach (View view in db.Views)
{
ViewToClass vtc=new ViewToClass();
vtc.OutputDir = Path.Combine(SavePath.Text, db.Name);
vtc.Process(view,strNameSpace,strClassPrefix,true);
vtc.SaveToFile();
}
sw.Flush();
sw.Close();
}
SaveBaseObject(strNameSpace,Path.Combine( Path.Combine(SavePath.Text, db.Name),"DataBaseObject.cs"));
MessageBox.Show("Files Has been generated.");
//this.Close();
}
private void SaveBaseObject(string NameSpace,string Filename)
{
Assembly asm = Assembly.GetExecutingAssembly();
Stream stream = asm.GetManifestResourceStream("DataObjectGen.DataObjectBase.txt");
StreamReader sr = new StreamReader(stream);
string filecontent = sr.ReadToEnd().Replace("{0}", NameSpace);
filecontent = filecontent.Replace("{1}", "localhost");
filecontent = filecontent.Replace("{2}", DatabaseName.Text);
filecontent = filecontent.Replace("{3}","sa" );
filecontent = filecontent.Replace("{4}", "p7z6y1f9");
FileInfo oinfo = new FileInfo(Filename);
FileStream sqlFile;
if (oinfo.Exists)
sqlFile = oinfo.Open(FileMode.Truncate, FileAccess.ReadWrite);
else
sqlFile = oinfo.Create();
using (StreamWriter sw = new StreamWriter(sqlFile))
{
sw.Write(filecontent);
sw.Flush();
sw.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}