|
我正在尝试获取已存储在SQL Server 2008 R2中的表的列名。/ _4 z; y2 ~4 h; ?" r% N+ u: ]/ H& f
我已经尝试了一切,但似乎找不到解决方法。+ o' d# H& N- M
现在这是我在C#中的代码& _3 C1 z/ y. P% _0 S6 j: b
public string[] getColumnsName()* ? I6 [7 L$ }6 x
{
8 J4 k7 l$ {7 ] List listacolumnas=new List();9 v0 S( o" l e k/ |
using (SqlConnection connection = new SqlConnection(Connection))+ b: t4 Z# b6 p' n' u) |5 ?
using (SqlCommand command = connection.CreateCommand())
! v4 m' A# {- l6 D {/ D ]7 g) Z" J* `* B
command.CommandText = "SELECT TOP 0 * FROM Usuarios";
f1 M# v$ p/ V0 c. V% a connection.Open();4 @$ b. j% T9 {' V/ g
using (var reader = command.ExecuteReader(CommandBehavior.KeyInfo))- X( d' n, a( g
{
8 u1 i8 v& o8 D# w reader.Read();
; \+ F8 _6 l# } var table = reader.GetSchemaTable();
. k- v* K6 D ~2 M3 I5 I0 G8 y8 O foreach (DataColumn column in table.Columns)
, p( j, O) \; d {6 t* u/ j) J" S& |. a6 C
listacolumnas.Add(column.ColumnName);
+ W1 e% _, n( X' Z2 \ }
9 m: M6 f7 Z+ L; h) F' u( P }
% o. ], ^. Y9 ] }4 J, Q7 O5 p. H. r
return listacolumnas.ToArray();: w* o3 e8 M( k: d3 Z7 _
}
, m: z7 g! G1 |; D% G但这又给了我以下 C) m, S V6 n5 Y4 D/ {( d y
ColumnName
9 i+ g9 l) k/ QColumnOrdinal
# ]/ n* l3 Q% z. m) G) B' T6 }ColumnSize
- P+ w! [8 l, FNumericPrecision) i M1 Q1 `' G" A: P
NumericScale" V; D* V8 u8 F. ^
IsUnique# Y: i" y# w( W" a) h j
IsKey
I, T- \2 c0 oBaseServerName
3 q% ?+ F4 i, H3 `BaseCatalogName
* S* u, }+ @8 \) h+ qBaseColumnName6 L) u# {' s# e: n0 o+ _& Q
BaseSchemaName' D' R! K |4 W, R4 \0 T9 e
BaseTableName
( Q- p' h; Q! m7 K6 pDataType3 J5 x) l, B% I7 B# a! U. M! P: C
AllowDBNull, w6 [- y3 n1 V5 |! C
ProviderType
# T2 o% X0 D( X' h& lIsAliased8 A# J" ?3 ^; W/ y
IsExpression
3 P; O2 k- u8 VIsIdentity0 N) D% A6 `- i. ]
IsAutoIncrement
# p/ V2 ~: w9 s4 ]# EIsRowVersion
# x3 {* `* v! H$ i A+ \IsHidden
) }: i) D4 ~' L4 }% B4 W* yIsLong
, ]1 {/ @4 I& ?& u! L. o- SIsReadOnly
/ P/ H3 }. m% h4 yProviderSpecificDataType8 |; t5 E. W3 E9 M" T* O5 c" v1 ] }
DataTypeName6 R9 ?7 j7 n$ {# G M0 S" s
XmlSchemaCollectionDatabase+ x+ k$ y1 N% B6 N* f `6 D
XmlSchemaCollectionOwningSchema
- W+ |/ q; B' b' T/ oXmlSchemaCollectionName2 p3 o% Z) A5 i6 l( t) j6 R
UdtAssemblyQualifiedName# j% D! U7 s* _& `; [
NonVersionedProviderType
3 l: J( q) y; d$ Q' Q5 V" YIsColumnSet
" K K# _6 _, {有任何想法吗?. |/ \1 c+ i2 b" f+ N6 E" j2 W
它显示标签,因为这是我的Web服务发送数据的方式。
& @% F6 {- R& Q/ z" l6 t( a . k/ O4 T$ Z7 H, ~/ Q0 s) v
解决方案:, [1 j' s4 T; a! ~; j1 @6 w2 u
8 L6 a/ a+ v9 ]) D
- ^8 R( b( G; U2 d, J) u
4 o( U5 j" v* m' ]% ~; S; \$ e
您可以使用下面的查询来获取表的列名。下面的查询获取给定名称的用户表的所有列:; g% C7 d9 _. J" Z
select c.name from sys.columns c
* ?$ J* S1 g& }6 Ainner join sys.tables t 9 [& h: E2 Z& ]& @% \, A2 n
on t.object_id = c.object_id2 }4 g; p; t8 e) L5 E/ X7 i
and t.name = 'Usuarios' and t.type = 'U'& f+ j4 P: c, ]/ |( [; I9 L
' {; F5 K% D/ k( s6 s在您的代码中,它将如下所示:
! S5 w9 Z+ F( u5 R2 g/ x9 Upublic string[] getColumnsName()
& {# y# p' m0 }+ A9 _+ H{
' C6 g3 P' M- n/ E' R6 W% H+ ` List listacolumnas=new List();, o6 V2 ~0 x0 Q: p( f
using (SqlConnection connection = new SqlConnection(Connection))& G8 k0 S" a! z* ?5 l' L
using (SqlCommand command = connection.CreateCommand())
/ x4 P: k. D8 ]3 e( p' ?: u. }$ o2 p3 u {% d, z% ~" ?; \5 Z9 ]8 s$ e7 d) e
command.CommandText = "select c.name from sys.columns c inner join sys.tables t on t.object_id = c.object_id and t.name = 'Usuarios' and t.type = 'U'";+ T" J- ]9 K3 k% z; E4 O
connection.Open();
. J7 P s( L( ` using (var reader = command.ExecuteReader())3 B7 \* Y! {) [5 ^& ^6 v9 q
{
( d. ] B* y) ^( [* ` while (reader.Read())+ [" m/ C/ f5 {% J2 M3 j
{
# ?0 H e% H! @6 J- b5 u" Q listacolumnas.Add(reader.GetString(0));# l9 W' _7 }; J. j2 [7 W
}
. e* s7 A$ h" Y# v$ ] }
, v3 f: k& A' u; s" v }* ?; e; _ i8 W! T% ]4 K: @
return listacolumnas.ToArray();
* {7 m; r+ x$ A7 v* M} |
|