生成C#执行指定存储过程的参数

CodeSimth
代码如下

<%-- 
Name:
Author: 
Description: 
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="False" Description="Template description here." %>
<%@ Property Name="PROC_NAME" Type="System.String" Category="Context" Description="????????" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
            SqlParameter[] parameters 
= {
        
<% 
            System.Text.StringBuilder builder 
= new System.Text.StringBuilder();
            DataSet ds 
= new DataSet();
            ds 
= GetDbParameter();
            
for(int i =0; i < ds.Tables[0].Rows.Count; i++)
            {
                
%>
                    
new SqlParameter("<%=ds.Tables[0].Rows[i]["PARAMETER_NAME"].ToString()%>", SqlDbType.<%=ds.Tables[0].Rows[i]["DATA_TYPE"].ToString()%>)<% if ((i+1)==ds.Tables[0].Rows.Count){%>
                    };
<%
                            }
                        
else
                        {
                    
%>,
                    
<%
                    }
                    
%>
                
<%
            }
        
%>
        
        
<%  for(int j=0; j < ds.Tables[0].Rows.Count;  j++)
            {
        
%>    
            
<% if( ds.Tables[0].Rows[j]["PARAMETER_MODE"].ToString().Trim()=="IN"){ %>
            parameters[
<% %><%=j%><% %>].Value=model.<%=SubStr(ds.Tables[0].Rows[j]["PARAMETER_NAME"].ToString().Trim()) %>;
            
<% } else{%>
            parameters[
<% %><%=j%><% %>].Direction=ParameterDirection.Output;
<%%>
            
<%
            }
        
%>


<script runat="template">

#region ??SQL??????
public DataSet GetDbParameter()
{
    SqlConnection con 
=new SqlConnection("Database=ksm000;Server=.;user id =sa;password =sa");
    SqlCommand cmd 
=new SqlCommand("select * from information_schema.parameters where specific_name ='s_customer_ADD'",con);
    SqlDataAdapter sda 
=new SqlDataAdapter();
    sda.SelectCommand 
= cmd;
    DataSet ds 
=new DataSet();
    sda.Fill(ds);
    
return ds;
}
#endregion

#region ????
public string SubStr(string str)
{
    
return str.Substring(1,str.Length-1);    
}

#endregion

</script>

 

MyGeneration
代码如下:

<%#REFERENCE System.Data.dll%><%#NAMESPACE System.Data%><%#NAMESPACE System.Data.SqlClient%><%#REFERENCE System.Xml.dll%><%
public class GeneratedTemplate : DotNetScriptTemplate
{
    
public GeneratedTemplate(ZeusContext context) : base(context) {}

    
//---------------------------------------------------
    
// Render() is where you want to write your logic    
    
//---------------------------------------------------
    public override void Render()
    {
        output.write(
"\t\t");
        output.write(
"SqlParameter[] parameters = {");
        System.Text.StringBuilder builder 
= new System.Text.StringBuilder();
        DataSet ds 
= new DataSet();
        ds 
= GetDbParameter1();
        output.write(
"\n");
        
for(int i =0; i < ds.Tables[0].Rows.Count; i++)
        {
            output.write(
"\t\t\t\t");
            output.write(
"new SqlParameter(");
            output.write(
"\"" + ds.Tables[0].Rows[i]["PARAMETER_NAME"].ToString() + "\""+", SqlDbType." + SubstrToUpper(ds.Tables[0].Rows[i]["DATA_TYPE"].ToString()));
            output.write(
")");
            
if ((i+1)==ds.Tables[0].Rows.Count)
            {
                output.write(
"\n");
                output.write(
"\t\t};");
            }
            
else
            {
                output.write(
",");
                output.writeln(
"");
            }
        }
        output.writeln(
"\t\n");
        
for(int j=0; j < ds.Tables[0].Rows.Count;  j++)
        {
            
if(ds.Tables[0].Rows[j]["PARAMETER_MODE"].ToString().Trim()=="IN")
            { 
                output.write(
"\t\t");
                output.write(
"parameters[");
                output.write(j.ToString());
                output.write(
"].Value=model.");
                output.write(SubStr(ds.Tables[
0].Rows[j]["PARAMETER_NAME"].ToString().Trim()));
                output.write(
";");
                output.writeln(
"");
            } 
            
else
            {
                output.write(
"\t\t");
                output.write(
"parameters["+j.ToString());
                output.write(
"].Direction=ParameterDirection.Output;");
                output.writeln(
"");
            } 
        }
    }
    
public DataSet GetDbParameter1()
    {
        SqlConnection con 
=new SqlConnection("Database=ksm000;Server=.;user id =sa;password =sa");
        SqlCommand cmd 
=new SqlCommand("select * from information_schema.parameters where specific_name ='s_customer_ADD'",con);
        SqlDataAdapter sda 
=new SqlDataAdapter();
        sda.SelectCommand 
= cmd;
        DataSet ds 
=new DataSet();
        sda.Fill(ds);
        
return ds;
    }
    
private string SubstrToUpper(string str)
    {
        
switch (str.ToLower())
        {
            
case "int":
                str 
= "Int";
                
break;
            
case "nchar":
                str 
= "NChar";
                
break;
            
case "bit":
                str 
= "Bit";
                
break;
            
case "nvarchar":
                str 
= "NVarChar";
                
break;
            
case "datetime":
                str 
= "DataTime";
                
break;
            
case "varchar":
                str 
= "VarChar";
                
break;
            
case "smalldatetime":
                str 
= "SmallDateTime";
                
break;
            
case "decimal":
                str 
= "Decimal";
                
break;
            
case "float":
                str 
= "Float";
                
break;
            
case "char":
                str 
= "Char";
                
break;
            
case "text":
                str 
= "Text";
                
break;
            
case "money":
                str 
= "Money";
                
break;
            
default:
                str 
= "没有找到数据类型";
                
break;
        }
        
return str;
    }

    
public string SubStr(string str)
    {
        
return str.Substring(1,str.Length-1);    
    }

}
%>

C#生成代码

    protected void Page_Load(object sender, EventArgs e)
    {
        StringBuilder sb 
= new StringBuilder();
        sb.Append(
"\t\t");
        sb.Append(
"SqlParameter[] parameters = {");
        System.Text.StringBuilder builder 
= new System.Text.StringBuilder();
        DataSet ds 
= new DataSet();
        ds 
= GetDbParameter1();
        sb.Append(
"\n");
        
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            sb.Append(
"\t\t\t\t");
            sb.Append(
"new SqlParameter(");
            sb.Append(
"\"" + ds.Tables[0].Rows[i]["PARAMETER_NAME"].ToString() + "\"" + ", SqlDbType." + SubstrToUpper(ds.Tables[0].Rows[i]["DATA_TYPE"].ToString()));
            sb.Append(
")");
            
if ((i + 1== ds.Tables[0].Rows.Count)
            {
                sb.Append(
"\n");
                sb.Append(
"\t\t};");
            }
            
else
            {
                sb.Append(
",");
                sb.Append(
"\n");
            }
        }
        sb.Append(
"\t\n");
        
for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
        {
            
if (ds.Tables[0].Rows[j]["PARAMETER_MODE"].ToString().Trim() == "IN")
            {
                sb.Append(
"\t\t");
                sb.Append(
"parameters[");
                sb.Append(j.ToString());
                sb.Append(
"].Value=model.");
                sb.Append(SubStr(ds.Tables[
0].Rows[j]["PARAMETER_NAME"].ToString().Trim()));
                sb.Append(
";");
                sb.Append(
"\n");
            }
            
else
            {
                sb.Append(
"\t\t");
                sb.Append(
"parameters[" + j.ToString());
                sb.Append(
"].Direction=ParameterDirection.Output;");
                sb.Append(
"\n");
            }
        }
        
//Response.Write(sb.ToString);
        TextBox1.Text = sb.ToString();
    }
    
public DataSet GetDbParameter1()
    {
        SqlConnection con 
= new SqlConnection("Database=ksm000;Server=.;user id =sa;password =sa");
        SqlCommand cmd 
= new SqlCommand("select * from information_schema.parameters where specific_name ='s_customer_ADD'", con);
        SqlDataAdapter sda 
= new SqlDataAdapter();
        sda.SelectCommand 
= cmd;
        DataSet ds 
= new DataSet();
        sda.Fill(ds);
        
return ds;
    }
    
private string SubstrToUpper(string str)
    {
        
switch (str.ToLower())
        {
            
case "int":
                str 
= "Int";
                
break;
            
case "nchar":
                str 
= "NChar";
                
break;
            
case "bit":
                str 
= "Bit";
                
break;
            
case "nvarchar":
                str 
= "NVarChar";
                
break;
            
case "datetime":
                str 
= "DataTime";
                
break;
            
case "varchar":
                str 
= "VarChar";
                
break;
            
case "smalldatetime":
                str 
= "SmallDateTime";
                
break;
            
case "decimal":
                str 
= "Decimal";
                
break;
            
case "float":
                str 
= "Float";
                
break;
            
case "char":
                str 
= "Char";
                
break;
            
case "text":
                str 
= "Text";
                
break;
            
case "money":
                str 
= "Money";
                
break;
            
default:
                
throw new Exception("没有找到匹配数据类型");
                
break;
        }
        
return str;
    }
    
public string SubStr(string str)
    {
        
return str.Substring(1, str.Length - 1);
    }

 代码下载

posted @ 2008-04-17 17:07  索马  阅读(578)  评论(0编辑  收藏  举报