C#调用Sql Server存储过程,有返回值的

目录

调用存储过程

C#配置文件

数据库存储过程


调用存储过程

cs 复制代码
 //调用存储过程生成Excel
                string inforStr="";
                conString = ConfigurationManager.ConnectionStrings["Con"].ConnectionString.ToString();
                SqlConnection conn2 = new SqlConnection(conString);
                conn2.Close();
                conn2.Open();

                //创建指令对象
                SqlCommand sqlCommand2 = new SqlCommand("ConvertAccountingDocumentToExcel", conn2);
                sqlCommand2.CommandType = CommandType.StoredProcedure;   //存储过程,一定要写这一句,否则无法当做存储过程来执行
                sqlCommand2.Parameters.Add("@gjahr", SqlDbType.Char, 4).Value = this.year;
                sqlCommand2.Parameters.Add("@monat", SqlDbType.Char, 2).Value = this.month.PadLeft(2,'0');
                sqlCommand2.Parameters.Add("@infor", SqlDbType.NVarChar,1000).Direction = ParameterDirection.Output; //返回参数
                sqlCommand2.ExecuteNonQuery();
                inforStr = sqlCommand2.Parameters["@infor"].Value.ToString();  //获取返回值
                conn2.Close();
                if (inforStr != "")
                {
                    MessageBox.Show(inforStr);
                }

C#配置文件

数据库存储过程

sql 复制代码
USE [HS_Label_System]
GO
/****** Object:  StoredProcedure [dbo].[ConvertAccountingDocumentToExcel]    Script Date: 02/07/2025 09:05:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		LiuHongyu
-- Create date: 2024-02-06
-- Description:	转换会计凭证,导出Excel文件
-- =============================================
ALTER PROCEDURE [dbo].[ConvertAccountingDocumentToExcel]
	-- Add the parameters for the stored procedure here
	@gjahr as char(4),
	@monat as char(2),
	@infor as nvarchar(max) output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	declare @sao int,
			@xpcmd as int
	SELECT @sao=convert(int,value) FROM sys.configurations where name='show advanced options' ORDER BY name
	SELECT @xpcmd=convert(int,value) FROM sys.configurations where name = 'xp_cmdshell' ORDER BY name
	if @sao = 0
	begin
		EXEC sp_configure 'show advanced options', 1
		RECONFIGURE
	end
	
	if @xpcmd = 0
	begin
		EXEC sp_configure 'xp_cmdshell', 1
		RECONFIGURE
	end
	
	SELECT * INTO ##Temp FROM AccountingDocument WHERE GJAHR = @gjahr AND MONAT = @monat
	SELECT * FROM ##Temp
	DECLARE @ReturnValue INT;
	EXEC @ReturnValue = master..xp_cmdshell 'bcp "Select Top 1 N''公司编码'' as BUKRS,N''公司名称'' as BUTXT,N''会计年度'' as GJAHR,N''凭证编号'' as BELNR,N''凭证项目'' as BUZEI,N''记账期间'' as MONAT,N''凭证日期'' as BLDAT,N''过账日期'' as BUDAT,N''凭证类型'' as BLART,N''冲销凭证号'' as STBLG,N''摘要'' as SGTXT,N''过账码'' as BSCHL,N''科目编码'' as HKONT,N''科目名称'' as TXT20,N''对方科目编码'' as GKONT,N''对方科目名称'' as GKONT_1,N''业务伙伴编码'' as BANUM,N''业务伙伴名称'' as BADEC,N''资产编码'' as ANLN1,N''资产名称'' as TXT50,N''成本中心编码'' as KOSTL,N''成本中心名称'' as KTEXT,N''订单'' as AUFNR,N''内部订单名称'' as AUFTX,N''币别'' as WAERS,N''汇率'' as KURSF,N''方向'' as SHKZG,N''借方金额(原币)'' as SWRBTR,N''贷方金额(原币)'' as HWRBTR,N''借方金额(本币)'' as SDMBTR,N''贷方金额(本币)'' as HDMBTR,N''功能范围'' as RFAREA FROM HS_Label_System.dbo.AccountingDocument Union all SELECT BUKRS,BUTXT,GJAHR,BELNR,BUZEI,MONAT,CONVERT(varchar,BLDAT,120),CONVERT(varchar,BUDAT,120),BLART,STBLG,SGTXT,BSCHL,HKONT,TXT20,GKONT,GKONT_1,BANUM,BADEC,ANLN1,TXT50,KOSTL,KTEXT,AUFNR,AUFTX,WAERS,CONVERT(VARCHAR(20),KURSF),SHKZG,CONVERT(VARCHAR(30),SWRBTR),CONVERT(VARCHAR(30),HWRBTR),CONVERT(VARCHAR(30),SDMBTR),CONVERT(VARCHAR(30),HDMBTR),RFAREA FROM tempdb.dbo.##Temp" queryout "D:\Projects\Visual_Studio_2022\WindowsFormsApp1_SAP_RFC\AccountingDocument.csv" -w -S"localhost" -U"sa" -P"GDhs3225966"'
	
	if @ReturnValue = 0
	begin
		set @infor = '创建Excel执行成功'
	end
	else
	begin
		set @Infor='创建EXCEL执行失败'
	end
	
	EXEC sp_configure 'xp_cmdshell', 0
	RECONFIGURE
	EXEC sp_configure 'show advanced options', 0
	RECONFIGURE
	--删除临时表
	DROP TABLE ##Temp
	
    -- Insert statements for procedure here
	
END
相关推荐
草莓熊Lotso1 小时前
【C++ 基于微服务的即时通讯系统】文件存储子服务实战:UUID生成、文件IO封装与全接口单元测试
开发语言·网络·数据库·c++·微服务·架构·单元测试
进击的荆棘2 小时前
MySQL——库的操作
数据库·mysql
huaqianzkh2 小时前
WinForm C#:循环修改控件UI闪烁卡顿?SuspendLayout / ResumeLayout 与 BeginUpdate 优化
开发语言·ui·c#
2601_967980112 小时前
“小程序·云开发”上线 助力开发者的高效利器
数据库·存储管理·小程序云开发·云函数·开发者工具
zhangjw342 小时前
1-9 SELECT 查询基础:where、group by、having、order by、limit offset
数据库·postgresql·pg数据库
唐老板2 小时前
Postgres 慢查询,先取证再动手
数据库·ai编程
笃行3509 小时前
数据库性能优化的五处内核改造,KingbaseES 配了四件诊断工具
数据库
非优秀程序员11 小时前
第02章 杭电的英语老师与一台服务器|《互联网产品恩仇录》
数据库·node.js·代码规范
SelectDB16 小时前
SelectDB 是哪个公司的?是国产数据库吗?一篇讲清飞轮科技与 Apache Doris 的关系
数据库
这个DBA有点耶16 小时前
库内计算推理:一条SQL背后的算子融合,和外挂方案差了几次网络往返
数据库·人工智能·sql