• 数据库获取所有的表,所有的视图,字段说明,字段类型,字段名


    //表、视图 绑定给下拉
    public void BindDropName() { string sql = "Select Name FROM SysObjects Where XType='U' orDER BY Name";// DataTable dt = SqlOP.ExecuteDataset(sql).Tables[0]; DataSet dv=SqlOP.ExecuteDataset("select [name] as Name from sys.views ");//视图 foreach (DataRow dr in dv.Tables[0].Rows) { DataRow row = dt.NewRow(); row["Name"] = dr[0]; dt.Rows.Add(row);//这样就可以添加了 } DropDownList1.DataSource = dt; DropDownList1.DataTextField = "Name"; DropDownList1.DataValueField = "Name"; DropDownList1.DataBind(); }

    生成实体

          protected void ToEity()
            {
                string sql = "SELECT   CAST(g.value AS nvarchar)as notes,  a.name,b.name as ztype ,c.isnullable FROM     systypes b,    sys.columns AS a LEFT OUTER JOIN  sys.syscolumns AS c ON a.name = c.name AND a.object_id = c.id left join sys.extended_properties g on (a.object_id = g.major_id AND a.column_id=g.minor_id) WHERE   (a.object_id = OBJECT_ID('" + DropDownList1.SelectedValue + "'))and c.xtype=b.xusertype order by object_id,a.column_id";
    
                DataSet ds = SqlOP.ExecuteDataset(sql);
                string json = "using System;
     using System.Text;
     using System.Collections; 
     using System.Collections.Generic;
     namespace mode 
     {
     public  class  " + DropDownList1.SelectedValue + "
    { ";
    
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    json += " /// <summary> 
     ///   " + dr[0].ToString() + " 
     /// </summary> 
     public " + sqlType(dr[2].ToString(), dr[3].ToString()) + " " + dr[1].ToString() + " 
    { 
     get;
     set;
     }
    ";
                }
                json += "} 
    }";
                txtVaule.Text = json;
            }
    
    
            protected string sqlType(string xtpye, string isnullable)
            {
    
                switch (xtpye)
                {
                    case "uniqueidentifier":
                        if (isnullable.Equals("1"))
                        {
                            return "Guid?";
                        }
                        else
                        {
                            return "Guid";
                        }
                        break;
                    case "nvarchar":
                        return "String";
                        break;
                    case "varchar":
                        return "String";
                        break;
                    case "sysname":
                        return "String";
                        break;
                    case "text":
                        return "String";
                        break;
                    case "char":
                        return "String";
                        break;
                    case "int":
                        if (isnullable.Equals("1"))
                        {
                            return "Int32?";
                        }
                        else
                        {
                            return "Int32";
                        }
                        break;
                    case "tinyint":
                        if (isnullable.Equals("1"))
                        {
                            return "Byte?";
                        }
                        else
                        {
                            return "Byte";
                        }
                        break;
    
                    case "bit":
                        if (isnullable.Equals("1"))
                        {
                            return "Byte?";
                        }
                        else
                        {
                            return "Byte";
                        }
                        break;
                    case "datetime":
                        if (isnullable.Equals("1"))
                        {
                            return "DateTime?";
                        }
                        else
                        {
                            return "DateTime";
                        }
    
                        break;
    
                    case "float":
                        if (isnullable.Equals("1"))
                        {
                            return "Double?";
                        }
                        else
                        {
                            return "Double";
                        }
    
                        break;
                    case "decimal":
                        if (isnullable.Equals("1"))
                        {
                            return "Decimal?";
                        }
                        else
                        {
                            return "Decimal";
                        }
    
                        break;
                    default:
                        return "String";
                        break;
                }
    
    
            }
  • 相关阅读:
    在WIN10下搭建Robot Framework环境
    Robot Framework导入selenium2library库不成功的解决方法
    git命令大全
    Axure RP 8过期,用户名和序列号(注册码)
    MySQL用命令行快速导出数据备份
    JMeter—正则表达式提取器
    用JMeter进行接口测试
    d3-project
    bower 问题
    andriod studio
  • 原文地址:https://www.cnblogs.com/lucoo/p/5056733.html
Copyright © 2020-2023  润新知