你怎么能转换" TINYINT" T-SQL的C#为整数? [英] How can you convert "tinyint" of t-sql to integer in c#?

查看:548
本文介绍了你怎么能转换" TINYINT" T-SQL的C#为整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中有一个 TINYINT 列,我希望将它转换为的Int32 SqlDataReader的

I have a tinyint column in the database and I wish to convert it to Int32 for an SqlDataReader.

我要如何做呢?

编辑#1

最近,我不得不这样做。

I recently had to do this.

int a = dataReader.GetByte(dr.GetOrdinal("ColumnName"));

#In除了答案

SQL Server数据类型映射

SQL Server Data Type Mappings

bigint           - GetInt64  
binary           - GetBytes  
int              - GetInt32  
money            - GetDecimal  
rowversion       - GetBytes  
smallint         - GetInt16  
tinyint          - GetByte  
uniqueidentifier - GetGuid   
...

有关更多信息请访问 - SQL Server的数据类型映射

For more info visit - SQL Server Data Type Mappings

推荐答案

这是什么正常回来的 - 字节?如果是这样,只是做一个拆箱再转换:

What does it normally come back as - byte? If so, just do an unbox and then a convert:

(int)(byte) reader["column"];

或只是让自然的转换发生:

or just let the conversion happen naturally:

int x = (byte) reader["column"];

或做同样的强类型的方法:

or do the same with the strongly typed methods:

int x = reader.GetByte(column);

调整这为sbyte 或什么,如果我错了,它映射到字节。你可以做在SQL Server端的转换,但在客户端我会亲自做它,而不是,并保持SQL简单。

Adjust this to sbyte or short or whatever if I'm wrong about it mapping to byte. You could do the conversion at the SQL Server side, but I'd personally do it at the client side instead, and keep the SQL simpler.

这篇关于你怎么能转换" TINYINT" T-SQL的C#为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆