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
相关推荐
底层玩家老张43 分钟前
绿盟和金仓共建七个月的复盘:适配记录、两个场景、交付与人
数据库·嵌入式数据库·日志检索·产品适配·主备读写分离
Mojitocean1 小时前
主流数据库对比与业务场景选型指南
数据库
武子康2 小时前
GameEval第一轮先测什么:正常对照、已知故障与未知探索
大数据·数据库·人工智能
代码不停2 小时前
Linux基础知识
java·linux·数据库
stormsha5 小时前
MySQL 连不上:2003 / 1045 / 1040 三张面孔与一个排查顺序
java·数据库·mysql
撸码猿5 小时前
老系统只读出口:MySQL 秒级时间戳当水位,3,375 次变更漏 817 行
数据库·mysql·数据一致性·数据同步·增量更新
衡石科技6 小时前
SQL 由模型写出来之后:行级安全在问数链路上的四个注入点
数据库·sql·安全·数据分析
真上帝的左手7 小时前
10. 软件设计&架构-架构师之路-102.MySQL秒级扩容
数据库·mysql·架构·系统架构·架构设计·高可扩展性
Decay__7 小时前
PostgreSQL 优化案例集 —— 慢 SQL 优化、索引重构与参数调整收益对比
运维·数据库·sql·postgresql
loopfde成序8 小时前
Node.js + SQLite 多租户交付台账工程实践:WAL 并发治理、VACUUM INTO 快照与乐观锁防覆盖
数据库·sqlite·node.js