得到的值作为数据类型字符串传递 [英] Get datatype from values passed as string
问题描述
我写一个框架,将连接到许多不同的数据源类型和从这些来源返回值。最简单的是SQL,Access和Oracle。该强硬的是Sharepoint的,CSV。
I am writing a framework that will connect to many different data source types and return values from these sources. The easy ones are SQL, Access and Oracle. The tougher ones are Sharepoint, CSV.
如果我回到从基于文本的来源值,我想确定数据的数据类型。
If I return values from text based sources, I would like to determine the datatype of the data.
由于一个CSV中所有文字,没有询问,我需要以某种方式解析数据,以确定数据类型的元数据
Since a CSV is all text, there is no metadata to interrogate, I would need to parse the data somehow to determine the data type.
例如:
名单, 0,1,0将是1,4,布尔结果,
名单-10,500是整数结果,
列表的15.2,2015.5896,1.0245,500将是2001年1月1日,2010/05/29 12:00的双重结果
名单,1990 / 12/25 10时34分21秒将日期时间
List of "true", "true", "false", "false" would be boolean
List of "1", "0", "1", "0" would be boolean
List of "1", "4", "-10", "500" would be integer
List of "15.2", "2015.5896", "1.0245", "500" would be double
List of "2001/01/01", "2010/05/29 12:00", "1989/12/25 10:34:21" would be datetime
它基于的 http://stackoverflow.com/questions/606365/c-doubt-finding-the-datatype/606381 #606381
object ParseString(string str)
{
Int32 intValue;
Int64 bigintValue;
double doubleValue;
bool boolValue;
DateTime dateValue;
// Place checks higher in if-else statement to give higher priority to type.
if (Int32.TryParse(str, out intValue))
return intValue;
else if (Int64.TryParse(str, out bigintValue))
return bigintValue;
else if (double.TryParse(str, out doubleValue))
return doubleValue;
else if (bool.TryParse(str, out boolValue))
return boolValue;
else if (DateTime.TryParse(str, out dateValue))
return dateValue;
else return str;
}
编辑:我只需要满足以下内容:
I only need to cater for the following:
BIT
DATETIME
INT
NVARCHAR(255)
NVARCHAR(MAX)
BIGINT
DECIMAL(36, 17)
你能看到任何可能的改进?要优先
Can you see any possible improvement to the priority?
推荐答案
我已经想出了以下解决方案,它的工作原理:
I've come up with the following solution which works:
enum dataType
{
System_Boolean = 0,
System_Int32 = 1,
System_Int64 = 2,
System_Double = 3,
System_DateTime = 4,
System_String = 5
}
private dataType ParseString(string str)
{
bool boolValue;
Int32 intValue;
Int64 bigintValue;
double doubleValue;
DateTime dateValue;
// Place checks higher in if-else statement to give higher priority to type.
if (bool.TryParse(str, out boolValue))
return dataType.System_Boolean;
else if (Int32.TryParse(str, out intValue))
return dataType.System_Int32;
else if (Int64.TryParse(str, out bigintValue))
return dataType.System_Int64;
else if (double.TryParse(str, out doubleValue))
return dataType.System_Double;
else if (DateTime.TryParse(str, out dateValue))
return dataType.System_DateTime;
else return dataType.System_String;
}
/// <summary>
/// Gets the datatype for the Datacolumn column
/// </summary>
/// <param name="column">Datacolumn to get datatype of</param>
/// <param name="dt">DataTable to get datatype from</param>
/// <param name="colSize">ref value to return size for string type</param>
/// <returns></returns>
public Type GetColumnType(DataColumn column, DataTable dt, ref int colSize)
{
Type T;
DataView dv = new DataView(dt);
//get smallest and largest values
string colName = column.ColumnName;
dv.RowFilter = "[" + colName + "] = MIN([" + colName + "])";
DataTable dtRange = dv.ToTable();
string strMinValue = dtRange.Rows[0][column.ColumnName].ToString();
int minValueLevel = (int)ParseString(strMinValue);
dv.RowFilter = "[" + colName + "] = MAX([" + colName + "])";
dtRange = dv.ToTable();
string strMaxValue = dtRange.Rows[0][column.ColumnName].ToString();
int maxValueLevel = (int)ParseString(strMaxValue);
colSize = strMaxValue.Length;
//get max typelevel of first n to 50 rows
int sampleSize = Math.Max(dt.Rows.Count, 50);
int maxLevel = Math.Max(minValueLevel, maxValueLevel);
for (int i = 0; i < sampleSize; i++)
{
maxLevel = Math.Max((int)ParseString(dt.Rows[i][column].ToString()), maxLevel);
}
string enumCheck = ((dataType)maxLevel).ToString();
T = Type.GetType(enumCheck.Replace('_', '.'));
//if typelevel = int32 check for bit only data & cast to bool
if (maxLevel == 1 && Convert.ToInt32(strMinValue) == 0 && Convert.ToInt32(strMaxValue) == 1)
{
T = Type.GetType("System.Boolean");
}
if (maxLevel != 5) colSize = -1;
return T;
}
这篇关于得到的值作为数据类型字符串传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!