schangxiang@126.com
2025-05-06 358e8f30447f8ba05af42b66d45ddf1a93878b6d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using GenerateCode_GEBrilliantFactory;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace GenerateCode_WeiBen_WMS.DataBaseFactory
{
    public class SqlServerDataBaseServiceHandle : DataBaseServiceHandle
    {
        public override List<ColumnModel> GetColumnList(string tableName, string connStr)
        {
            string strSql = @" select
col.name as ColumnName,
t.name as DataType,
ep.value as Description,
col.max_length as DataLength,
col.is_nullable as IsNullable,
 
(
    select top 1 ind.is_primary_key from sys.index_columns ic
    left join sys.indexes ind
    on ic.object_id=ind.object_id
    and ic.index_id=ind.index_id
    and ind.name like 'PK_%'
    where ic.object_id=obj.object_id
    and ic.column_id=col.column_id
) as IsPrimaryKey,
col.Precision as MyPrecision,
col.Scale 
from sys.objects obj
inner join sys.columns col
on obj.object_id=col.object_id
left join sys.types t
on t.user_type_id=col.user_type_id
left join sys.extended_properties ep
on ep.major_id=obj.object_id
and ep.minor_id=col.column_id
and ep.name='MS_Description'
where obj.name='" + tableName + "'  ";
 
 
            List<ColumnModel> columnList = new List<ColumnModel>();
            try
            {
                DataSet ds = SqlHelper.Query(connStr, strSql);
                columnList = DataTableToList(ds.Tables[0]);
            }
            catch
            {
                throw;
            }
            finally
            {
            }
            return columnList;
        }
 
        public override List<ColumnModel> DataTableToList(DataTable dt)
        {
            try
            {
                List<ColumnModel> modelList = new List<ColumnModel>();
                int rowsCount = dt.Rows.Count;
                if (rowsCount > 0)
                {
                    ColumnModel model;
                    for (int n = 0; n < rowsCount; n++)
                    {
                        model = new ColumnModel();
                        if (dt.Rows[n]["Precision"].ToString() != "")
                        {
                            model.MyPrecision = int.Parse(dt.Rows[n]["Precision"].ToString());
                        }
                        if (dt.Rows[n]["Scale"].ToString() != "")
                        {
                            model.Scale = int.Parse(dt.Rows[n]["Scale"].ToString());
                        }
                        model.ColumnName = dt.Rows[n]["ColumnName"].ToString();
                        model.DataLength = dt.Rows[n]["DataLength"].ToString();
                        model.DataType = dt.Rows[n]["DataType"].ToString();
 
                        if (model.DataType.ToUpper() == "nvarchar".ToUpper())
                        {
                            if (model.DataLength != "-1")
                            {
                                model.DataLength = ((int)Convert.ToInt32(model.DataLength) / 2).ToString();
                            }
                        }
 
                        model.Description = dt.Rows[n]["Description"].ToString();
                        if (dt.Rows[n]["IsNullable"].ToString() != "")
                        {
                            if ((dt.Rows[n]["IsNullable"].ToString() == "1") || (dt.Rows[n]["IsNullable"].ToString().ToLower() == "true"))
                            {
                                model.IsNullable = true;
                            }
                            else
                            {
                                model.IsNullable = false;
                            }
                        }
                        if (dt.Rows[n]["IsPrimaryKey"].ToString() != "")
                        {
                            if ((dt.Rows[n]["IsPrimaryKey"].ToString() == "1") || (dt.Rows[n]["IsPrimaryKey"].ToString().ToLower() == "true"))
                            {
                                model.IsPrimaryKey = true;
                            }
                            else
                            {
                                model.IsPrimaryKey = false;
                            }
                        }
 
                        modelList.Add(model);
                    }
                }
                return modelList;
            }
            catch (Exception)
            {
 
                throw;
            }
        }
 
    }
}