Files
bnhtrade/src/bnhtrade.Core/Test/SQLLoop.cs
2024-04-11 12:26:13 +01:00

81 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
namespace bnhtrade.Core.Test
{
public class SQLLoop
{
public void Go(string sqlConnectionString)
{
using (TransactionScope scope = new TransactionScope())
{
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
{
UI.Console.WriteLine("Starting.....");
conn.Open();
int count = 0;
int total;
using (SqlCommand cmd = new SqlCommand(@"
SELECT COUNT(StockJournalID)
FROM tblStockJournal
", conn))
{
total = (int)cmd.ExecuteScalar();
}
var progress = new UI.ConsoleProgressBar(total, "Processing " + total + " records...");
using (SqlCommand cmd = new SqlCommand(@"
SELECT * FROM tblStockJournal
", conn))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
count = count + 1;
//progress.Report(2677);
progress.Report(count);
int journalId = reader.GetInt32(0);
DateTime entryDate = DateTime.SpecifyKind(reader.GetDateTime(3), DateTimeKind.Utc);
DateTime modifiedDate = DateTime.SpecifyKind(reader.GetDateTime(5), DateTimeKind.Utc);
DateTime postDate = entryDate;
DateTime minPostDate = DateTime.SpecifyKind(new DateTime(2015, 06, 24), DateTimeKind.Utc);
if (modifiedDate < entryDate)
{ postDate = entryDate; }
if (postDate < minPostDate)
{ postDate = minPostDate; }
using (
SqlCommand updateCmd = new SqlCommand(@"
UPDATE tblStockJournal
SET PostDate = @postDate
WHERE StockJournalID = @journalId;
", conn))
{
updateCmd.Parameters.AddWithValue("@journalId", journalId);
updateCmd.Parameters.AddWithValue("@postDate", postDate.ToUniversalTime());
updateCmd.ExecuteNonQuery();
}
}
}
}
progress.Dispose();
scope.Complete();
}
}
}
}
}