对于每次迭代,如何写入循环增加的持续时间? [英] How to write for loop increase from hour by duration for every iteration ?

查看:105
本文介绍了对于每次迭代,如何写入循环增加的持续时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题



如何为每次迭代的持续时间从小时开始增加循环次数?



我在windows表单应用程序上工作csharp vs2015



i有三个文本框



txt_fromhour 6



txt_tohour 12



txt_Duration 1.5



txt_countduration 4(计算为12-6 = 6 / 1.5 = 4)



我有桌面培训有3个字段



serial int



fromhour decimal



tohour decimal



我需要在三个字段上写下循环插入数据作为下面的示例



当我从6点开始写小时12,持续时间为1.5,数据将插入如下



序列fromhour 

1 6 7 5

2 7 5 9

3 9 10 5

4 10 5 12





我的尝试:



  for  int  i =  0 ; i< =  4 ; i ++)

{

插入培训(序列号) ,fromhour,tohour)值(最大+ 1,??,??)

}

解决案
你需要使用一个可变的环的外侧。请参阅:



  //  将其用于数字格式化 
System.Globalization.CultureInfo cu = new System.Globalization.CultureInfo( en-us);
int fromhour = 6 ;
int tohour = 12 ;
float duration = 1 .5F;

int count =( int )((tohour - fromhour)/ duration );
// 这里!
float nextfrom = fromhour;
for int i = 1 ; i< = count; i ++)
{
Console.WriteLine( {0 } \t-\t {1},nextfrom.ToString( 0.0,cu),(nextfrom + duration).ToString( 0.0,cu));
// 在此处插入声明
nextfrom + = duration;
}





结果:

 6.0  -  7.5 
7.5 - 9.0
9.0 - 10.5
10.5 - 12.0







我忘了提到你必须使用日期时间数据类型而不是数字来避免将来出现问题。

 DateTime counter = DateTime.Today.AddHours(fromhour); 
for int i = 1 ; i< = count; i ++)
{
Console.WriteLine( {0 } \t-\t {1},counter.ToString( HH:mm ,cu),counter.AddHours(duration).ToString( HH:mm ,cu));
// 在此处插入声明
counter = counter.AddHours(duration);
}



结果:

 06:00  -  07:30 
07 :30 - 09:00
09:00 - 10:30
10:30 - 12:00


首先转换yoru三个文本框值到数字: Decimal.TryParse方法(系统)| Microsoft Docs [ ^ ]将执行此操作,并向用户报告错误而不是继续。



然后创建并打开数据库连接:

 使用(SqlConnection con =  new  SqlConnection( strConnect))
{
con.Open();
...
}

在打开的连接中,设置一个循环:,而将执行,停止时 fromHour 大于 toHour



循环,创建一个 SQLCommand 对象(如上所述使用使用块)并将两次传递给DB参数化查询。如有必要,每次都传递 serial 值(尽管我在数据库中将其设置为IDENTITY字段,因此SQL处理所有这些)增加 fromHour 持续时间



轻松做 - 只需试一试手动,您将看到我的意思。


将您的持续时间存储为变量。将计时器设置为每小时关闭并增加它。如果您希望从给定时间开始永久增加,请使用DateTime.Now在属性上计算它。


Problem

How to write for loop increase from hour by Duration for every iteration ?

I work on windows form application csharp vs2015

i have three textboxes

txt_fromhour 6

txt_tohour 12

txt_Duration 1.5

txt_countduration 4 (calculated as 12-6=6/1.5=4)

I have table name training have 3 fields

serial int

fromhour decimal

tohour decimal

I need to write for loop insert data on three fields as sample below

when i write fromhour 6 and tohour 12 and duration is 1.5 data will inserted as below

serial    fromhour            tohour

1            6                7.5

2            7.5               9

3            9                10.5

4            10.5              12



What I have tried:

for(int i=0;i<=4;i++)

{

insert into training(serial,fromhour,tohour) values(max+1,??,??)

}

解决方案

You need to use a variable outside a loop. See:

//use it for number formatting
System.Globalization.CultureInfo cu = new System.Globalization.CultureInfo("en-us");
int fromhour = 6;
int tohour = 12;
float duration = 1.5F;

int count = (int)((tohour - fromhour) / duration);
//here!
float nextfrom = fromhour;
for(int i=1; i<=count; i++)
{
	Console.WriteLine("{0} \t-\t {1}", nextfrom.ToString("0.0", cu), (nextfrom+duration).ToString("0.0", cu));
	//insert statement here
	nextfrom += duration; 
}



Result:

6.0   -   7.5
7.5   -   9.0
9.0   -   10.5
10.5   -   12.0



[EDIT]
I forgot to mention that you have to work with datetime data type instead of numbers to avoid issues in a future.

DateTime counter = DateTime.Today.AddHours(fromhour);
for(int i=1; i<=count; i++)
{
	Console.WriteLine("{0} \t-\t {1}", counter.ToString("HH:mm", cu), counter.AddHours(duration).ToString("HH:mm", cu));
	//insert statement here
	counter = counter.AddHours(duration); 
}


Result:

06:00   -   07:30
07:30   -   09:00
09:00   -   10:30
10:30   -   12:00


Start by converting yoru three textbox values to numbers: Decimal.TryParse Method (System) | Microsoft Docs[^] will do it, and report errors to the user instead of continuing.

Then create and open your DB connection:

            using (SqlConnection con = new SqlConnection(strConnect))
                {
                con.Open();
...
                }

Inside the open connection, set up a loop: while will do, stopping when the fromHour is greater than the toHour.

Inside the loop, create an an SQLCommand object (with a using block as above) and pass the two times to the DB as a parameterized query. If necessary, pass the serial value each time (though I'd make it an IDENTITY field in the DB, so SQL handles all that) Increment the fromHour by the duration.

Easy to do - just give it a try manually and you will see what I mean.


Store your duration as a variable. Set a timer to go off every hour and increment it. If you want a permanent increase from a given time, use DateTime.Now to calculate it on a property.


这篇关于对于每次迭代,如何写入循环增加的持续时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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