Skip to content

System.InvalidCastException : The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects. #401

Closed
@2881099

Description

@2881099

如果用 System.Data.SqlClient 执行命令时,

传入的 DbParameter 类型是 Microsoft.Data.SqlClient.SqlParameter 类型就会报这个错误。

例如在使用 System.Data.SqlClient 的实现中使用如下代码:

fsql.Ado.Query<dynamic>(
    System.Data.CommandType.Text, 
    "select * from xxx where Id = @Id",
    new Microsoft.Data.SqlClient.SqlParameter("Id", 1)
);

另外,可以不使用具体的参数化类型传递,如下:

fsql.Ado.Query<dynamic>(
    "select * from xxx where Id = @Id",
    new { id = 1 }
);

// 或者

fsql.Ado.Query<dynamic>(
    "select * from xxx where Id = @Id",
    new Dictionary<string, object> { ["id"] = 1 }
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions