如何在C#中使用开始日期和结束日期填写一系列日期 [英] How to fill a series of date using start date and end date in C#

查看:198
本文介绍了如何在C#中使用开始日期和结束日期填写一系列日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功创建开始日期和结束日期,但我需要在c#中插入第一个日期到最后一个日期。



开始日期:01/02 / 2018

02/02/2018











结束日期:22/05/2018。





我还需要另外一个代码。



当我插入一个值= 1000时。

它会插入像

01/02/201 = 100。

02/02/201 = 100。

03/02/2020 = 100。













10/02/2018 = 100。



请给我#c代码谢谢你的朋友。



我尝试过:



i不知道如何上传我的截图

i have create start date and end date successfully,but i need to insert first date to last date in c#.example,

start date:01/02/2018
02/02/2018
.
.
.
.
.
end date:22/05/2018.


and i need another code also.

when i insert a value =1000.
it will insert like
01/02/2018=100.
02/02/2018=100.
03/02/2018=100.
.
.
.
.
.
.
10/02/2018=100.

please give me a c# code thank u friends.

What I have tried:

i dont know how to upload my screenshot

推荐答案

实现这一目标的最佳方法是创建商店d程序 [ ^ ]。请参阅SQL代码示例。



The best way to achieve that is to create stored procedure[^] on SQL server level. See an SQL code sample.

USE YourDateBaseName;

CREATE PROCEDURE uspInsertDateRange
--input variables
    @startDate DATE,
    @endDate DATE,
    @myVal INT
AS
    SET NOCOUNT ON;

    --start recursive query
    ;WITH CTE AS 
    (
        --initial values
	SELECT @startDate AS myDate, @myVal AS myValue
	WHERE @startDate <= @endDate 
        -recursion part
	UNION ALL
	SELECT DATEADD(DD, 1, myDate) AS myDate, @myVal AS myValue
	FROM CTE
	WHERE DATEADD(DD, 1, myDate) <= @endDate 
    )
    --insert statement
    INSERT INTO YourTableName (myDate, myValue )
    SELECT myDate, myValue 
    FROM CTE
    OPTION (MAXRECURSION 0)

GO;





如何从代码中调用它?请检查:如何:设置和获取命令对象的参数 [ ^ ]





如果您想使用UI来实现这一点,您需要创建 DataTable [ ^ ]和 SqlBulkCopy类(System.Data.SqlClient) [ ^ ]



试试!



How to call it from code? Check this: How to: Set and Get Parameters for Command Objects[^]


In case you want to use a UI to achieve that, you'll need to create DataTable[^] and SqlBulkCopy Class (System.Data.SqlClient)[^]

Try!


因此,出于某种原因,您需要一个从开始日期到结束日期的日期列表吃了差不多一天?

只需使用DateTime的 AddDays 方法。



当我插入一个值= 1000时,我不明白你的意思 - 请更好地描述你的情景(整个问题不是很明确 - 更好地告诉你想要实现的目标)
So for some reason you want a list of dates from a start-date to an end-date with a difference of one day?
Just use DateTime's AddDays method.

I don't get what you could mean with "when i insert a value =1000" - Please describe your scenario better (the whole question is not very clear - Better tell what you want to achieve)

您似乎正在尝试根据最低付款金额计算付款计划。



简单地将贷款金额除以应付的最低金额(在这种情况下,它是10)。这将确定需要支付多少付款。当然,您的代码还应该处理支付金额不能平均分配到贷款金额的可能性。在确定需要支付多少付款后,使用 startDate.AddMonths(numberOfPayments)确定上次付款的日期是一件简单的事情。如果您需要实际日期(恕我直言,但程序员的家庭作业通常充满了荒谬的要求),那么从开始日期开始迭代以获得每个后续付款日期。



如果您不理解作业,请向教师澄清(或在教师指导时尽量保持清醒)。这并不难......
It looks like you're trying to calculate a payment schedule based on a minimum payment value.

Simply divide the loan amount by the minimum amount due (in this case, it's 10). This will establish how many payments need to be made. Of course, your code should also handle the possibility that the payment amount is not evenly divisible into the loan amount. After you've established how many payments need to be made, it's a simple matter to establish the date of the last payment using startDate.AddMonths(numberOfPayments). If you need the actual dates (which IMHO is preposterous, but programmer homework is often rife with preposterous requirements), then iterate the months from the start date to get each subsequent payment date)

If you don't understand the assignment, ask your instructor for clarification (or try to stay awake in class while your instructor is instructing). It ain't hard...


这篇关于如何在C#中使用开始日期和结束日期填写一系列日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆