C#网页版代码生成器,还在更新中....以前修复过ASPNETPAGER分页控件的网页版存储过程代码生成器,学到了一种思维,替换字符串,因此有空就写下了这个。仅供参考,本人水平还有待提高,写这个完全是兴趣,写的不好的地方不要骂我就行,呵呵。
存储过程代码如下,参考MSSQL SERVER文档。
存储过程代码
1
2 ALTER proc [dbo].[GetTablesFromDatabase]
3 @dataBaseName nvarchar(200)
4 as
5 declare @sql nvarchar(2000);
6 set @sql='SELECT TABLE_CATALOG as "数据库", TABLE_SCHEMA as "表架构" , TABLE_NAME as "表名" ,TABLE_TYPE as "表类型" FROM INFORMATION_SCHEMA.TABLES
7 where TABLE_CATALOG=N'+@dataBaseName+' order by TABLE_NAME '; --get the role !
8 --SELECT * FROM INFORMATION_SCHEMA.TABLES
9 --where TABLE_CATALOG='TestSub' --get the schema from databse
10 exec(@sql);
11 print(@sql);
12
13 ALTER proc [dbo].[GetColumnsFromTable]
14 @dataBaseName nvarchar(200),
15 @tableName nvarchar(200)
16 as
17 declare @dataBase nvarchar(200);
18 set @dataBase=rtrim(@dataBaseName)+'.INFORMATION_SCHEMA.COLUMNS';
19 declare @sql nvarchar(2000);
20 --set @sql='SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, COLUMN_DEFAULT
21 --FROM '+@dataBaseName+'.INFORMATION_SCHEMA.COLUMNS
22 --WHERE TABLE_NAME =N'+@tableName+';'
23 set @sql='SELECT TABLE_CATALOG as "数据库", TABLE_SCHEMA as "表架构" , TABLE_NAME as "表名", COLUMN_NAME as "列名",DATA_TYPE as "列数据类型"
24 FROM '+@dataBase
25 +' WHERE TABLE_NAME =N'+@tableName+' order by COLUMN_NAME';
26 exec(@sql);
27 print(@sql);
2 ALTER proc [dbo].[GetTablesFromDatabase]
3 @dataBaseName nvarchar(200)
4 as
5 declare @sql nvarchar(2000);
6 set @sql='SELECT TABLE_CATALOG as "数据库", TABLE_SCHEMA as "表架构" , TABLE_NAME as "表名" ,TABLE_TYPE as "表类型" FROM INFORMATION_SCHEMA.TABLES
7 where TABLE_CATALOG=N'+@dataBaseName+' order by TABLE_NAME '; --get the role !
8 --SELECT * FROM INFORMATION_SCHEMA.TABLES
9 --where TABLE_CATALOG='TestSub' --get the schema from databse
10 exec(@sql);
11 print(@sql);
12
13 ALTER proc [dbo].[GetColumnsFromTable]
14 @dataBaseName nvarchar(200),
15 @tableName nvarchar(200)
16 as
17 declare @dataBase nvarchar(200);
18 set @dataBase=rtrim(@dataBaseName)+'.INFORMATION_SCHEMA.COLUMNS';
19 declare @sql nvarchar(2000);
20 --set @sql='SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, COLUMN_DEFAULT
21 --FROM '+@dataBaseName+'.INFORMATION_SCHEMA.COLUMNS
22 --WHERE TABLE_NAME =N'+@tableName+';'
23 set @sql='SELECT TABLE_CATALOG as "数据库", TABLE_SCHEMA as "表架构" , TABLE_NAME as "表名", COLUMN_NAME as "列名",DATA_TYPE as "列数据类型"
24 FROM '+@dataBase
25 +' WHERE TABLE_NAME =N'+@tableName+' order by COLUMN_NAME';
26 exec(@sql);
27 print(@sql);
页面代码如下:
页面布局代码
1 <html xmlns="http://www.w3.org/1999/xhtml">
2 <head runat="server">
3 <title>无标题页</title>
4 <style type="text/css" >
5 .dg-HeaderStyle {
6 background-color: #E3E3E3;
7 font-size: 12px;
8 font-weight: bold;
9 text-indent: 5px;
10 line-height: 15px;
11 }
12 .dg-ItemStyle {
13 text-indent: 5px;
14 font-size: 12px;
15 line-height: 15px;
16 }
17 .textInput { 185px; height:22px; float:left;}
18 .textInput_focus { border:solid red 1px; height:20px; line-height:20px; background:url(/DataBaseTablesColumns/textInput_bg.gif) repeat-x left; 175px; padding:0 0 0 5px;}
19 .textInput_blur { border:solid blue 1px; height:20px; line-height:20px; background:url(/DataBaseTablesColumns/textInput_bg.gif) repeat-x left;175px; padding:0 0 0 5px;}
20
21 </style>
22 </head>
23 <body>
24 <form id="form1" runat="server">
25 <div>
26 <div style="100%; height:28px; background-color:Silver;">
27 <p style="border-style: groove; border-color: #0000FF; color: #CC00FF; text-decoration: underline; text-align: center; font-weight: bold; background-color: #C0C0C0;"> Maded By JasenKin 2010/01/11 00:53</p></div>
28
29 <div style="45%; float: left;">
30 <p style="font:blue;">第一步:选择数据库</p>
31 <asp:DropDownList ID="ddlDataBase" runat="server">
32 </asp:DropDownList><br />
33 <asp:Button ID="btnShowTables" runat="server" Text="Show Tables From DataBase"
34 onclick="btnShowTables_Click" BackColor="Silver" BorderStyle="Groove"
35 Font-Bold="True" />
36 <br />
37 <asp:DataGrid ID="dg" runat="server" AutoGenerateColumns="False" CellPadding="3"
38 CellSpacing="0" PageSize="1" OnItemCommand="dg_ItemCommand"
39 style="margin-right: 17px">
40 <HeaderStyle CssClass="dg-HeaderStyle"></HeaderStyle>
41 <ItemStyle CssClass="dg-ItemStyle" />
42 <Columns>
43 <asp:TemplateColumn HeaderText="数据库">
44 <HeaderStyle Width="50px" />
45 <ItemStyle Width="50px" />
46 <ItemTemplate>
47 <%# DataBinder.Eval(Container.DataItem, "数据库")%>
48 </ItemTemplate>
49 </asp:TemplateColumn>
50 <asp:TemplateColumn HeaderText="表架构">
51 <HeaderStyle Width="50px" />
52 <ItemStyle Width="50px" />
53 <ItemTemplate>
54 <%# DataBinder.Eval(Container.DataItem, "表架构").ToString()%>
55 </ItemTemplate>
56 </asp:TemplateColumn>
57 <asp:TemplateColumn HeaderText="表名">
58 <HeaderStyle Width="50px" />
59 <ItemStyle Width="50px" />
60 <ItemTemplate>
61 <%# GetTableName(Container.DataItem)%>
62 </ItemTemplate>
63 </asp:TemplateColumn>
64 <asp:TemplateColumn HeaderText="表类型">
65 <HeaderStyle Width="100px" />
66 <ItemStyle Width="100px" />
67 <ItemTemplate>
68 <%# DataBinder.Eval(Container.DataItem, "表类型").ToString()%>
69 </ItemTemplate>
70 </asp:TemplateColumn>
71 <asp:TemplateColumn HeaderText="操作">
72 <HeaderStyle Width="65px" />
73 <ItemStyle Width="65px" />
74 <ItemTemplate>
75 <asp:LinkButton ID="edit" CommandName="edit" ForeColor="#FF0000" runat="server">编辑</asp:LinkButton>
76 <asp:LinkButton ID="del" CommandName="del" ForeColor="#FF0000" runat="server">删除</asp:LinkButton>
77 </ItemTemplate>
78 </asp:TemplateColumn>
79 </Columns>
80 </asp:DataGrid>
81 </div>
82
83 <div style="45%; float:right;">
84 <p style="font:blue;">第二步:选择表名</p>
85 <asp:DropDownList ID="ddlTablesName" runat="server"
86 onselectedindexchanged="ddlTablesName_SelectedIndexChanged"
87 Visible="False" AutoPostBack="True" >
88 </asp:DropDownList>
89 <br />
90 <asp:DataGrid ID="gwShowColumn" runat="server" AutoGenerateColumns="False" CellPadding="3"
91 CellSpacing="0" PageSize="1" style="margin-right: 17px">
92 <HeaderStyle CssClass="dg-HeaderStyle"></HeaderStyle>
93 <ItemStyle CssClass="dg-ItemStyle" />
94 <Columns>
95 <asp:TemplateColumn HeaderText="数据库">
96 <HeaderStyle Width="70px" />
97 <ItemStyle Width="70px" />
98 <ItemTemplate>
99 <%# DataBinder.Eval(Container.DataItem, "数据库")%>
100 </ItemTemplate>
101 </asp:TemplateColumn>
102 <asp:TemplateColumn HeaderText="表架构">
103 <HeaderStyle Width="50px" />
104 <ItemStyle Width="50px" />
105 <ItemTemplate>
106 <%# DataBinder.Eval(Container.DataItem, "表架构").ToString()%>
107 </ItemTemplate>
108 </asp:TemplateColumn>
109 <asp:TemplateColumn HeaderText="表名">
110 <HeaderStyle Width="70px" />
111 <ItemStyle Width="70px" />
112 <ItemTemplate>
113 <%# GetTableName(Container.DataItem)%>
114 </ItemTemplate>
115 </asp:TemplateColumn>
116 <asp:TemplateColumn HeaderText="列名">
117 <HeaderStyle Width="70px" />
118 <ItemStyle Width="70px" />
119 <ItemTemplate>
120 <%# DataBinder.Eval(Container.DataItem, "列名").ToString()%>
121 </ItemTemplate>
122 </asp:TemplateColumn>
123 <asp:TemplateColumn HeaderText="列数据类型">
124 <HeaderStyle Width="70px" />
125 <ItemStyle Width="70px" />
126 <ItemTemplate>
127 <%# DataBinder.Eval(Container.DataItem, "列数据类型").ToString()%>
128 </ItemTemplate>
129 </asp:TemplateColumn>
130 </Columns>
131 </asp:DataGrid>
132 <br />
133 <p style="font:blue;">第三步:指定命名空间,点击按钮获取数据库表对象模型</p>
134 Input NameSpace Name:<asp:TextBox ID="tbNameSpace" runat="server" CssClass="textInput_blur" onfocus="this.className='textInput_focus'"
135 onblur="this.className='textInput_blur'"></asp:TextBox>
136 <br />
137 <asp:Button ID="btnCreate" runat="server" Text="Create Object Model"
138 onclick="btnCreate_Click" BorderColor="Silver" BorderStyle="Groove"
139 Font-Bold="True" />
140 <br />
141 <asp:Button ID="btnCreateFile" runat="server" Text="CreateFile" OnClick="btnCreateFile_Click" /><br />
142
143 <asp:Label ID="lblShow" runat="server" BackColor="Silver" BorderColor="Blue"
144 BorderStyle="Solid"></asp:Label>
145 </div>
146 <br />
147 <div>
148
149
150
151 </div>
152
153
154 </div>
155 </form>
156 </body>
157 </html>
158
2 <head runat="server">
3 <title>无标题页</title>
4 <style type="text/css" >
5 .dg-HeaderStyle {
6 background-color: #E3E3E3;
7 font-size: 12px;
8 font-weight: bold;
9 text-indent: 5px;
10 line-height: 15px;
11 }
12 .dg-ItemStyle {
13 text-indent: 5px;
14 font-size: 12px;
15 line-height: 15px;
16 }
17 .textInput { 185px; height:22px; float:left;}
18 .textInput_focus { border:solid red 1px; height:20px; line-height:20px; background:url(/DataBaseTablesColumns/textInput_bg.gif) repeat-x left; 175px; padding:0 0 0 5px;}
19 .textInput_blur { border:solid blue 1px; height:20px; line-height:20px; background:url(/DataBaseTablesColumns/textInput_bg.gif) repeat-x left;175px; padding:0 0 0 5px;}
20
21 </style>
22 </head>
23 <body>
24 <form id="form1" runat="server">
25 <div>
26 <div style="100%; height:28px; background-color:Silver;">
27 <p style="border-style: groove; border-color: #0000FF; color: #CC00FF; text-decoration: underline; text-align: center; font-weight: bold; background-color: #C0C0C0;"> Maded By JasenKin 2010/01/11 00:53</p></div>
28
29 <div style="45%; float: left;">
30 <p style="font:blue;">第一步:选择数据库</p>
31 <asp:DropDownList ID="ddlDataBase" runat="server">
32 </asp:DropDownList><br />
33 <asp:Button ID="btnShowTables" runat="server" Text="Show Tables From DataBase"
34 onclick="btnShowTables_Click" BackColor="Silver" BorderStyle="Groove"
35 Font-Bold="True" />
36 <br />
37 <asp:DataGrid ID="dg" runat="server" AutoGenerateColumns="False" CellPadding="3"
38 CellSpacing="0" PageSize="1" OnItemCommand="dg_ItemCommand"
39 style="margin-right: 17px">
40 <HeaderStyle CssClass="dg-HeaderStyle"></HeaderStyle>
41 <ItemStyle CssClass="dg-ItemStyle" />
42 <Columns>
43 <asp:TemplateColumn HeaderText="数据库">
44 <HeaderStyle Width="50px" />
45 <ItemStyle Width="50px" />
46 <ItemTemplate>
47 <%# DataBinder.Eval(Container.DataItem, "数据库")%>
48 </ItemTemplate>
49 </asp:TemplateColumn>
50 <asp:TemplateColumn HeaderText="表架构">
51 <HeaderStyle Width="50px" />
52 <ItemStyle Width="50px" />
53 <ItemTemplate>
54 <%# DataBinder.Eval(Container.DataItem, "表架构").ToString()%>
55 </ItemTemplate>
56 </asp:TemplateColumn>
57 <asp:TemplateColumn HeaderText="表名">
58 <HeaderStyle Width="50px" />
59 <ItemStyle Width="50px" />
60 <ItemTemplate>
61 <%# GetTableName(Container.DataItem)%>
62 </ItemTemplate>
63 </asp:TemplateColumn>
64 <asp:TemplateColumn HeaderText="表类型">
65 <HeaderStyle Width="100px" />
66 <ItemStyle Width="100px" />
67 <ItemTemplate>
68 <%# DataBinder.Eval(Container.DataItem, "表类型").ToString()%>
69 </ItemTemplate>
70 </asp:TemplateColumn>
71 <asp:TemplateColumn HeaderText="操作">
72 <HeaderStyle Width="65px" />
73 <ItemStyle Width="65px" />
74 <ItemTemplate>
75 <asp:LinkButton ID="edit" CommandName="edit" ForeColor="#FF0000" runat="server">编辑</asp:LinkButton>
76 <asp:LinkButton ID="del" CommandName="del" ForeColor="#FF0000" runat="server">删除</asp:LinkButton>
77 </ItemTemplate>
78 </asp:TemplateColumn>
79 </Columns>
80 </asp:DataGrid>
81 </div>
82
83 <div style="45%; float:right;">
84 <p style="font:blue;">第二步:选择表名</p>
85 <asp:DropDownList ID="ddlTablesName" runat="server"
86 onselectedindexchanged="ddlTablesName_SelectedIndexChanged"
87 Visible="False" AutoPostBack="True" >
88 </asp:DropDownList>
89 <br />
90 <asp:DataGrid ID="gwShowColumn" runat="server" AutoGenerateColumns="False" CellPadding="3"
91 CellSpacing="0" PageSize="1" style="margin-right: 17px">
92 <HeaderStyle CssClass="dg-HeaderStyle"></HeaderStyle>
93 <ItemStyle CssClass="dg-ItemStyle" />
94 <Columns>
95 <asp:TemplateColumn HeaderText="数据库">
96 <HeaderStyle Width="70px" />
97 <ItemStyle Width="70px" />
98 <ItemTemplate>
99 <%# DataBinder.Eval(Container.DataItem, "数据库")%>
100 </ItemTemplate>
101 </asp:TemplateColumn>
102 <asp:TemplateColumn HeaderText="表架构">
103 <HeaderStyle Width="50px" />
104 <ItemStyle Width="50px" />
105 <ItemTemplate>
106 <%# DataBinder.Eval(Container.DataItem, "表架构").ToString()%>
107 </ItemTemplate>
108 </asp:TemplateColumn>
109 <asp:TemplateColumn HeaderText="表名">
110 <HeaderStyle Width="70px" />
111 <ItemStyle Width="70px" />
112 <ItemTemplate>
113 <%# GetTableName(Container.DataItem)%>
114 </ItemTemplate>
115 </asp:TemplateColumn>
116 <asp:TemplateColumn HeaderText="列名">
117 <HeaderStyle Width="70px" />
118 <ItemStyle Width="70px" />
119 <ItemTemplate>
120 <%# DataBinder.Eval(Container.DataItem, "列名").ToString()%>
121 </ItemTemplate>
122 </asp:TemplateColumn>
123 <asp:TemplateColumn HeaderText="列数据类型">
124 <HeaderStyle Width="70px" />
125 <ItemStyle Width="70px" />
126 <ItemTemplate>
127 <%# DataBinder.Eval(Container.DataItem, "列数据类型").ToString()%>
128 </ItemTemplate>
129 </asp:TemplateColumn>
130 </Columns>
131 </asp:DataGrid>
132 <br />
133 <p style="font:blue;">第三步:指定命名空间,点击按钮获取数据库表对象模型</p>
134 Input NameSpace Name:<asp:TextBox ID="tbNameSpace" runat="server" CssClass="textInput_blur" onfocus="this.className='textInput_focus'"
135 onblur="this.className='textInput_blur'"></asp:TextBox>
136 <br />
137 <asp:Button ID="btnCreate" runat="server" Text="Create Object Model"
138 onclick="btnCreate_Click" BorderColor="Silver" BorderStyle="Groove"
139 Font-Bold="True" />
140 <br />
141 <asp:Button ID="btnCreateFile" runat="server" Text="CreateFile" OnClick="btnCreateFile_Click" /><br />
142
143 <asp:Label ID="lblShow" runat="server" BackColor="Silver" BorderColor="Blue"
144 BorderStyle="Solid"></asp:Label>
145 </div>
146 <br />
147 <div>
148
149
150
151 </div>
152
153
154 </div>
155 </form>
156 </body>
157 </html>
158
后台操作代码:由于获取操作系统消耗内存太大,有点卡,就没那么写了
操作代码
1 using System;
2 using System.Collections;
3 using System.Configuration;
4 using System.Data;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.HtmlControls;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.WebControls.WebParts;
11
12 using ForeTech.Data;
13 using System.Data.SqlClient;
14
15 using System.IO;
16
17 public partial class DataBaseTablesColumns_GetTablesFromDB : System.Web.UI.Page
18 {
19 protected void Page_Load(object sender, EventArgs e)
20 {
21 btnCreate.Enabled = false;
22 BindData();
23 }
24
25 private void BindData()
26 {
27 lblShow.Text = "<br />操作指南<br />" + "第一步:选择数据库 <br />" + "第二步:选择表名 <br />" + "第三步:指定命名空间,点击按钮获取数据库表对象模型<br /><br />";
28 if (ddlDataBase.Items.Count <= 0)
29 {
30 ddlDataBase.Items.Add(new ListItem("ForeTechTest"));//get the database from sql server
31 //throw new NotImplementedException();
32 }
33 }
34
35 protected void btnShowTables_Click(object sender, EventArgs e)
36 {
37 string dataBaseName = "'" + ddlDataBase.SelectedItem.Text.Trim() + "'";
38 ddlTablesName.Visible = true;
39 SqlParameter sp = new SqlParameter("@dataBaseName", SqlDbType.NVarChar, 200);
40 sp.Value = dataBaseName;
41 DataSet ds = DBTool.ExecuteDataset(CommandType.StoredProcedure, "GetTablesFromDatabase", sp);
42 ddlTablesName.DataTextField = "表名";
43 ddlTablesName.DataValueField = "表名";
44 ddlTablesName.DataSource = ds;
45 ddlTablesName.DataBind();
46 dg.DataSource = ds;
47 dg.DataBind();
48 /* try
49 {
50 int colWidth = 200;
51 if (colWidth > 0)
52 {
53 for (int i = 0; i < gwShowTables.Columns.Count; i++)
54 {
55 gwShowTables.Columns[i].HeaderStyle.Width = 200;// ItemStyle.Width = colWidth;
56 }
57 }
58 }
59 catch
60 {
61 // Report error.
62 }
63 */
64 }
65
66 protected void ddlTablesName_SelectedIndexChanged(object sender, EventArgs e)
67 {
68 string strTable = "'" + ddlTablesName.SelectedItem.Value.Trim() + "'";
69 SqlParameter[] sp = new SqlParameter[2];
70 SqlParameter database = new SqlParameter("@dataBaseName", SqlDbType.NVarChar, 200);
71 database.Value = ddlDataBase.SelectedItem.Text.Trim();
72 SqlParameter selectedTable = new SqlParameter("@tableName", SqlDbType.NVarChar, 200);
73 selectedTable.Value = strTable;
74 sp[0] = database;
75 sp[1] = selectedTable;
76 DataSet ds = DBTool.ExecuteDataset(CommandType.StoredProcedure, "GetColumnsFromTable", sp);
77 gwShowColumn.DataSource = ds;
78 gwShowColumn.DataBind();
79 btnCreate.Enabled = true;
80 /* try
81 {
82 int colWidth = 200;
83 if (colWidth > 0)
84 {
85 for (int i = 0; i < gwShowTables.Columns.Count; i++)
86 {
87 gwShowTables.Columns[i].HeaderStyle.Width = 200;// ItemStyle.Width = colWidth;
88 }
89 }
90 }
91 catch
92 {
93 // Report error.
94 }*/
95
96 }
97 protected string GetTableName(object dataItem)
98 {
99 string str = "";
100 if (dataItem.GetType().Name.Equals("DataRowView"))
101 {
102 DataRowView row = (DataRowView)dataItem;
103 str = row["表名"].ToString();
104 }
105 return str;
106 }
107
108 protected void dg_ItemCommand(object source, DataGridCommandEventArgs e)
109 {
110 /*
111 if (e.CommandName == "edit")
112 {
113
114 }
115 else if (e.CommandName == "del")
116 {
117
118 }*/
119 }
120
121 protected void btnCreate_Click(object sender, EventArgs e)
122 {
123
124 if (tbNameSpace.Text == "")
125 {
126 Response.Write("<script>alert('Please input namespace!!!');</script>");
127 }
128 btnCreate.Enabled = true;
129 string className = ddlTablesName.SelectedItem.Text.ToString();
130 string strTable = "'" + className + "'";
131 SqlParameter[] sp = new SqlParameter[2];
132 SqlParameter database = new SqlParameter("@dataBaseName", SqlDbType.NVarChar, 200);
133 database.Value = ddlDataBase.SelectedItem.Text.Trim();
134 SqlParameter selectedTable = new SqlParameter("@tableName", SqlDbType.NVarChar, 200);
135 selectedTable.Value = strTable;
136 sp[0] = database;
137 sp[1] = selectedTable;
138 DataSet ds = DBTool.ExecuteDataset(CommandType.StoredProcedure, "GetColumnsFromTable", sp);
139 string fields = "";
140 string propertities = "";
141 if (ds != null && ds.Tables[0].Rows.Count > 0)
142 {
143 for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
144 {
145 propertities += " %null%%null%%null%%null%%null%%null%%null%%null%public " + ChangeToCSharpType(ds.Tables[0].Rows[i]["列数据类型"].ToString().Trim()) + " " + ds.Tables[0].Rows[i]["列名"].ToString().Trim() + "%changeRow%" +
146 "%null%%null%%null%%null%%null%%null%%null%%null%{%changeRow%" +
147 " %null%%null%%null%%null%%null%%null%%null%%null%%null%%null%%null%set" + " { " + ds.Tables[0].Rows[i]["列名"].ToString().Trim() + " = value; }%changeRow%" +
148 " %null%%null%%null%%null%%null%%null%%null%%null%%null%%null%%null%get" + " { return _" + ds.Tables[0].Rows[i]["列名"].ToString().Trim() + "; }%changeRow%" +
149 "%null%%null%%null%%null%%null%%null%%null%%null%}%changeRow%";
150 fields += " %null%%null%%null%%null%%null%%null%%null%%null%private " + ChangeToCSharpType(ds.Tables[0].Rows[i]["列数据类型"].ToString().Trim()) + " _" + ds.Tables[0].Rows[i]["列名"].ToString().Trim() + ";%changeRow%";
151 }
152 }
153 string sql = @"
154 using System;%changeRow%
155 namespace %namespace%%changeRow%
156 {%changeRow%
157 %null%%null%%null%%null% /// <summary>%changeRow%
158 %null%%null%%null%%null% /// 实体类%class% (属性说明自动提取数据库字段的描述信息)%changeRow%
159 %null%%null%%null%%null% /// </summary>%changeRow%
160 %null%%null%%null%%null% public partial class %class% %changeRow%
161 %null%%null%%null%%null% {%changeRow%
162 %null%%null%%null%%null%%null%%null%%null%%null% public %class% ()%changeRow%
163 %null%%null%%null%%null%%null%%null%%null%%null% {%changeRow%
164 %null%%null%%null%%null%%null%%null%%null%%null% }%changeRow%
165
166 %null%%null%%null%%null%%null%%null%%null%%null% #region 字段%changeRow%
167 %fields%%changeRow%
168 %null%%null%%null%%null%%null%%null%%null%%null% #endregion 字段%changeRow%
169
170 %null%%null%%null%%null%%null%%null%%null%%null% #region 属性%changeRow%
171 %propertities%%changeRow%
172 %null%%null%%null%%null%%null%%null%%null%%null% #endregion 属性%changeRow%
173 %null%%null%%null%%null% }%changeRow%
174 }
175 ";
176 string replaceSql = sql.Replace("%namespace%", tbNameSpace.Text.Trim()).Replace("%class%", className).Replace("%fields%", fields).Replace("%propertities%", propertities).Replace("%null%", " ").Replace("%changeRow%", "<br />");
177
178 // tbObjectData.Text =Server.HtmlDecode(replaceSql);
179 lblShow.Text = replaceSql;
180 WriteContent(replaceSql,className);
181 }
182
183 /// <summary>
184 /// 数据库中与C#中的数据类型对照
185 /// </summary>
186 /// <param name="type"></param>
187 /// <returns></returns>
188 private string ChangeToCSharpType(string type)
189 {
190 string reval = string.Empty;
191 switch (type.ToLower())
192 {
193 case "int":
194 reval = "Int32";
195 break;
196 case "text":
197 reval = "String";
198 break;
199 case "bigint":
200 reval = "Int64";
201 break;
202 case "binary":
203 reval = "System.Byte[]";
204 break;
205 case "bit":
206 reval = "Boolean";
207 break;
208 case "char":
209 reval = "String";
210 break;
211 case "datetime":
212 reval = "System.DateTime";
213 break;
214 case "decimal":
215 reval = "System.Decimal";
216 break;
217 case "float":
218 reval = "System.Double";
219 break;
220 case "image":
221 reval = "System.Byte[]";
222 break;
223 case "money":
224 reval = "System.Decimal";
225 break;
226 case "nchar":
227 reval = "String";
228 break;
229 case "ntext":
230 reval = "String";
231 break;
232 case "numeric":
233 reval = "System.Decimal";
234 break;
235 case "nvarchar":
236 reval = "String";
237 break;
238 case "real":
239 reval = "System.Single";
240 break;
241 case "smalldatetime":
242 reval = "System.DateTime";
243 break;
244 case "smallint":
245 reval = "Int16";
246 break;
247 case "smallmoney":
248 reval = "System.Decimal";
249 break;
250 case "timestamp":
251 reval = "System.DateTime";
252 break;
253 case "tinyint":
254 reval = "System.Byte";
255 break;
256 case "uniqueidentifier":
257 reval = "System.Guid";
258 break;
259 case "varbinary":
260 reval = "System.Byte[]";
261 break;
262 case "varchar":
263 reval = "String";
264 break;
265 case "Variant":
266 reval = "Object";
267 break;
268 default:
269 reval = "String";
270 break;
271 }
272 return reval;
273 }
274
275 protected void btnCreateFile_Click(object sender, EventArgs e)
276 {
277 // WriteContent();
278
279 }
280
281 private void WriteContent(string inputString,string className)
282 {
283 string path = @"d:\MyTestDir\"+className.Trim()+".cs";
284 string inputStr = inputString;
285 try
286 {
2 using System.Collections;
3 using System.Configuration;
4 using System.Data;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.HtmlControls;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.WebControls.WebParts;
11
12 using ForeTech.Data;
13 using System.Data.SqlClient;
14
15 using System.IO;
16
17 public partial class DataBaseTablesColumns_GetTablesFromDB : System.Web.UI.Page
18 {
19 protected void Page_Load(object sender, EventArgs e)
20 {
21 btnCreate.Enabled = false;
22 BindData();
23 }
24
25 private void BindData()
26 {
27 lblShow.Text = "<br />操作指南<br />" + "第一步:选择数据库 <br />" + "第二步:选择表名 <br />" + "第三步:指定命名空间,点击按钮获取数据库表对象模型<br /><br />";
28 if (ddlDataBase.Items.Count <= 0)
29 {
30 ddlDataBase.Items.Add(new ListItem("ForeTechTest"));//get the database from sql server
31 //throw new NotImplementedException();
32 }
33 }
34
35 protected void btnShowTables_Click(object sender, EventArgs e)
36 {
37 string dataBaseName = "'" + ddlDataBase.SelectedItem.Text.Trim() + "'";
38 ddlTablesName.Visible = true;
39 SqlParameter sp = new SqlParameter("@dataBaseName", SqlDbType.NVarChar, 200);
40 sp.Value = dataBaseName;
41 DataSet ds = DBTool.ExecuteDataset(CommandType.StoredProcedure, "GetTablesFromDatabase", sp);
42 ddlTablesName.DataTextField = "表名";
43 ddlTablesName.DataValueField = "表名";
44 ddlTablesName.DataSource = ds;
45 ddlTablesName.DataBind();
46 dg.DataSource = ds;
47 dg.DataBind();
48 /* try
49 {
50 int colWidth = 200;
51 if (colWidth > 0)
52 {
53 for (int i = 0; i < gwShowTables.Columns.Count; i++)
54 {
55 gwShowTables.Columns[i].HeaderStyle.Width = 200;// ItemStyle.Width = colWidth;
56 }
57 }
58 }
59 catch
60 {
61 // Report error.
62 }
63 */
64 }
65
66 protected void ddlTablesName_SelectedIndexChanged(object sender, EventArgs e)
67 {
68 string strTable = "'" + ddlTablesName.SelectedItem.Value.Trim() + "'";
69 SqlParameter[] sp = new SqlParameter[2];
70 SqlParameter database = new SqlParameter("@dataBaseName", SqlDbType.NVarChar, 200);
71 database.Value = ddlDataBase.SelectedItem.Text.Trim();
72 SqlParameter selectedTable = new SqlParameter("@tableName", SqlDbType.NVarChar, 200);
73 selectedTable.Value = strTable;
74 sp[0] = database;
75 sp[1] = selectedTable;
76 DataSet ds = DBTool.ExecuteDataset(CommandType.StoredProcedure, "GetColumnsFromTable", sp);
77 gwShowColumn.DataSource = ds;
78 gwShowColumn.DataBind();
79 btnCreate.Enabled = true;
80 /* try
81 {
82 int colWidth = 200;
83 if (colWidth > 0)
84 {
85 for (int i = 0; i < gwShowTables.Columns.Count; i++)
86 {
87 gwShowTables.Columns[i].HeaderStyle.Width = 200;// ItemStyle.Width = colWidth;
88 }
89 }
90 }
91 catch
92 {
93 // Report error.
94 }*/
95
96 }
97 protected string GetTableName(object dataItem)
98 {
99 string str = "";
100 if (dataItem.GetType().Name.Equals("DataRowView"))
101 {
102 DataRowView row = (DataRowView)dataItem;
103 str = row["表名"].ToString();
104 }
105 return str;
106 }
107
108 protected void dg_ItemCommand(object source, DataGridCommandEventArgs e)
109 {
110 /*
111 if (e.CommandName == "edit")
112 {
113
114 }
115 else if (e.CommandName == "del")
116 {
117
118 }*/
119 }
120
121 protected void btnCreate_Click(object sender, EventArgs e)
122 {
123
124 if (tbNameSpace.Text == "")
125 {
126 Response.Write("<script>alert('Please input namespace!!!');</script>");
127 }
128 btnCreate.Enabled = true;
129 string className = ddlTablesName.SelectedItem.Text.ToString();
130 string strTable = "'" + className + "'";
131 SqlParameter[] sp = new SqlParameter[2];
132 SqlParameter database = new SqlParameter("@dataBaseName", SqlDbType.NVarChar, 200);
133 database.Value = ddlDataBase.SelectedItem.Text.Trim();
134 SqlParameter selectedTable = new SqlParameter("@tableName", SqlDbType.NVarChar, 200);
135 selectedTable.Value = strTable;
136 sp[0] = database;
137 sp[1] = selectedTable;
138 DataSet ds = DBTool.ExecuteDataset(CommandType.StoredProcedure, "GetColumnsFromTable", sp);
139 string fields = "";
140 string propertities = "";
141 if (ds != null && ds.Tables[0].Rows.Count > 0)
142 {
143 for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
144 {
145 propertities += " %null%%null%%null%%null%%null%%null%%null%%null%public " + ChangeToCSharpType(ds.Tables[0].Rows[i]["列数据类型"].ToString().Trim()) + " " + ds.Tables[0].Rows[i]["列名"].ToString().Trim() + "%changeRow%" +
146 "%null%%null%%null%%null%%null%%null%%null%%null%{%changeRow%" +
147 " %null%%null%%null%%null%%null%%null%%null%%null%%null%%null%%null%set" + " { " + ds.Tables[0].Rows[i]["列名"].ToString().Trim() + " = value; }%changeRow%" +
148 " %null%%null%%null%%null%%null%%null%%null%%null%%null%%null%%null%get" + " { return _" + ds.Tables[0].Rows[i]["列名"].ToString().Trim() + "; }%changeRow%" +
149 "%null%%null%%null%%null%%null%%null%%null%%null%}%changeRow%";
150 fields += " %null%%null%%null%%null%%null%%null%%null%%null%private " + ChangeToCSharpType(ds.Tables[0].Rows[i]["列数据类型"].ToString().Trim()) + " _" + ds.Tables[0].Rows[i]["列名"].ToString().Trim() + ";%changeRow%";
151 }
152 }
153 string sql = @"
154 using System;%changeRow%
155 namespace %namespace%%changeRow%
156 {%changeRow%
157 %null%%null%%null%%null% /// <summary>%changeRow%
158 %null%%null%%null%%null% /// 实体类%class% (属性说明自动提取数据库字段的描述信息)%changeRow%
159 %null%%null%%null%%null% /// </summary>%changeRow%
160 %null%%null%%null%%null% public partial class %class% %changeRow%
161 %null%%null%%null%%null% {%changeRow%
162 %null%%null%%null%%null%%null%%null%%null%%null% public %class% ()%changeRow%
163 %null%%null%%null%%null%%null%%null%%null%%null% {%changeRow%
164 %null%%null%%null%%null%%null%%null%%null%%null% }%changeRow%
165
166 %null%%null%%null%%null%%null%%null%%null%%null% #region 字段%changeRow%
167 %fields%%changeRow%
168 %null%%null%%null%%null%%null%%null%%null%%null% #endregion 字段%changeRow%
169
170 %null%%null%%null%%null%%null%%null%%null%%null% #region 属性%changeRow%
171 %propertities%%changeRow%
172 %null%%null%%null%%null%%null%%null%%null%%null% #endregion 属性%changeRow%
173 %null%%null%%null%%null% }%changeRow%
174 }
175 ";
176 string replaceSql = sql.Replace("%namespace%", tbNameSpace.Text.Trim()).Replace("%class%", className).Replace("%fields%", fields).Replace("%propertities%", propertities).Replace("%null%", " ").Replace("%changeRow%", "<br />");
177
178 // tbObjectData.Text =Server.HtmlDecode(replaceSql);
179 lblShow.Text = replaceSql;
180 WriteContent(replaceSql,className);
181 }
182
183 /// <summary>
184 /// 数据库中与C#中的数据类型对照
185 /// </summary>
186 /// <param name="type"></param>
187 /// <returns></returns>
188 private string ChangeToCSharpType(string type)
189 {
190 string reval = string.Empty;
191 switch (type.ToLower())
192 {
193 case "int":
194 reval = "Int32";
195 break;
196 case "text":
197 reval = "String";
198 break;
199 case "bigint":
200 reval = "Int64";
201 break;
202 case "binary":
203 reval = "System.Byte[]";
204 break;
205 case "bit":
206 reval = "Boolean";
207 break;
208 case "char":
209 reval = "String";
210 break;
211 case "datetime":
212 reval = "System.DateTime";
213 break;
214 case "decimal":
215 reval = "System.Decimal";
216 break;
217 case "float":
218 reval = "System.Double";
219 break;
220 case "image":
221 reval = "System.Byte[]";
222 break;
223 case "money":
224 reval = "System.Decimal";
225 break;
226 case "nchar":
227 reval = "String";
228 break;
229 case "ntext":
230 reval = "String";
231 break;
232 case "numeric":
233 reval = "System.Decimal";
234 break;
235 case "nvarchar":
236 reval = "String";
237 break;
238 case "real":
239 reval = "System.Single";
240 break;
241 case "smalldatetime":
242 reval = "System.DateTime";
243 break;
244 case "smallint":
245 reval = "Int16";
246 break;
247 case "smallmoney":
248 reval = "System.Decimal";
249 break;
250 case "timestamp":
251 reval = "System.DateTime";
252 break;
253 case "tinyint":
254 reval = "System.Byte";
255 break;
256 case "uniqueidentifier":
257 reval = "System.Guid";
258 break;
259 case "varbinary":
260 reval = "System.Byte[]";
261 break;
262 case "varchar":
263 reval = "String";
264 break;
265 case "Variant":
266 reval = "Object";
267 break;
268 default:
269 reval = "String";
270 break;
271 }
272 return reval;
273 }
274
275 protected void btnCreateFile_Click(object sender, EventArgs e)
276 {
277 // WriteContent();
278
279 }
280
281 private void WriteContent(string inputString,string className)
282 {
283 string path = @"d:\MyTestDir\"+className.Trim()+".cs";
284 string inputStr = inputString;
285 try
286 {
if (!Directory.Exists(@"d:\MyTestDir\"))
{
Directory.CreateDirectory(@"d:\MyTestDir\");
}
287 if (File.Exists(path))
288 {
289 File.Delete(path);
290
291 }
292 if (!File.Exists(path))
293 {
294 // Create a file to write to.
295
296 using (StreamWriter sw = File.CreateText(path))
297 {
298 string[] strRows = inputStr.Replace(" ", " ").Replace("<br />", Convert.ToChar(255).ToString()).Split(Convert.ToChar(255));
299 foreach (string strRow in strRows)
300 {
301 sw.WriteLine(strRow);
302 }
303 }
304 }
305
306 // Open the file to read from.
307 using (StreamReader sr = File.OpenText(path))
308 {
309 string s = "";
310 while ((s = sr.ReadLine()) != null)
311 {
312 Response.Write(s + "<br />");
313 }
314 }
315 }
316 catch (IOException ex)
317 {
318 Response.Write(ex.ToString());
319 }
320 }
321 }
322
{
Directory.CreateDirectory(@"d:\MyTestDir\");
}
287 if (File.Exists(path))
288 {
289 File.Delete(path);
290
291 }
292 if (!File.Exists(path))
293 {
294 // Create a file to write to.
295
296 using (StreamWriter sw = File.CreateText(path))
297 {
298 string[] strRows = inputStr.Replace(" ", " ").Replace("<br />", Convert.ToChar(255).ToString()).Split(Convert.ToChar(255));
299 foreach (string strRow in strRows)
300 {
301 sw.WriteLine(strRow);
302 }
303 }
304 }
305
306 // Open the file to read from.
307 using (StreamReader sr = File.OpenText(path))
308 {
309 string s = "";
310 while ((s = sr.ReadLine()) != null)
311 {
312 Response.Write(s + "<br />");
313 }
314 }
315 }
316 catch (IOException ex)
317 {
318 Response.Write(ex.ToString());
319 }
320 }
321 }
322
操作图片如下: