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
相关推荐
笨小孩@GF 知行合一3 小时前
易语言-高级应用
数据库·编程·易语言·中文编程
KaiwuDB3 小时前
KaiwuDB 运维实战04:DRBD + KaiwuDB——物联网场景下的低成本数据库高可用方案
运维·数据库·物联网·时序数据库·kaiwudb·aiot·多模数据库
波力海苔夹心脆6755 小时前
C# 值类型与引用类型详解:存储位置、赋值机制、参数传递、相等比较、装箱拆箱与常见陷阱
开发语言·jvm·经验分享·笔记·c#·.net
夜雪一千5 小时前
MySQL外键彻底入门:是什么、怎么用、什么时候不该用
数据库·mysql
qq_334466865 小时前
删除SSMS的历史登录记录
数据库·sql
估值探索者5 小时前
【Python量化策略实战 #02】多因子合成mom和vol两因子打分合成
开发语言·c++·python·数据挖掘·c#
zl_dfq5 小时前
Redis 之 【常见面试题总结】(从数据类型到高可用、缓存一致性全解析)
数据库·redis
龙亘川6 小时前
铭记英烈守初心,数字强基促服务:亘川智城退役军人服务系统的实践与价值
java·数据库·人工智能
kv1106 小时前
Android studio国内开发有关
java·数据库·android studio