解决办法:Call stored procedure using ExecuteSqlCommand (expects parameters which was not supplied)
not 解决办法 Using call was parameters which procedure
2023-09-14 09:10:49 时间
问题描述
在做.NET Core demo的时候使用 ExecuteSqlCommand
调用执行是出现如下错误
Microsoft.Data.SqlClient.SqlException (0x80131904): The parameterized query ‘(xxx’ expects the parameter ‘@xxx’, which was not supplied.
原因分析
之前在使用EF的时候完全没有问题,后改用调用sp后出现问题,又排查了sql语句以及参数都没有发现错误,后来在Postman发现测试数据这个值是null值,后赋值给这个字段再次跑postman发现可以跑过。分析是因为直接赋值null给sp的过程中出错,但是该字段并不是必填项,可以有空的时候,后调查使用 DBNull.Value
来解决
解决办法
在SqlParameter赋值的时候使用 DBNull.Value
来做一次判断:
SqlParameter paraXxxx = new SqlParameter("@Xxxx", regRequest.Xxxx ?? DBNull.Value);
按照上述代码修改后发现有编译错误:
CS0019 Operator ‘??’ cannot be applied to operands of type ‘string’ and ‘DBNull’
问题原因:
字符串和 DBNull
之间不能自动进行转换
解决办法
可以有如下的代码进行转换:
SqlParameter paraXxxx = new SqlParameter("@Xxxx", (object)regRequest.Xxxx ?? DBNull.Value);
或
SqlParameter paraXxxx = new SqlParameter("@Xxxx", regRequest.Xxxx ?? Convert.DBNull);
或者
SqlParameter paraXxxx = new SqlParameter("@Xxxx", regRequest.Xxxx ?? (object)DBNull.Value);
至此问题已解决
相关文章
- linux git 报错提示 fatal: 'origin' does not appear to be a git repository 解决办法
- Request method 'POST' not supported解决办法
- java.lang.RuntimeException: com.netflix.client.ClientException: Load balancer does not have available server for client: service-one
- Working copy not locked; this is probably a bug, please report
- SQLExecption:Operation not allowed after ResultSet closed解决办法
- Activity not started, its current task has been brought to the front的解决办法
- Unexpected error: UnicodeDecodeError: 'ascii' codec can't decode byte 0xd2 in position 69: ordinal not in range(128)-解决办法
- tesseract is not installed or it's not in your PATH
- Centos下安装apahce的configure: error: APR not found. Please read the documentation解决办法
- 'not all arguments converted during string formatting'错误告警信息解决办法
- Unexpected error: UnicodeDecodeError: 'ascii' codec can't decode byte 0xd2 in position 69: ordinal not in range(128)-解决办法
- Python报错:IndentationError: unindent does not match any outer indentation level解决办法
- SAP UI5应用在SAP WebIDE里clone之后出现404 not found error for sap-ui-core.js错误的原因分析
- IDEA在编辑时提示could not autowire
- 如何处理Fiori错误消息 - User Parameter is not maintained
- not executable: 64-bit ELF file 已解决
- 成功解决 pypmml.base.PmmlError: (‘PmmlException‘, ‘Not a valid PMML‘)
- 成功解决404 Not Found Not Found The requested URL was not found on the server. If yo
- Py之GUI之PyQt:成功解决Exception “unhandled TypeError“ expected str, bytes or os.PathLike object, not tuple
- adb remount 失败:remount failed: Operation not permitted
- 完美解决Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource的问题
- 执行git命令报错:fatal: not a git repository (or any of the parent directories): .git
- [MySQL] 解决办法:mysqld: File ‘.binlog.index‘ not found (OS errno 13 - Permission denied)
- 解决办法:Linux mysql命令 -bash: mysql: command not found 找不到命令
- 解决办法:The name 'Response' does not exist in the current context
- [Maven in action] 解决办法:Could not get the value for parameter encoding for plugin execution...
- [Maven in action] 解决办法:Project configuration is not up-to-date with pom.xml. Run project ...
- consumer zookeeper is not a recognized option
- 成功解决解决VM软件安装Linux的Ubuntu过程,打开时 Operating System not found
- Public Key Retrieval is not allowed(不允许公钥检索)【解决办法】
- [Git & GitHub] 解决办法:Could not read from remote repository.Please make sure you have the...