Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

从1.3.5升级到1.7.0后报错,Ado.ExecuteNonQuery(CommandType.Text, stb.ToString(), paramList.ToArray()); #395

Closed
Tony518 opened this issue Jul 31, 2020 · 10 comments

Comments

@Tony518
Copy link

Tony518 commented Jul 31, 2020

你好,
我从1.3.5升级到1.7.0后,Ado.ExecuteNonQuery(CommandType.Text, stb.ToString(), paramList.ToArray());这一句一直报错,paramList中是有值的,在1.3.5是运行正常的
报错内容:SqlParameterCollection only accepts non-null SqlParameter type objects, not DBNull objects

@2881099
Copy link
Collaborator

2881099 commented Jul 31, 2020

是不是 FreeSql.Provider.Sqlserver

1.6.0 会报错吗

1.7.0 换了 adonet 驱动包,Microsoft.Data.SqlClient

@Tony518
Copy link
Author

Tony518 commented Jul 31, 2020

是的,
1.6.0的没有试,我暂时已经先回到1.3.5了

@2881099
Copy link
Collaborator

2881099 commented Jul 31, 2020

估计是跟改 adonet 驱动的机制有关,方便的话试下1.6.0

@2881099
Copy link
Collaborator

2881099 commented Jul 31, 2020

可能很 paramList 里面的值有关,看是不是有 null,应该改成 DbNull.Value

@Tony518
Copy link
Author

Tony518 commented Jul 31, 2020

好的,谢谢,我先试下,有结果通知您

@Tony518
Copy link
Author

Tony518 commented Jul 31, 2020

我试了1.6.0的确是可以的, paramList中没有为null的

@2881099
Copy link
Collaborator

2881099 commented Jul 31, 2020

Microsoft.Data.SqlClient 这个是微软推荐的新库。必须要换的。

可以去他的 github 搜索错误描述,看有没有相关回复。

@Tony518
Copy link
Author

Tony518 commented Jul 31, 2020

好的,谢谢

@2881099
Copy link
Collaborator

2881099 commented Aug 3, 2020

如果用 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 }
);

#401

2881099 pushed a commit that referenced this issue Aug 3, 2020
2881099 pushed a commit that referenced this issue Aug 3, 2020
@2881099 2881099 closed this as completed Aug 3, 2020
@Tony518
Copy link
Author

Tony518 commented Aug 3, 2020

感谢大佬,厉害啊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants