SQL Server to .Net type conversions(SQL Server 到 .Net 类型的转换)
问题描述
我有以下映射,用于在 SQL Server 类型、SQLData 类型和 .NET 类型之间进行转换:
I have the following map that I use to convert betwen SQL Server types, SQLData types, and .NET types:
/// <summary>
/// The map of types. THis maps all the corresponding types between sql server types, .net sql types, and .net types
/// </summary>
public static List<SqlTypeConversionHolder> TypeList = new List<SqlTypeConversionHolder>()
{
new SqlTypeConversionHolder("bigint", typeof(SqlInt64),typeof(Int64)),
new SqlTypeConversionHolder("binary", typeof(SqlBytes),typeof(Byte[])),
new SqlTypeConversionHolder("bit", typeof(SqlBoolean),typeof(Boolean)),
new SqlTypeConversionHolder("char", typeof(SqlChars),typeof(char)), //this one may need work
new SqlTypeConversionHolder("cursor", null,null),
new SqlTypeConversionHolder("date", typeof(SqlDateTime),typeof(DateTime)),
new SqlTypeConversionHolder("datetime", typeof(SqlDateTime),typeof(DateTime)),
new SqlTypeConversionHolder("datetime2", null,typeof(DateTime)),
new SqlTypeConversionHolder("DATETIMEOFFSET", null,typeof(DateTimeOffset)),
new SqlTypeConversionHolder("decimal", typeof(SqlDecimal),typeof(Decimal)),
new SqlTypeConversionHolder("float", typeof(SqlDouble),typeof(Double)),
//new SqlTypeConversionHolder("geography", typeof(SqlGeography),typeof(null));
//new SqlTypeConversionHolder("geometry", typeof(SqlGeometry),typeof(null));
//new SqlTypeConversionHolder("hierarchyid", typeof(SqlHierarchyId),typeof(null));
new SqlTypeConversionHolder("image", null,null),
new SqlTypeConversionHolder("int", typeof(SqlInt32),typeof(Int32)),
new SqlTypeConversionHolder("money", typeof(SqlMoney),typeof(Decimal)),
new SqlTypeConversionHolder("nchar", typeof(SqlChars),typeof(String)),
new SqlTypeConversionHolder("ntext", null,null),
new SqlTypeConversionHolder("numeric", typeof(SqlDecimal),typeof(Decimal)),
new SqlTypeConversionHolder("nvarchar", typeof(SqlChars),typeof(String)),
new SqlTypeConversionHolder("nvarchar(1)", typeof(SqlChars),typeof(Char)),
new SqlTypeConversionHolder("nchar(1)", typeof(SqlChars),typeof(Char)),
new SqlTypeConversionHolder("real", typeof(SqlSingle),typeof(Single)),
new SqlTypeConversionHolder("rowversion", null,typeof(Byte[])),
new SqlTypeConversionHolder("smallint", typeof(SqlInt16),typeof(Int16)),
new SqlTypeConversionHolder("smallmoney", typeof(SqlMoney),typeof(Decimal)),
new SqlTypeConversionHolder("sql_variant", null,typeof(Object)),
new SqlTypeConversionHolder("table", null,null),
new SqlTypeConversionHolder("text", typeof(SqlString),typeof(string)), //this one may need work
new SqlTypeConversionHolder("time", null,typeof(TimeSpan)),
new SqlTypeConversionHolder("timestamp", null,null),
new SqlTypeConversionHolder("tinyint", typeof(SqlByte),typeof(Byte)),
new SqlTypeConversionHolder("uniqueidentifier", typeof(SqlGuid),typeof(Guid)),
new SqlTypeConversionHolder("varbinary", typeof(SqlBytes),typeof(Byte[])),
new SqlTypeConversionHolder("varbinary(1)", typeof(SqlBytes),typeof(byte)),
new SqlTypeConversionHolder("binary(1)", typeof(SqlBytes),typeof(byte)),
new SqlTypeConversionHolder("varchar", typeof(SqlString),typeof(string)), //this one may need work
new SqlTypeConversionHolder("xml", typeof(SqlXml),typeof(string))
};
SqlTypeConversionHolder
的第一个参数是 sql server 类型的名称.第二个参数是 .Net Sql 类型.第三种是.net类型.
The first parameter of a SqlTypeConversionHolder
is the name of the sql server type. The second parameter is the .Net Sql type. The third is the .net type.
那些说 This one may need work
是我不确定的.有人可以就正确的转换提供一些帮助吗?在 MSDN 上查看,似乎他们没有正确的转换类型.我觉得这很难相信.从 SQL Server 类型到 .NET 类型必须有某种方法.
The ones that say This one may need work
are the ones that I am not sure about. Could someone provide some help as to what the proper conversions would be? Looking on MSDN, it seems as if they dont have a proper conversion type. I find that hard to beleive. There has to be someone way to go from the SQL Server type to the .NET type.
推荐答案
char
不是 SQL Server char
的正确 .NET 数据类型.它必须转换为 char[]
或 string
,因为 SQL char
可以包含多个字符.
char
is not the correct .NET data type for a SQL Server char
. It must be converted to either char[]
or string
, since a SQL char
can hold more than one character.
微软参考这里同意我的看法.其他的看起来是正确的.
The Microsoft reference here agrees with me. The other ones look correct.
这篇关于SQL Server 到 .Net 类型的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SQL Server 到 .Net 类型的转换
基础教程推荐
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- rabbitmq 的 REST API 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01