无法隐式转换类型';System.Collections.Generic.List<;System.Data.

本文关键字:System Collections List Data lt Generic 转换 类型 | 更新日期: 2023-09-27 18:00:55

对于代码:

sqlParameter = parameterSettings.GetParameters<T>(table, config);

我收到错误:

错误1无法将类型"System.Collections.Generic.List"隐式转换为"System.Data.SqlClient.SqlParameter"C:''Users''jonathan.cacameron''Documents''Visual Studio 2013''Projects''DataGenerator''DataGenerator''Methods''Query.cs 99 28 DataGenerator

GetParameters:的方法签名

public List<SqlParameter> GetParameters<T>(T table, Config config)

代码的其余部分

public int RunInsertQuery<T>(bool returnPrimary, SqlCommand cmd, Config config, 
             ParameterSettings parameterSettings, SqlParameter sqlParameter, 
                T table)
{
        // Used to create an INSERT Query
    Query query = new Query();
        // Pass item that needs to be inserted into SQL
    cmd.CommandText = query.InsertQuery<T>(table, config);
        // If the primary key needs to be returned...
    if(returnPrimary == true)
    {
        cmd.CommandText += "SELECT CAST(scope_identity() AS int)";
    }            
        // Obtain column names/data types/values into the SqlParameter object
    sqlParameter = parameterSettings.GetParameters<T>(table, config);
        // Check for null in Sql Parameter values, apply DBNull.Value, return parameters to
        // SqlCommand instance
    cmd.Parameters.AddRange(parameterSettings.NullValueCheck(sqlParameter).ToArray());
        // if returnPrimary is truen, return the primary key while executing 
        // the sql parameter
    if (returnPrimary)
    {
        return (int)cmd.ExecuteScalar();
    }
    else
        cmd.ExecuteNonQuery();
    return 1;            
}

无法隐式转换类型';System.Collections.Generic.List<;System.Data.

GetParameters方法返回一个List,因此不能分配给SqlParameter字段。此字段的类型需要为List<SqlParameter>,或者需要从GetParameters方法的返回值中选择一个值。

相关文章: