我通过type t = obj.GetType()取得了一个数据的类型
请问能将t直接转换成SqlDbType

解决方案 »

  1.   

    根本没必要做这种转换,Command.Parameters.Add( )or AddWithValue()可以自动转换的。
      

  2.   

    DataTable dt=new DataTable();
    for (int k=0; k<dt.Columns.Count; ++k)
    {
    System.Data.IDataParameter iparam=new  SqlParameter();
    iparam.ParameterName    = "@"+ dt.Columns[k].ColumnName;
    iparam.DbType            = GetDbType(dt.Columns[k].DataType);
    iparam.Value            = dt.Row[k];
    sqlcom.Parameters.Add(iparam);
    }private static System.Data.DbType GetDbType(Type type)
    {
    DbType result = DbType.String;
    if( type.Equals(typeof(int)) ||  type.IsEnum)
    result = DbType.Int32;
    else if( type.Equals(typeof(long)))
    result = DbType.Int32;
    else if( type.Equals(typeof(double)) || type.Equals( typeof(Double)))
    result = DbType.Decimal;
    else if( type.Equals(typeof(DateTime)))
    result = DbType.DateTime;
    else if( type.Equals(typeof(bool)))
    result = DbType.Boolean;
    else if( type.Equals(typeof(string) ) )
    result = DbType.String;
    else if( type.Equals(typeof(decimal)))
    result = DbType.Decimal;
    else if( type.Equals(typeof(byte[])))
    result = DbType.Binary;
    else if( type.Equals(typeof(Guid)))
    result = DbType.Guid;
            return result;
    }
    参考
    http://singlepine.cnblogs.com/articles/255374.html