为了编写sql查询,我需要能够采用.NET类型并获取PostgreSQL类型的字符串。例如,我需要从
开始typeof(int) --> "int"
这很棘手,因为typeof(int)是System.Int32。我找到了一些表such as this one,但我想知道是否有一个类自动执行它而不是自己编写所有映射代码。
答案 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");
}
}