1

我将对象分配给此 SqlParameter 列表,然后尝试执行 SqlCommand,但它引发异常,指出其中一个对象无法转换为 SqlDbType。最好在将这些对象添加到参数集合列表之前处理它们。那么,我将如何检查添加到参数列表的值是否是好的/正确的?我应该检查什么属性?

这是我的代码:

bool Submit(Progs progs, CommandType commandType, string commandText)
{   
    try
    {
        List<SqlParameter> paramCollection = new List<SqlParameter>();
        foreach(Prog p in progs)
        {
            SqlParameter spTemp = new SqlParameter { ParameterName = p.Name , Value = p.Value};
            paramCollection.Add(spTemp);
            using (SqlConnection con = GetConnection())
            {
                SqlCommand cmd = new SqlCommand { CommandType = commandType, CommandText = commandText, Connection = con };  
                con.Open();
                cmd.Parameters.AddRange(paramCollection ); // Exception is thrown from this line
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        catch(Exception exc)
        {
            return false;
        }
    }

异常表示: 不存在从对象类型 sol2.CodeBase.BL.Letter[] 到已知托管提供程序本机类型的映射。

PS:SqlParameter 有一个名为ParamaterIsSqlType的属性(是的,它是 paramAter 而不是 paramEter),它仅在运行时出现(即,当我在下一行检查 spTemp 时使用断点)并且始终设置为 false?这是什么属性,所以只在运行时出现???另外,这个“ParamaterIsSqlType”表示什么值?

4

1 回答 1

0

如果未明确设置,请执行从toSqlParameter 推断转换的操作。所以,不,框架中(还)没有可用的属性或方法。TypeSqlDbType

System.Type type = p.Value.GetType();
var isConvertible = IsConvertibleToSqlDbType(type);
if(!isConvertible){
    //call your custom ToSqlType-method
}

以下方法直接来自SqlParemeter's private void InferSqlType (object value)

public static bool IsConvertibleToSqlDbType(Type type)
{
    switch(type.FullName) {
        case "System.Int64":
        case "System.Data.SqlTypes.SqlInt64":
            //SetSqlDbType (SqlDbType.BigInt);
            return true;
        case "System.Boolean":
        case "System.Data.SqlTypes.SqlBoolean":
            //SetSqlDbType (SqlDbType.Bit);
            return true;
        case "System.String":
        case "System.Data.SqlTypes.SqlString":
            //SetSqlDbType (SqlDbType.NVarChar);
            return true;
        case "System.DateTime":
        case "System.Data.SqlTypes.SqlDateTime":
            //SetSqlDbType (SqlDbType.DateTime);
            return true;
        case "System.Decimal":
        case "System.Data.SqlTypes.SqlDecimal":
            //SetSqlDbType (SqlDbType.Decimal);
            return true;
        case "System.Double":
        case "System.Data.SqlTypes.SqlDouble":
            //SetSqlDbType (SqlDbType.Float);
            return true;
        case "System.Byte[]":
        case "System.Data.SqlTypes.SqlBinary":
            //SetSqlDbType (SqlDbType.VarBinary);
            return true;
        case "System.Byte":
        case "System.Data.SqlTypes.SqlByte":
            //SetSqlDbType (SqlDbType.TinyInt);
            return true;
        case "System.Int32":
        case "System.Data.SqlTypes.SqlInt32":
            //SetSqlDbType (SqlDbType.Int);
            return true;
        case "System.Single":
        case "System.Data.SqlTypes.Single":
            //SetSqlDbType (SqlDbType.Real);
            return true;
        case "System.Int16":
        case "System.Data.SqlTypes.SqlInt16":
            //SetSqlDbType (SqlDbType.SmallInt);
            return true;
        case "System.Guid":
        case "System.Data.SqlTypes.SqlGuid":
            //SetSqlDbType (SqlDbType.UniqueIdentifier);
            return true;
        case "System.Money":
        case "System.SmallMoney":
        case "System.Data.SqlTypes.SqlMoney":
            //SetSqlDbType (SqlDbType.Money);
            return true;
        case "System.Object":
            //SetSqlDbType (SqlDbType.Variant); 
            return true;
        default:
            return false;
    }
}
于 2012-02-25T22:36:52.313 回答