Obtain table information according to the table name, including field descriptions, and generate C# class attributes

/********************************************  
* Get table information according to the table name, including field descriptions, and generate C# class attributes      
********************************************/     
Create PROC [dbo].[sp_help_table]   
(@tableName VARCHAR(200), @ColumnLike VARCHAR(200) = NULL)           
AS     
--If the table name does not exist, select the similar table directly  
IF NOT EXISTS(  
       SELECT 1  
       FROM   sysobjects  
       WHERE  id = OBJECT_ID(@tableName)  
              AND TYPE = 'U'  
   )  
BEGIN  
    SELECT NAME FROM   sysobjects  
    WHERE  NAME LIKE '%' + @tableName + '%' AND TYPE = 'U'     
    RETURN  
END   
   
   
-- filter similar column names  
IF (@ColumnLike IS NULL)  
    SET @ColumnLike = ''  
     
DECLARE @ColumnTable TABLE(cName VARCHAR(200))     
INSERT @ColumnTable  
  (  
    cName  
  )  
SELECT a.name  
FROM   syscolumns a,sysobjects d  
WHERE  a.id = d.id  
       AND d.name = @tableName  
       AND a.name LIKE '%' + @ColumnLike + '%'     
       
--Query table structure information             
SELECT table name = CASE  
                   WHEN a.colorder = 1 THEN d.name  
                   ELSE ''  
              END,  
       Table Description = CASE  
                     WHEN a.colorder = 1 THEN ISNULL(f.value, '')  
                     ELSE ''  
                END,  
       field number = a.colorder,  
       fieldname = a.name,  
       field description = ISNULL(g.[value], ''),  
       ID=CASE  
                   WHEN COLUMNPROPERTY (a.id, a.name, 'IsIdentity') = 1 THEN '√'  
                   ELSE ''  
              END,  
       primary key = CASE  
                   WHEN EXISTS(  
                            SELECT 1 FROM   sysobjects WHERE  xtype = 'PK' AND parent_obj = a.id  
                                   AND NAME   IN (SELECT NAME FROM   sysindexes  
                                                  WHERE  indid   IN (SELECT indid FROM sysindexkeys  
                                                                     WHERE  id = a.id AND  colid = a.colid))  
                        ) THEN '√'  
                   ELSE ''  
              END,  
       type = b.name,  
       Occupied bytes = a.length,  
       length = COLUMNPROPERTY(a.id, a.name, 'PRECISION'),  
       Number of decimal places = ISNULL(COLUMNPROPERTY(a.id, a.name, 'Scale'), 0),  
       allow null = CASE WHEN a.isnullable = 1 THEN '√'  
                     ELSE ''  
                END,  
       Default = ISNULL(e.text, ''),  
        [c# class field] =    
        case when g.[value] is not null then '/// <summary>'+CHAR(13)+'/// '+cast(g.[value] as nvarchar) + CHAR(13)+'/// </summary>' + CHAR(13) else '' end  
        + '[DataMember]' + CHAR(13) + 'public '  
        + case when b.name='bigint' then 'long'   
                when b.name='nvarchar' or b.name='varchar' then 'string'  
                when b.name='int' or b.name='tinyint' then 'int'  
                when b.name='bit' then 'bool'  
                when b.name='datetime' then 'DateTime'  
        else b.name end +' '+a.name+' { get; set; } '+ CHAR(13)  
FROM   syscolumns a  
       LEFT   JOIN systypes b  
            ON  a.xusertype = b.xusertype  
       INNER   JOIN sysobjects d  
            ON  a.id = d.id  
            AND d.xtype = 'U'  
            AND d.name <> 'dtproperties'  
       LEFT   JOIN syscomments e  
            ON  a.cdefault = e.id  
       LEFT   JOIN sys.extended_properties g  
            ON  a.id = g.major_id  
            AND a.colid = g.minor_id  
       LEFT   JOIN sys.extended_properties f  
            ON  d.id = f.major_id  
            AND f.minor_id = 0  
                --where d.name='table to query' --if only the specified table is queried, add this condition  
WHERE  d.name = @tableName  
       AND EXISTS(  
               SELECT 1  
               FROM   @ColumnTable  
               WHERE  cname = a.name  
           )  
ORDER BY a.id,a.colorder  

Reprinted from: https://blog.csdn.net/guochunyang/article/details/50580705

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325759027&siteId=291194637
Recommended