-
Notifications
You must be signed in to change notification settings - Fork 0
/
DinamicUploadingDataFromFileToDatabaseTable
157 lines (115 loc) · 5.15 KB
/
DinamicUploadingDataFromFileToDatabaseTable
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Zdorov.ru.Models.Tables;
using Dapper;
using System.Data;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
namespace Zdorov.ru.Models
{
public class UploadRepository : IUploadRepository
{
private readonly IConfiguration _config;
public UploadRepository(IConfiguration config)
{
_config = config;
}
public IDbConnection Connection
{
get
{
return new SqlConnection(_config.GetConnectionString("ZdorovConnection"));
}
}
public async Task<UploadFileResult> UploadFile(string filePath)
{
var srcEncoding = Encoding.GetEncoding(1251);
int updatesCount = 0;
int insertsCount = 0;
int exceptionsCount = 0;
string tableName = string.Empty;
UploadFileResult result = new UploadFileResult();
using (StreamReader sr = new StreamReader(filePath, encoding: srcEncoding))
{
tableName = sr.ReadLine();
string columnsNames = sr.ReadLine();
columnsNames = columnsNames.Substring(1, columnsNames.Length - 2);
string[] columnsNamesSplit = columnsNames.Split(new string[] { ", " }, StringSplitOptions.None);
while (sr.Peek() >= 0)
{
try
{
string data = sr.ReadLine();
data = data.Replace("\\N", "null");
string[] dataSplit = data.Split('\t');
string query = "SELECT * FROM " + tableName + " WHERE " + columnsNamesSplit[0] + " = " + dataSplit[0];
using (IDbConnection db = Connection)
{
var resultQuery = db.Query(query).FirstOrDefault();
if (resultQuery != null)
{
//Update
string updateQuery = "UPDATE " + tableName + " SET";
for (int i = 1; i < dataSplit.Length; i++)
{
if (dataSplit[i][0] == '\'')
{
dataSplit[i] = "N" + dataSplit[i];
}
updateQuery = updateQuery + " " + columnsNamesSplit[i] + " = " + dataSplit[i] + ",";
}
//remove last ','
updateQuery = updateQuery.Remove(updateQuery.Length - 1);
updateQuery = updateQuery + " WHERE " + columnsNamesSplit[0] + " = " + dataSplit[0];
db.Execute(updateQuery);
updatesCount++;
}
else
{
//Insert
string insertQuery = "INSERT INTO " + tableName + " (";
for (int i = 0; i < columnsNamesSplit.Length; i++)
{
insertQuery = insertQuery + columnsNamesSplit[i] + ", ";
}
//remove last ', '
insertQuery = insertQuery.Remove(insertQuery.Length - 2);
insertQuery = insertQuery + ")";
insertQuery = insertQuery + " VALUES (";
for (int i = 0; i < dataSplit.Length; i++)
{
if (dataSplit[i][0] == '\'')
{
dataSplit[i] = "N" + dataSplit[i];
}
insertQuery = insertQuery + dataSplit[i] + ", ";
}
//remove last ', '
insertQuery = insertQuery.Remove(insertQuery.Length - 2);
insertQuery = insertQuery + ")";
db.Execute(insertQuery);
insertsCount++;
}
}
}
catch (Exception ex)
{
result.Message = result.Message + "; Exception: " + ex.Message;
exceptionsCount++;
}
}
}
result.FileName = tableName;
result.UpdatesCount = updatesCount;
result.InsertsCount = insertsCount;
result.ExceptionsCount = exceptionsCount;
result.Message = result.Message + " Finish";
return result;
}
}
}