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
相关推荐
eleven_4u7 分钟前
MySQL引擎层是如何工作的
数据库·mysql
ACP广源盛1392462567310 分钟前
Type-C 扩展坞选型笔记:DP1.4a 转 HDMI2.0 桥接方案 GSV2201E @ACP评估
笔记·硬件架构·硬件工程·视频·hdmi·国产芯片
xian_wwq22 分钟前
【学习笔记-一文读懂系列】读懂GBZ-195-2026《人工智能 工业智能体参考架构》
笔记·学习
adinnet20261 小时前
企业级 NL2SQL 的重要基础:数据库 Schema 自动理解与元数据管理
数据库
IvorySQL1 小时前
PostgreSQL 日报| OpenCode 遭黑客入侵 API 密钥失窃(10 月 20 日)
数据库·postgresql·区块链
Elastic 中国社区官方博客1 小时前
控制告警如何触发、恢复和发送通知:Elastic 可观测性中的 Kibana 通用告警
大数据·数据库·安全·elasticsearch·搜索引擎·全文检索·es
GlobalInfo1 小时前
全球人工智能数据中心保险市场动态、发展趋势及项目可行性研究报告2026-2032
数据库·人工智能·ai·数据中心
星马梦缘2 小时前
关于人工智能学院游戏设计大赛决赛的内部分工通知
数据库·人工智能·游戏
Databend2 小时前
以云原生湖仓支撑 AI 应用落地丨Databend 亮相 2026 人工智能开放计算大会
大数据·数据库·数据分析