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
相关推荐
我是唐青枫1 小时前
C#.NET PInvoke 深入解析:封送、内存布局与原生互操作实战
开发语言·c#·.net
Am-Chestnuts1 小时前
AI 生成的数学公式,怎么用 DS随心转 导出成 Word 里能改的公式
人工智能·c#·word
geovindu2 小时前
CSharp: Strategy Pattern
开发语言·后端·c#·.net·.netcore·策略模式·行为模式
古少侠2 小时前
AI 内容粘到 Word 就乱?用 DS随心转 把复制的内容直接变成排版好的一段
人工智能·c#·word
DBA_G2 小时前
GBase 8a数据库表空间多路径功能特点综述
数据库·oracle
TDengine (老段)2 小时前
TDgpt 使用 — 部署、SQL、算法
大数据·数据库·sql·算法·时序数据库·tdengine·涛思数据
geovindu2 小时前
CSharp: Task Scheduler
开发语言·后端·c#·.net·.netcore
晴天¥2 小时前
Oracle 19c数据库内存管理优化(内存管理方式调整及内存扩容)
数据库·oracle·oracle数据库集群调优
半亩码田2 小时前
C#转Python第4.5篇:单元测试:pytest vs xUnit/NUnit
python·单元测试·c#