2

有没有办法做这样的事情:

(SqlDbType.Int).Parse(dtbDataTable.Rows[0]["Id"])
4

3 回答 3

2

发回我的解决方法:

    public static string ParseValue(SqlDbType psdtParameter, string pstrValue, string pstrDateFormat = null)
    {
        object objReturn = new object();
        if (pstrValue != "")
        {
            switch (psdtParameter.ToString())
            {
                case "BigInt":
                    objReturn = TypeDescriptor.GetConverter(typeof(Int64)).ConvertFromString(pstrValue);
                    break;
                case "Bit":
                    objReturn = TypeDescriptor.GetConverter(typeof(Boolean)).ConvertFromString(pstrValue);
                    break;
                case "NText":
                case "NVarChar":
                case "VarChar":
                case "NChar":
                case "Text":
                case "Char":
                    objReturn = TypeDescriptor.GetConverter(typeof(String)).ConvertFromString(pstrValue);
                    break;
                case "SmallDateTime":
                case "DateTime":
                    objReturn = DateTime.ParseExact(pstrValue, pstrDateFormat, CultureInfo.InvariantCulture);
                    //TypeDescriptor.GetConverter(typeof(DateTime)).ConvertFromString(pstrValue);
                    break;
                case "Money":
                case "SmallMoney":
                case "Decimal":
                    objReturn = TypeDescriptor.GetConverter(typeof(Decimal)).ConvertFromString(null, CultureInfo.InvariantCulture, pstrValue);
                    break;
                case "Float":
                    objReturn = TypeDescriptor.GetConverter(typeof(Double)).ConvertFromString(pstrValue);
                    break;
                case "Binary":
                case "VarBinary":
                case "Timestamp":
                case "Image":
                    objReturn = TypeDescriptor.GetConverter(typeof(Byte[])).ConvertFromString(pstrValue);
                    break;
                case "Int":
                    objReturn = TypeDescriptor.GetConverter(typeof(Int32)).ConvertFromString(pstrValue);
                    break;
                case "Real":
                    objReturn = TypeDescriptor.GetConverter(typeof(Single)).ConvertFromString(pstrValue);
                    break;
                case "SmallInt":
                    objReturn = TypeDescriptor.GetConverter(typeof(Int16)).ConvertFromString(pstrValue);
                    break;
                case "TinyInt":
                    objReturn = TypeDescriptor.GetConverter(typeof(Byte)).ConvertFromString(pstrValue);
                    break;
            }
            return objReturn.ToString();
        }
        else
        {
            return null;
        }
    }

谢!

于 2013-07-05T13:03:07.453 回答
0

抱歉不行。SqlDbType 是一个枚举,所以(SqlDbType.Int)实际上归结为一个整数值,而不是一个类型。我能想到的唯一方法是某种 switch 语句:

switch (SqlDbType dbType)
{
    case SqlDbType.Int:
       int value = Int32.Parse(dtbDataTable.Rows[0]["Id"]);
       //Do stuff with this value
    //repeat for other types
 }
于 2013-06-28T15:47:07.640 回答
0

我认为这很难做到,而且这不是最易读的方式。我通过扩展方法来处理这个问题,以帮助处理 TinyInt、SmallInt 和可空值。例如:

using (var dr = new SafeDataReader(cmd.ExecuteReader()) {
  while (dr.Read()) {
   int? id = dr.GetNullableIntFromSqlTinyInt(0);
   // Other stuff like that to handle type conversions
  }
}

SafeDataReader 是 CSLA 业务对象框架的一部分,但如果您愿意,您可以实现自己的 DataReader。它更加清晰易读,并将幕后的所有解析逻辑封装到扩展方法中。

于 2013-06-28T15:50:15.043 回答