using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
namespace dac.role
{
/// <summary>
/// WebForm2 的摘要说明。
/// 作用:读一个XML,把它转成ORACLE存储过程
/// 使用方法:
/// 第一步将表输出为XML,在PL/SQL DEVELOPERR软件的表里,点你想要的表,右键选择EDIT,在COLUMNS的空白处点右键将表输出为XML文件,第二步,在本页面里选中XML,点生成就可得到想要得到的存储过程
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label MSG;
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Button show;
protected System.Web.UI.WebControls.Button build;
protected System.Web.UI.WebControls.TextBox fd;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
//dim
public string FileName;
public string[] split;
public DataSet ds=new DataSet();
public string temp1;
public string temp2;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(! this.Page.IsPostBack )
{
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.build.Click += new System.EventHandler(this.Button1_Click);
this.show.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
//给DATAGRID绑定数据
public void Button2_Click(object sender, System.EventArgs e)
{ //ds.ReadXml(File1.PostedFile.FileName.ToString().Replace("//",""));
ds.ReadXml(File1.PostedFile.FileName.ToString());
//ds.ReadXml(this.Session["MyPath"].ToString());
this.DataGrid1.DataSource=ds.Tables[0].DefaultView;
this.DataGrid1.DataBind();
}
//找是不是关键字
public bool pk_find(string fd_string)
{
split = fd.Text.ToString().Split(',');
for (int x = 0; x < split.Length; x++)
{
if(split[x].Trim()==fd_string.Trim())
{
return true;
}
}
return false;
}
//生成数据
public void Button1_Click(object sender, System.EventArgs e)
{
//ds.ReadXml(File1.PostedFile.FileName.ToString().Replace("//",""));
ds.ReadXml(File1.PostedFile.FileName.ToString());
//得到文件名并去掉.XML,把它当作存储过程名的一部分,例:文件名为name.xml生成NAME_Add
FileName=File1.PostedFile.FileName.ToString().Substring(File1.PostedFile.FileName.ToString().LastIndexOf("//")+1);
FileName=FileName.Substring(0,FileName.Length-4).ToUpper();//去掉.xml
split = fd.Text.ToString().Split(',');
for (int x = 0; x < split.Length; x++)
{
temp1+="p_"+split[x]+"="+split[x].Trim()+",";
temp2+=split[x]+"=p_"+split[x].Trim()+" and ";
}
temp1=temp1.Substring(0,temp1.Length-1);
temp2=temp2.Substring(0,temp2.Length-4);
StringBuilder text = new StringBuilder();
text.Append("CREATE OR REPLACE PROCEDURE "+FileName+"_ADD<br>");
text.Append("/***********************************************************/<br>");
text.Append("/**表名:"+FileName+" **/<br>");
text.Append("/***********************************************************/<br>(<br>");
StringBuilder text1 = new StringBuilder();//变量列表 in insert
StringBuilder text2 = new StringBuilder();//字段列表 insert
StringBuilder text3 = new StringBuilder();//变量列表 insert
StringBuilder text4 = new StringBuilder();//字段列表 update
StringBuilder text5 = new StringBuilder();//字段列表 delete
StringBuilder text6 = new StringBuilder();//输入参数 ORACLE
StringBuilder text7 = new StringBuilder();//输入参数 ORACLE 2
DataTable tb=ds.Tables[0];
for(int k=0;k<tb.Rows.Count;k++)
{
text1.Append("p_"+tb.Rows[k][0].ToString());
text1.Append(" in "+tb.Rows[k][1].ToString().Substring(0,tb.Rows[k][1].ToString().IndexOf("(")));//输入值列表
text2.Append(tb.Rows[k][0]);
text3.Append("p_"+tb.Rows[k][0]);
text6.Append("mycommand.Parameters.Add(");//输入参数 ORACLE
text6.Append(""");
text6.Append("p_"+tb.Rows[k][0]);
text6.Append(""");
if(tb.Rows[k][1].ToString().IndexOf("VARCHAR2")>0)
text6.Append(",System.Data.OracleClient.OracleType."+tb.Rows[k][1].ToString().Replace('(',',')); else
text6.Append(",System.Data.OracleClient.OracleType."+tb.Rows[k][1].ToString().Replace("VARCHAR2(","VarChar,"));
text6.Append(";<BR>");
text7.Append("mycommand.Parameters[");//输入参数 ORACLE2
text7.Append(""");
text7.Append("p_"+tb.Rows[k][0]);
text7.Append("""+"].Value=");
text7.Append(tb.Rows[k][0]);
text7.Append(";<BR>");
//if(!(tb.Rows[k][0].ToString()==fd.Text.ToString()))//update//del 检查是不是关键字
if(! pk_find(tb.Rows[k][0].ToString()))//update//del 检查是不是关键字
{
text4.Append(tb.Rows[k][0].ToString()+"=p_"+tb.Rows[k][0].ToString());
}
else
{
text5.Append("p_"+tb.Rows[k][0].ToString());
text5.Append(" in "+tb.Rows[k][1].ToString().Substring(0,tb.Rows[k][1].ToString().IndexOf("("))+",<br>");//输入值列表
}
if(k<tb.Rows.Count-1)
{
text1.Append(",");
text2.Append(",");
text3.Append(",");
//if(!(tb.Rows[k][0].ToString()==fd.Text.ToString()))//update
if(! pk_find(tb.Rows[k][0].ToString()))//update//del 检查是不是关键字
{
text4.Append(",");
}
}
text1.Append("<br>");
//text5.Append(",<br>");
}
//sub: insert
text.Append(text1.ToString());
text.Append(")<br>is<br>begin<br>");
text.Append("insert into ");
text.Append(FileName);
text.Append(" ("+text2.ToString()+")<br> values ("+text3.ToString()+");");
text.Append("<br>end;<br><br><br>");
//sub:update
text.Append("CREATE OR REPLACE PROCEDURE "+FileName+"_UP<br>");
text.Append("/***********************************************************/<br>");
text.Append("/**表名:"+FileName+" **/<br>");
text.Append("/***********************************************************/<br>(<br>");
text.Append(text1.ToString());
text.Append(")<br>is<br>begin<br>");
text.Append("UPDATE "+FileName+" SET ");
text.Append(" <br> ");
text.Append(text4.ToString());
//text.Append(text4.ToString().Substring(0,text4.ToString().Length-1));
//text.Append("<br> WHERE "+fd.Text.ToString()+"=p_"+fd.Text.ToString()+";<br>end;<br><br><br>");
text.Append("<br> WHERE "+temp2+";<br>end;<br><br><br>");
//sub: del
text.Append("CREATE OR REPLACE PROCEDURE "+FileName+"_DEL<br>");
text.Append("/***********************************************************/<br>");
text.Append("/**表名:"+FileName+" **/<br>");
text.Append("/***********************************************************/<br>(<br>");
text.Append(text5.ToString().Trim().Substring(0,text5.ToString().Trim().Length-5));
text.Append("<br>)<br>is<br>begin<br>DELETE FROM ");
text.Append(FileName);
text.Append(" <br> ");
//text.Append(" WHERE "+fd.Text.ToString()+"=p_"+fd.Text.ToString()+";<br>end;<br><br><br>");
text.Append(" WHERE "+temp2+";<br>end;<br><br><br>");
text.Append("//注意:以下内容的字段有可能不准确<br>");
text.Append(text6.ToString());//输入参数 ORACLE
text.Append("<BR>");
text.Append(text7.ToString());//输入参数 ORACLE2
this.MSG.Text=text.ToString();
}
}
}
<%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="dac.role.WebForm2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 536px; POSITION: absolute; TOP: 40px"
runat="server"></asp:DataGrid>
<asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 40px; POSITION: absolute; TOP: 40px" runat="server">存储过程生成</asp:Label>
<asp:Label id="MSG" style="Z-INDEX: 103; LEFT: 40px; POSITION: absolute; TOP: 176px" runat="server"></asp:Label><INPUT id="File1" style="Z-INDEX: 104; LEFT: 144px; POSITION: absolute; TOP: 88px" type="file"
name="File1" runat="server">
<asp:TextBox id="fd" style="Z-INDEX: 105; LEFT: 144px; POSITION: absolute; TOP: 128px" runat="server"></asp:TextBox>
<asp:Label id="Label2" style="Z-INDEX: 106; LEFT: 40px; POSITION: absolute; TOP: 88px" runat="server">打开XML文件</asp:Label>
<asp:Label id="Label3" style="Z-INDEX: 107; LEFT: 40px; POSITION: absolute; TOP: 128px" runat="server">关键字段</asp:Label>
<asp:Button id="build" style="Z-INDEX: 108; LEFT: 312px; POSITION: absolute; TOP: 128px" runat="server"
Width="56px" Text="生成"></asp:Button>
<asp:Button id="show" style="Z-INDEX: 109; LEFT: 384px; POSITION: absolute; TOP: 88px" runat="server"
Text="显示" CausesValidation="False"></asp:Button>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" style="Z-INDEX: 110; LEFT: 304px; POSITION: absolute; TOP: 136px"
runat="server" ErrorMessage="*" ControlToValidate="fd"></asp:RequiredFieldValidator></FONT></form>
</body>
</HTML>