博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Webservice自动表生成TableForGen
阅读量:4134 次
发布时间:2019-05-25

本文共 4059 字,大约阅读时间需要 13 分钟。

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));
        }
    }
}

 

转载地址:http://cvpvi.baihongyu.com/

你可能感兴趣的文章
2019年哪些外快收入可达到2万以上?
查看>>
【JavaScript 教程】标准库—Date 对象
查看>>
前阿里手淘前端负责人@winter:前端人如何保持竞争力?
查看>>
【JavaScript 教程】面向对象编程——实例对象与 new 命令
查看>>
我在网易做了6年前端,想给求职者4条建议
查看>>
SQL1015N The database is in an inconsistent state. SQLSTATE=55025
查看>>
RQP-DEF-0177
查看>>
Linux查看mac地址
查看>>
Linux修改ip
查看>>
MySQL字段类型的选择与MySQL的查询效率
查看>>
Java的Properties配置文件用法【续】
查看>>
JAVA操作properties文件的代码实例
查看>>
IPS开发手记【一】
查看>>
Java通用字符处理类
查看>>
文件上传时生成“日期+随机数”式文件名前缀的Java代码
查看>>
Java代码检查工具Checkstyle常见输出结果
查看>>
北京十大情人分手圣地
查看>>
Android自动关机代码
查看>>
Android中启动其他Activity并返回结果
查看>>
2009年33所高校被暂停或被限制招生
查看>>