是否有一个类自动将.NET框架类型转换为PostgreSQL类型?

时间:2015-07-14 19:00:46

标签: c# sql .net type-systems

为了编写sql查询,我需要能够采用.NET类型并获取PostgreSQL类型的字符串。例如,我需要从

开始
typeof(int) --> "int"

这很棘手,因为typeof(int)是System.Int32。我找到了一些表such as this one,但我想知道是否有一个类自动执行它而不是自己编写所有映射代码。

1 个答案:

答案 0 :(得分:1)

可能会起作用:

public static class DotNetTypeToPostgreSqlType
{
    public static string GetPostgreType(object o)
    {
        if (o is Int64)
        {
            return "int8";
        }
        if (o is Boolean)
        {
            return "bool";
        }
        if (o is Byte[])
        {
            return "bytea";
        }
        if (o is DateTime)
        {
            return "date";
        }
        if (o is Double)
        {
            return "float8";
        }
        if (o is Int32)
        {
            return "int4";
        }
        if (o is Decimal)
        {
            return "money";
        }
        if (o is Decimal)
        {
            return "numeric";
        }
        if (o is Single)
        {
            return "float4";
        }
        if (o is Int16)
        {
            return "int2";
        }
        if (o is String)
        {
            return "text";
        }
        if (o is DateTime)
        {
            return "time";
        }
        if (o is DateTime)
        {
            return "timetz";
        }
        if (o is DateTime)
        {
            return "timestamp";
        }
        if (o is DateTime)
        {
            return "timestamptz";
        }
        if (o is TimeSpan)
        {
            return "interval";
        }
        if (o is String)
        {
            return "varchar";
        }
        if (o is IPAddress)
        {
            return "inet";
        }
        if (o is Boolean)
        {
            return "bit";
        }
        if (o is Guid)
        {
            return "uuid";
        }
        if (o is Array)
        {
            return "array";
        }
        throw new NotImplementedException("Unknown postgresql type");
    }
}