Webservice自动表生成TableForGen

using System;
using System.Linq;
using System.Web;
using System.Collections.Generic;

namespace SVL
{
    [Serializable]
    public class TableForGen
    {
        public string TableName;
        public ColumnInfo[] Columns;

        public TableForGen() { }

  

    }
}

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.OleDb;

namespace SVL
{
    /// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        private System.Windows.Forms.Button button1;

        [WebMethod]
        public string AddTable(TableForGen tg)
        {
            string sql="";

            sql += "Create table [" + tg.TableName + "](";
            for (int i = 0; i < tg.Columns.Length; i++)
            {

                sql += "[" + tg.Columns[i].ColumnName + "] ";
                if (tg.Columns[i].ColumnType == "int")
                {
                    //INT无需字段长度
                    sql += " [int] ";
                }
                else if (tg.Columns[i].ColumnType == "ntext")
                {
                    //ntext无需字段长度
                    sql += " [ntext] ";

                }
                else if (tg.Columns[i].ColumnType == "image")
                {
                    //image无需字段长度
                    sql += " [image] ";
                }
                else if (tg.Columns[i].ColumnType == "nvarchar")
                {
                    sql += "[nvarchar]";

                    if (tg.Columns[i].ColumnSize != "")
                    {
                        sql += "  (" + tg.Columns[i].ColumnSize + ") ";
                    }

                }
                else if (tg.Columns[i].ColumnType == "decimal")
                {
                    sql += "[decimal]";

                    if (tg.Columns[i].ColumnSize != "")
                    {
                        sql += "  (" + tg.Columns[i].ColumnSize + ") ";
                    }

                }
                else
                {
                    sql += "[nvarchar] (50)";
                }

                if (tg.Columns[i].IsNull)
                {
                    sql += " NULL";
                }

                //如果最后一个则没有逗号
                if (i != tg.Columns.Length - 1)
                {

                    sql += ",";
                }
                else
                {
                    sql += ") ON [PRIMARY]";
                }
              
           
            }

            return sql;
           
        }

        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(0, 0);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SVL
{
    [Serializable]
    public class ColumnInfo
    {
        public string ColumnName;
        public string ColumnType;
        public string ColumnSize;
        public bool IsNull;

       
        //public ColumnInfo(string columnName, string columnType, string columnSize, bool isNull)
        //{
        //    this.ColumnName = columnName;
        //    this.ColumnType = columnType;
        //    this.ColumnSize = columnSize;
        //    this.IsNull = isNull;
        //}
        public ColumnInfo() { }

    }
}

 

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SVL_Demo.SVC;

namespace SVL_Demo
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SVL_Demo.SVC.TableForGen tg = new SVL_Demo.SVC.TableForGen();
            tg.Columns = new ColumnInfo[5];

            tg.TableName = "myTable";

            SVL_Demo.SVC.ColumnInfo tc1 = new SVL_Demo.SVC.ColumnInfo();
            tc1.ColumnName="c1";
            tc1.ColumnType="int";
            tc1.ColumnSize="";
            tc1.IsNull = false;

            SVL_Demo.SVC.ColumnInfo tc2 = new SVL_Demo.SVC.ColumnInfo();
            tc2.ColumnName="c2";
            tc2.ColumnType="nvarchar";
            tc2.ColumnSize="59";
            tc2.IsNull = false;

            SVL_Demo.SVC.ColumnInfo tc3 = new SVL_Demo.SVC.ColumnInfo();
            tc3.ColumnName = "c3";
            tc3.ColumnType = "decimal";
            tc3.ColumnSize = "18,2";
            tc3.IsNull = true;

            SVL_Demo.SVC.ColumnInfo tc4 = new SVL_Demo.SVC.ColumnInfo();
            tc4.ColumnName = "c4";
            tc4.ColumnType = "image";
            tc4.ColumnSize = "";
            tc4.IsNull = true;

            SVL_Demo.SVC.ColumnInfo tc5 = new SVL_Demo.SVC.ColumnInfo();
            tc5.ColumnName = "字段5";
            tc5.ColumnType = "ntext";
            tc5.ColumnSize = "";
            tc5.IsNull = true;

           
            tg.Columns[0] = tc1;
            tg.Columns[1] = tc2;
            tg.Columns[2] = tc3;
            tg.Columns[3] = tc4;
            tg.Columns[4] = tc5;


            SVL_Demo.SVC.Service1 s = new SVL_Demo.SVC.Service1();
            Response.Write(s.AddTable(tg));
        }
    }
}

 

posted @ 2009-07-08 16:24  昕友软件开发  阅读(250)  评论(0编辑  收藏  举报
欢迎访问我的开源项目:xyIM企业即时通讯