【问题标题】:automatically generate invoice number in c# using stored procedure in dapper使用 dapper 中的存储过程在 C# 中自动生成发票编号
【发布时间】:2019-06-06 09:52:18
【问题描述】:

我想生成自定义交易编号,例如(INV2019-12-000001),并通过使用存储过程保存数据来生成下一个发票编号。

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 您可以使用数据库中的Sequences 来创建新号码。与标识列不同,序列是可以直接访问的独立数据库对象。您可以根据您的业务逻辑每年或每月重置一次
  • 欢迎来到stackoverflow。请花一分钟时间回答tour,尤其是如何How to Askedit 您的问题。

标签: c# sql-server-2016


【解决方案1】:

您可以使用类似于以下的 SQL 代码在数据库上创建序列对象:

CREATE SEQUENCE Test.CountBy1TestSequence  
    START WITH 1  
    INCREMENT BY 1 ;  
GO  

然后可以使用 SQL 调用:

SELECT NEXT VALUE FOR Test.CountBy1TestSequence;  

或通过 dapper 调用:

using (var conn = new SqlConnection(ConnectionString))
            {
                String sql = @"SELECT NEXT VALUE FOR Test.CountBy1TestSequence";

                var result = conn.ExecuteScalar<Int>(sql);

                // Do whatever with the result    
            }

使用序列值,您可以使用 dapper 将其传递到数据库上的存储过程,代码类似于以下内容:

 var p = new DynamicParameters();
 p.Add("@inputInvoiceNumber", $"INV{DateTime.Now.Year}-{DateTime.Now.Month}-{result}, DbType.String, ParameterDirection.Input);
 p.Add("@retVal", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue); // If there is any return

 cnn.Execute("spTestProc", p, commandType: CommandType.StoredProcedure);

其中@inputInvoiceNumber@retVal 是存储过程中的参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    相关资源
    最近更新 更多