执行SqlCommand.Prepare出错;

SqlCommand.Prepare()方法。在IDbCommand接口上就定义了Prepare()方法,这个方法可以把CommandType为Text的SQL语句提前在数据库中编译为一个临时的StoredProcedure然后再执行,这样对于需要多次执行的DbCommand来说,可以提高一定的执行效率:)注意:请在指定了Command的Connection之后再调用Prepare()方法。

Creates a prepared version of the command on an instance of SQL Server.

但是今天执行遇到这样一个问题;
SqlCommand.Prepare method requires all parameters to have an explicitly set type.

说我的Parameter没有显式指定类型;我的SqlParamter的用法是:
SqlParameter param = new SqlParameter(paramname, paramvalue);
直接指定parametername和value
看来这样是不行,需要为这个参数指定类型和大小;


If CommandType is set to StoredProcedure, the call to Prepare should succeed, although it may cause a no-op.

假如CommandType 指定为存储过程StoredProcedure,执行也会成功,但是和不Prepare没有差别;(也就说Prepare为空操作)

Before you call Prepare, specify the data type of each parameter in the statement to be prepared. For each parameter that has a variable length data type, you must set the Size property to the maximum size needed. Prepare returns an error if these conditions are not met.

执行Prepare之前,为每个参数指定参数类型,指定大小长度,否则会返回错误;

If you call an Execute method after calling Prepare, any parameter value that is larger than the value specified by the Size property is automatically truncated to the original specified size of the parameter, and no truncation errors are returned.

Prepare之后执行Execute方法时,如果有参数值长度大于预先指定的大小,将会自动截尾,而且没有任何错误返回;

Output parameters (whether prepared or not) must have a user-specified data type. If you specify a variable length data type, you must also specify the maximum Size.

无论是否使用Prepare,返回参数(output)必须指定参数类型;

转载于:https://www.cnblogs.com/upzone/archive/2006/05/19/404117.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
fail: 2023/7/14 14:31:33.417 CoreEventId.QueryIterationFailed[10100] (Microsoft.EntityFrameworkCore.Query) An exception occurred while iterating over the results of a query for context type 'iMES.Core.EFDbContext.SysDbContext'. System.InvalidOperationException: There is already an open DataReader associated with this Connection which must be closed first. at Microsoft.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command) at Microsoft.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command) at Microsoft.Data.SqlClient.SqlCommand.ValidateCommand(Boolean isAsync, String method) at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String method) at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at Microsoft.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior) at Microsoft.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject) at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.InitializeReader(Enumerator enumerator) at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.<>c.<MoveNext>b__21_0(DbContext _, Enumerator enumerator) at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded) at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
最新发布
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值