如何用Ormlite执行含输出参数的存储过程并获取execution_id
用Ormlite获取SSIS存储过程输出的execution_id
方法1:使用输出参数执行存储过程
Ormlite支持直接传递ADO.NET的SqlParameter,可以通过定义输出参数捕获execution_id:
// 定义输出参数,匹配SSIS存储过程的BigInt类型参数 var executionIdParam = new SqlParameter("@execution_id", SqlDbType.BigInt) { Direction = ParameterDirection.Output }; // 执行第一个SSIS存储过程,传入业务参数和输出参数 db.Execute( @"EXEC [catalog].[create_execution] @folder_name = @FolderName, @project_name = @ProjectName, @package_name = @PackageName, @execution_id = @execution_id OUTPUT", new { FolderName = "你的SSIS文件夹名", ProjectName = "你的SSIS项目名", PackageName = "你的SSIS包名" }, executionIdParam ); // 从输出参数中提取execution_id long executionId = (long)executionIdParam.Value; // 用获取到的ID执行第二个依赖存储过程 db.Execute( "EXEC [catalog].[start_execution] @execution_id = @ExecutionId", new { ExecutionId = executionId } );
方法2:通过结果集直接获取execution_id
如果你的SSIS存储过程执行后会将execution_id作为结果集返回,可直接用Ormlite的SingleScalar方法提取:
// 执行存储过程并返回单个结果值(execution_id) long executionId = db.SingleScalar<long>( @"EXEC [catalog].[create_execution] @folder_name = @FolderName, @project_name = @ProjectName, @package_name = @PackageName", new { FolderName = "你的SSIS文件夹名", ProjectName = "你的SSIS项目名", PackageName = "你的SSIS包名" } ); // 执行第二个依赖存储过程 db.Execute("EXEC [catalog].[start_execution] @execution_id = @ExecutionId", new { ExecutionId = executionId });
注意事项
- 确保SSIS存储过程的参数名称、类型与代码定义完全匹配,比如
@execution_id为BigInt类型,不能错配。 - 避免使用
ExecuteSql,该方法更适合无返回值的SQL执行;带输出参数或需返回结果的场景优先用Execute或SingleScalar。 - 若存储过程有其他必填参数,需一并传入保证执行逻辑完整。
内容的提问来源于stack exchange,提问作者A_0
相关产品推荐
相关产品推荐

