在(sql.mm)中添加多次在sqlserver中的数据

问题描述:


在gridview中调用它
如何在列中添加所有时间,我有4列计算不同的总时间。
例如:
时间1 = 256.20(HH.mm)
时间2 = 257.41(HH.mm)
时间3 = 114.50(HH.mm)
时间4 = 371.20(HH.mm)
total = 1000.11(HH.mm)
在(sql.mm)中添加多次在sqlserver中的数据

总数会持续增加,但格式必须是小时和分钟。
我加入2-时间的时间从1以上,使用字符串分割
在C#的.NET做

多多关照。

非常感谢你@CodingYoshi

>  //string time1 = TextBox1.Text; 
     //string time2 = TextBox2.Text; 

     //string[] part1 = time1.Split('.'); 
     //int h1 = int.Parse(part1[0]); 
     //int m1 = int.Parse(part1[1]); 

     //string[] part2 = time2.Split('.'); 
     //int h2 = int.Parse(part2[0]); 
     //int m2 = int.Parse(part2[1]); 

     //int totalHours = h1 + h2; 
     //int totalMinutes = m1 + m2; 

     //if (totalMinutes >= 60) 
     //{ 
     // totalHours = totalHours + 1; 
     // totalMinutes = totalMinutes % 60; 
     //} 

     var times = new List<string> 
     { 
      TextBox1.Text,TextBox2.Text 
     }; 

     TimeSpan total = new TimeSpan(); 
     foreach (var thisString in times) 
     { 
      var split = thisString.Split('.'); 
      total = total.Add(new TimeSpan(int.Parse(split[0]), int.Parse(split[1]), 0)); 
     } 

     var totalHours = (int)total.TotalHours; 
     var totalMinutes = total.Minutes; 

     TextBox3.Text = totalHours.ToString() + "." + totalMinutes.ToString(); 

从gridview的

SqlCommand cmd = con.CreateCommand(); 
     cmd.CommandText = "select * from totalHours"; 
     SqlDataReader dr = cmd.ExecuteReader(); 
     DataTable dt = new DataTable(); 
     dt.Load(dr); 
     GridView1.DataSource = dt; 
     GridView1.DataBind(); 

    TimeSpan total = new TimeSpan(); 

    var times = new List<string>(); 

    for (int i = 0; i < GridView1.Rows.Count; i++) 
    { 
     times.Add((GridView1.Rows[i].Cells[0].Text)); 
    } 

    foreach (var thisString in times) 
    { 
     var split = thisString.Split('.'); 
     total = total.Add(new TimeSpan(int.Parse(split[0]), int.Parse(split[1]), 0)); 
    } 

    var totalHours = (int)total.TotalHours; 
    var totalMins = total.Minutes; 

    Label2.Text = totalHours.ToString() + "." + totalMins.ToString(); 

感谢您再次@CodingYoshi

+0

是它的持续时间? 256.20不是有效的时间值?你怎么能显示2位数的1000? –

+0

就是几个小时。 256小时20分钟,257小时41分钟,114小时50分钟,371小时20分钟。总计 - 1000小时11分钟 –

这里我加入他们的2,你可以做其他:

var t1 = new TimeSpan(256, 20, 0); 
var t2 = new TimeSpan(257, 41, 0); 
var tTotal = t1.Add(t2).ToString(@"dd\.hh\:mm\:ss"); 

tTotal将显示添加t1t2的天数,小时数,分钟数和秒数;


如果你想使用一个循环做这些事,你能做到这样的:

var times = new List<string> 
{ 
    "256.20", 
    "257.41", 
    "114.50", 
    "371.20", 
    "1000.11" 
}; 

TimeSpan total = new TimeSpan(); 
foreach (var thisString in times) 
{ 
    var split = thisString.Split('.'); 
    total = total.Add(new TimeSpan(int.Parse(split[0]), int.Parse(split[1]), 0)); 
} 
var totalDisplay = total.ToString(@"dd\.hh\:mm\:ss"); 

如果你想总的小时和分钟:

var totalHours = (int)total.TotalHours; 
var totalMins = total.Minutes; 
+0

我想添加小时和分钟。有没有办法做到这一点 –

+0

@DeepShah是的。请参阅编辑。 – CodingYoshi

+0

for var totalDisplay = total.ToString(@“dd \ .hh \:mm \:ss”);
我可以使用var totalDisplay = total.ToString(@“hh \ .mm”); –