C#中的数据类型到SQL Server数据类型 [英] C# data types to SQL Server data types
本文介绍了C#中的数据类型到SQL Server数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎样才能转换C#的数据类型到SQL Server数据类型(即 SqlDbType
已知)
即
C# - > 字符串
SQL服务器 - > N'String'
解决方案
试试这个:
它是一种扩展类,因此该文件要使用上添加这些方法:
使用工具;
下面是代码:
使用系统;
使用System.Data这;
使用Microsoft.SqlServer.Server;
命名空间实用程序
{
公共静态类TypeExtension
{
公共静态SqlDbType ToSqlDbType(这种类型clrType)
{
变种S =新SqlMetaData(,SqlDbType.NVarChar,clrType);
返回s.SqlDbType;
}
公共静态类型ToClrType(SqlDbType则sqlType)
{
开关(则sqlType)
{
情况SqlDbType。 BigInt有:
返回的typeof(?长);
情况下SqlDbType.Binary:
情况下SqlDbType.Image:
情况下SqlDbType.Timestamp:
情况下SqlDbType.VarBinary:
返回的typeof(字节[] );
情况下SqlDbType.Bit:
返回的typeof(?布尔);
情况下SqlDbType.Char:
情况下SqlDbType.NChar:
情况下SqlDbType.NText:
情况下SqlDbType.NVarChar:
情况下SqlDbType.Text:
情况下SqlDbType.VarChar:
情况下SqlDbType.Xml:
返回typeof运算(字符串);
情况下SqlDbType.DateTime:
情况下SqlDbType.SmallDateTime:
情况下SqlDbType.Date:
情况下SqlDbType.Time:
情况下SqlDbType.DateTime2:
返回的typeof(DateTime的);
情况下SqlDbType.Decimal:
情况下SqlDbType.Money:
情况下SqlDbType.SmallMoney:
返回的typeof(?十进制);
情况下SqlDbType.Float:
返回的typeof(?双);
情况下SqlDbType.Int:
返回的typeof(?INT);
情况下SqlDbType.Real:
返回的typeof(浮动);
情况下SqlDbType.UniqueIdentifier:
返回的typeof(?GUID);
情况下SqlDbType.SmallInt:
返回的typeof(?短);
情况下SqlDbType.TinyInt:
返回的typeof(?字节);
情况下SqlDbType.Variant:
情况下SqlDbType.Udt:
返回的typeof(对象);
情况下SqlDbType.Structured:
返回的typeof(数据表);
情况下SqlDbType.DateTimeOffset:
返回的typeof(?的DateTimeOffset);
默认:
抛出新ArgumentOutOfRangeException(则sqlType);
}
}
}
}
How can I "convert" C# datatype to SQL Server datatype (the SqlDbType
is known)
i.e:
C# -> "String"
SQL Server -> N'String'
解决方案
Try This: Its a Extension class, so on the file you want to use these methods on add:
using Utility;
Here is the code:
using System;
using System.Data;
using Microsoft.SqlServer.Server;
namespace Utility
{
public static class TypeExtension
{
public static SqlDbType ToSqlDbType(this Type clrType)
{
var s = new SqlMetaData("", SqlDbType.NVarChar, clrType);
return s.SqlDbType;
}
public static Type ToClrType(SqlDbType sqlType)
{
switch (sqlType)
{
case SqlDbType.BigInt:
return typeof (long?);
case SqlDbType.Binary:
case SqlDbType.Image:
case SqlDbType.Timestamp:
case SqlDbType.VarBinary:
return typeof (byte[]);
case SqlDbType.Bit:
return typeof (bool?);
case SqlDbType.Char:
case SqlDbType.NChar:
case SqlDbType.NText:
case SqlDbType.NVarChar:
case SqlDbType.Text:
case SqlDbType.VarChar:
case SqlDbType.Xml:
return typeof (string);
case SqlDbType.DateTime:
case SqlDbType.SmallDateTime:
case SqlDbType.Date:
case SqlDbType.Time:
case SqlDbType.DateTime2:
return typeof (DateTime?);
case SqlDbType.Decimal:
case SqlDbType.Money:
case SqlDbType.SmallMoney:
return typeof (decimal?);
case SqlDbType.Float:
return typeof (double?);
case SqlDbType.Int:
return typeof (int?);
case SqlDbType.Real:
return typeof (float?);
case SqlDbType.UniqueIdentifier:
return typeof (Guid?);
case SqlDbType.SmallInt:
return typeof (short?);
case SqlDbType.TinyInt:
return typeof (byte?);
case SqlDbType.Variant:
case SqlDbType.Udt:
return typeof (object);
case SqlDbType.Structured:
return typeof (DataTable);
case SqlDbType.DateTimeOffset:
return typeof (DateTimeOffset?);
default:
throw new ArgumentOutOfRangeException("sqlType");
}
}
}
}
这篇关于C#中的数据类型到SQL Server数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文