执行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)必须指定参数类型;

posted @ 2006-05-19 10:47  upzone  阅读(812)  评论(0编辑  收藏  举报