C#调用Sql Server存储过程,并且使用传入表结构

目录

定义DataTable

调用存储过程

C#配置文件

数据库存储过程


定义DataTable

private System.Data.DataTable dt = new System.Data.DataTable();

调用存储过程

cs 复制代码
 //存储数据库

conString = ConfigurationManager.ConnectionStrings["Con"].ConnectionString.ToString();
SqlConnection conn = new SqlConnection(conString);
conn.Close();
conn.Open();


 //创建指令对象

SqlCommand sqlCommand = new SqlCommand("InsertAccountingDocument", conn);
sqlCommand.CommandType = CommandType.StoredProcedure;   //存储过程
sqlCommand.Parameters.Add("@AccountingDocumentTable", SqlDbType.Structured).Value = dt;
sqlCommand.Parameters.Add("@gjahr", SqlDbType.Char, 4).Value = this.year;
 sqlCommand.Parameters.Add("@monat", SqlDbType.Char, 2).Value = this.month.PadLeft(2, '0'); //指定2位,不足位数时左边补零



if (begin == 1)
{
      sqlCommand.Parameters.Add("@begin", SqlDbType.Int).Value = 1;
}

else
 {
       sqlCommand.Parameters.Add("@begin", SqlDbType.Int).Value = 0;
 }
      sqlCommand.ExecuteNonQuery();
      conn.Close();

C#配置文件

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<configuration>
	<configSections>
		<sectionGroup name="SAP.Middleware.Connector">
			<sectionGroup name="ClientSettings">
				<section name="DestinationConfiguration" type="SAP.Middleware.Connector.RfcDestinationConfiguration,sapnco" />
			</sectionGroup>
		</sectionGroup>
	</configSections>
	
	<!--连接字符串-->
	<connectionStrings>
		<add name="Con" connectionString="server=localhost;uid=sa;pwd=GDhs3225966;database=HS_Label_System;" />
	</connectionStrings>
	
	<SAP.Middleware.Connector>
		<ClientSettings>
			<DestinationConfiguration>
				<destinations>
					<!--310测试系统-->
					<!--<add NAME="S4D" USER="019246" PASSWD="GZdyg3225966" CLIENT="200" SYSNR="00" ASHOST="192.168.4.20" LANG="ZH" GROUP="PUBLIC" MAX_POOL_SIZE="10" IDLE_TIMEOUT="600"/>-->
					<add NAME="S4D" USER="024083" PASSWD="Hs87654321" CLIENT="310" SYSNR="00" ASHOST="192.168.4.20" LANG="ZH" GROUP="PUBLIC" MAX_POOL_SIZE="10" IDLE_TIMEOUT="600" />
					<!--500测试系统-->
					<add NAME="S4Q" USER="000072" PASSWD="Hs123456789" CLIENT="500" SYSNR="00" ASHOST="192.168.4.21" LANG="ZH" GROUP="PUBLIC" MAX_POOL_SIZE="10" IDLE_TIMEOUT="600" />

					<!--800正式系统-->
					<add NAME="S4P" USER="000072" PASSWD="Hs123456789" CLIENT="800" SYSNR="00" ASHOST="192.168.4.23" LANG="ZH" GROUP="PUBLIC" MAX_POOL_SIZE="10" IDLE_TIMEOUT="600" />
				</destinations>
			</DestinationConfiguration>
		</ClientSettings>
	</SAP.Middleware.Connector>

	<startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
    </startup>

	<startup useLegacyV2RuntimeActivationPolicy="true">
		<supportedRuntime version="v4.0" />
	</startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

数据库存储过程

sql 复制代码
USE [HS_Label_System]
GO
/****** Object:  StoredProcedure [dbo].[InsertAccountingDocument]    Script Date: 02/07/2025 10:02:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		LiuHongyu
-- Create date: 2025-01-23
-- Description:	批量插入会计凭证
-- =============================================
ALTER PROCEDURE [dbo].[InsertAccountingDocument] 
	-- Add the parameters for the stored procedure here
	@AccountingDocumentTable as AccountingDocument readonly,
	@gjahr as char(4),
	@monat AS CHAR(2),
	@begin AS int
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	
	IF	@begin = 1
	BEGIN
		DELETE FROM AccountingDocument WHERE GJAHR = @gjahr and MONAT = @monat;
	END
    Insert into AccountingDocument(BUKRS,BUTXT,GJAHR,BELNR,BUZEI,MONAT,BLDAT,BUDAT,BLART,STBLG,SGTXT,BSCHL,HKONT,TXT20,GKONT,GKONT_1,BANUM,BADEC,ANLN1,TXT50,KOSTL,KTEXT,AUFNR,AUFTX,WAERS,KURSF,SHKZG,SWRBTR,HWRBTR,SDMBTR,HDMBTR,RFAREA)
    select BUKRS,BUTXT,GJAHR,BELNR,BUZEI,MONAT,BLDAT,BUDAT,BLART,STBLG,SGTXT,BSCHL,HKONT,TXT20,GKONT,GKONT_1,BANUM,BADEC,ANLN1,TXT50,KOSTL,KTEXT,AUFNR,AUFTX,WAERS,KURSF,SHKZG,SWRBTR,HWRBTR,SDMBTR,HDMBTR,RFAREA
    from @AccountingDocumentTable;

END
相关推荐
Decay__38 分钟前
PostgreSQL 监控体系构建 —— postgres_exporter + Prometheus + Grafana 全栈实战
运维·数据库·sql·postgresql
Decay__2 小时前
PostgreSQL 告警与自动化运维 —— 告警分级收敛、Webhook、定时巡检、脚本库
运维·数据库·sql·postgresql
老纪的技术唠嗑局3 小时前
OceanBase × 知跃空间智能:从感知,到记忆,再到行为,打造具身智能数据底座
数据库
嘉泰姆半导体官方3 小时前
固定5V输出的非隔离AC-DC芯片选型与电路设计笔记(CXAC85369)
笔记
郝学胜-神的一滴4 小时前
Effective Python 条款45:巧用属性注解平滑升级类属性
运维·开发语言·数据库·python·网络协议·numpy
iiiiii115 小时前
从最大熵到 SAC:Soft Q-Learning 与 Soft Actor-Critic 公式推导笔记
人工智能·笔记·算法·机器学习·强化学习
千寻技术帮6 小时前
基于微信小程序的零食销售系统(源码+文档+脚本)50047
数据库·mysql·微信小程序·小程序·毕业设计·springboot·课程设计
Uio2416 小时前
Java 泛型详解:为什么要用泛型?实战+避坑(CSDN)
笔记
张人玉6 小时前
C#如何使用FreeSql连接数据库
数据库·c#·freesql
会编程的吕洞宾6 小时前
Spring Cloud Gateway 限流鉴权与灰度发布实战
java·经验分享·后端·spring