-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
139 lines (127 loc) · 4.93 KB
/
Program.cs
File metadata and controls
139 lines (127 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.IO;
using OfficeOpenXml;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using TagReplacer.Context;
class Program
{
static void Main(string[] args)
{
// Check database connection
if (TestDatabaseConnection())
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("The connection string is correct and the connection with the database was established");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("A problem has occurred, the connection with the database has not been established");
Console.ResetColor();
return;
}
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("Press Enter to continue ...");
Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
string filePath = "products.xlsx";
// Check if the Excel file exists
if (!File.Exists(filePath))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Excel file NOT FOUND");
Console.WriteLine("Place the 'products.xlsx' file right next to the 'program.cs' file");
Console.ResetColor();
return;
}
FileInfo fileInfo = new FileInfo(filePath);
using (ExcelPackage package = new ExcelPackage(fileInfo))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
int rowCount = worksheet.Dimension.Rows;
int batchSize = 300;
int totalBatches = (rowCount - 1) / batchSize + 1;
using (var context = new ProductContext())
{
for (int batch = 0; batch < totalBatches; batch++)
{
int startRow = batch * batchSize + 2;
int endRow = Math.Min(startRow + batchSize - 1, rowCount);
for (int row = startRow; row <= endRow; row++)
{
long productId;
string keyWords;
try
{
productId = Convert.ToInt64(worksheet.Cells[row, 1].Value);
keyWords = worksheet.Cells[row, 2].Value.ToString();
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Error reading data from Excel for row {row}: {ex.Message}");
Console.ResetColor();
continue;
}
try
{
var product = context.Products.SingleOrDefault(p => p.Id == productId);
if (product != null)
{
product.KeyWords = keyWords;
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Product with Id {productId} not found in database.");
Console.ResetColor();
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Error updating product with Id {productId}: {ex.Message}");
Console.ResetColor();
}
}
try
{
context.SaveChanges();
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Error saving changes to the database: {ex.Message}");
Console.ResetColor();
}
if (batch < totalBatches - 1)
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("Press Enter to process the next batch...");
Console.ResetColor();
Console.ReadLine();
}
}
}
}
Console.WriteLine("Products updated successfully.");
}
static bool TestDatabaseConnection()
{
try
{
using (var context = new ProductContext())
{
context.Database.OpenConnection();
context.Database.CloseConnection();
}
return true;
}
catch
{
return false;
}
}
}