如何取到sqlserver的表的列信息?使用ado.net

DataBind 2005-02-22 04:31:48
我需要返回列的类型(SqlDbType,为了和SqlParameter统一)
...全文
112 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
marvelstack 2005-02-22
  • 打赏
  • 举报
回复
http://blog.csdn.net/zhzuo/archive/2004/07/03/33273.aspx
lyvvvv 2005-02-22
  • 打赏
  • 举报
回复
SELECT
表名=case when a.colorder=1 then d.name else '' end,
表说明=case when a.colorder=1 then isnull(f.value,'') else '' end,
字段序号=a.colorder,
字段名=a.name,
标识=case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end,
主键=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and name in (
SELECT name FROM sysindexes WHERE indid in(
SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid
))) then 'true' else 'false' end,
类型=b.name,
占用字节数=a.length,
长度=COLUMNPROPERTY(a.id,a.name,'PRECISION'),
小数位数=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允许空=case when a.isnullable=1 then 'true'else 'false' end,
默认值=isnull(e.text,''),
字段说明=isnull(g.[value],'')
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 sysproperties g on a.id=g.id and a.colid=g.smallid
left join sysproperties f on d.id=f.id and f.smallid=0
where d.name='BUG' --如果只查询指定表,加上此条件
order by a.id,a.colorder
web_gus 2005-02-22
  • 打赏
  • 举报
回复
up
xixigongzhu 2005-02-22
  • 打赏
  • 举报
回复
ms-help://MS.NETFrameworkSDKv1.1.CHS/cpref/html/frlrfSystemDataSqlDbTypeClassTopic.htm
系统类型与sql类型的对应表
amendajing 2005-02-22
  • 打赏
  • 举报
回复
mark
doubon 2005-02-22
  • 打赏
  • 举报
回复
顶!
wcbgyjs 2005-02-22
  • 打赏
  • 举报
回复
请看系统表:
数据类型表systypes
表、存储过程、视图等信息表sysobjects
列信息表syscolumns
jackie615 2005-02-22
  • 打赏
  • 举报
回复
up
JasonHeung 2005-02-22
  • 打赏
  • 举报
回复
/// <summary>
/// 将出据库类型转换为系统数据类型
/// </summary>
/// <param name="byType"></param>
/// <returns></returns>
public static string GetSystemType(byte byType)
{
string str = "";
switch (byType)
{
case 34://image
str = "System.Byte";
break;
case 35://Text
str = "System.String";
break;
case 36:
str = "System.Int32";
break;
case 48:///byte
str = "System.Byte";
break;
case 52://smallint
str = "System.Int16";
break;
case 56://int
str = "System.Int32";
break;
case 58://datetime
str = "System.DateTime";
break;
case 59://real
str = "System.Double";
break;
case 60://money
str = "System.Decimal";
break;
case 61://datetime
str = "System.DateTime";
break;
case 62:
str = "System.Decimal";
break;
case 98:
str = "System.Object";
break;
case 99:
str = "System.String";
break;
case 104://bit
str = "System.Byte";
break;
case 106://decimal
str = "System.Decimal";
break;
case 108://
str = "System.Decimal";
break;
case 122:
str = "System.Decimal";
break;
case 127://bigint
str = "System.Int64";
break;
case 167://varchar
str = "System.String";
break;
case 165:
str = "System.Byte";
break;
case 173://binary
str = "System.Byte";
break;
case 175:
str = "System.String";
break;
case 189:
str = "System.Int64";
break;
case 231:
str = "System.String";
break;
case 239:
str = "System.String";
break;

}
return str;
}
JasonHeung 2005-02-22
  • 打赏
  • 举报
回复
CREATE PROCEDURE DBO.GETUSERCOLUMNS @TABLEID INTEGER AS
SELECT NAME,ID,XTYPE,TYPESTAT,XUSERTYPE,LENGTH,COLID,XPREC,XSCALE,COLSTAT,ISOUTPARAM FROM SYSCOLUMNS
WHERE ID = @TABLEID

/// <summary>
/// 取对应SQL定义的数据类型
/// </summary>
/// <param name="byType">数据类型id</param>
/// <returns>数据类型</returns>
public static SqlDbType GetSqlType(byte byType)
{
SqlDbType type = SqlDbType.Variant;
switch (byType)
{
case 34://image
type = SqlDbType.Image;
break;
case 35://Text
type = SqlDbType.Text;
break;
case 36://UniqueIdentifier
type = SqlDbType.UniqueIdentifier;
break;
case 48:///byte
type = SqlDbType.TinyInt;
break;
case 52://smallint
type = SqlDbType.SmallInt;
break;
case 55://decimal
type = SqlDbType.Decimal;
break;
case 56://int
type = SqlDbType.Int;
break;
case 58:
type = SqlDbType.SmallDateTime;
break;
case 59:
type = SqlDbType.Real;
break;
case 60://decimal
type = SqlDbType.Money;
break;
case 61://datetime
type = SqlDbType.DateTime;
break;
case 62://float
type = SqlDbType.Float;
break;
case 98://smallint
type = SqlDbType.Variant;
break;
case 99:
type = SqlDbType.NText;
break;
case 104://bit
type = SqlDbType.Bit;
break;
case 106://decimal
type = SqlDbType.Decimal;
break;
case 108://numeric
type = SqlDbType.Decimal;
break;
case 122://smallmoney
type = SqlDbType.SmallMoney;
break;
case 127://bigint
type = SqlDbType.BigInt;
break;
case 165://varbinery
type = SqlDbType.VarBinary;
break;
case 167://varchar
type = SqlDbType.VarChar;
break;
case 173://binary
type = SqlDbType.Binary;
break;
case 175://char
type = SqlDbType.Char;
break;
case 189://timestamp
type = SqlDbType.Timestamp;
break;
case 231:
type = SqlDbType.NVarChar;
break;
case 239:
type = SqlDbType.NChar;
break;
}
return type;
}
lazyfish 2005-02-22
  • 打赏
  • 举报
回复
SqlDataReader reader = command.ExecuteReader(CommandBehavior.KeyInfo|CommandBehavior.SchemaOnly|CommandBehavior.Default) ;
DataTable tableInfo = reader.GetSchemaTable() ;

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧