TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
hasib budi
NA
20
5.3k
loop through csv file
Sep 22 2020 3:38 AM
Have a proble with loop through csv file. Example i have two csv file in folder. csvfile1 has 10 row, and csvfile2 has 20 row. It will insert into database. And if a 2 new row added in csvfile1, Only insert into database those 2 new row.
foreach
(
string
path
in
Directory.EnumerateFiles(@
"C:\ihelp\record"
,
"*.csv"
)) {
string
[] lines = File.ReadAllLines(path);
List <
string
> list =
new
List <
string
> (lines);
list.RemoveAt(0);
list.RemoveAt(0);
list.RemoveAt(0);
int
row = list.Count;
Console.WriteLine(
"CSV got : "
+ row +
" line"
);
var upd1 =
"update list set lastrow =@lastrow where filename =@filename"
;
var cmd5 =
new
NpgsqlCommand(upd1, conn);
cmd5.Parameters.AddWithValue(
"@filename"
, Convert.ToString(path));
cmd5.Parameters.AddWithValue(
"@lastrow"
, Convert.ToDouble(row));
cmd5.ExecuteNonQuery();
if
(row >= RecordCount) {
for
(
int
f = RecordCount; f < row; f++) {
var line = list[f];
Console.WriteLine(
"Record count :"
+ RecordCount);
Console.WriteLine(
"line :"
+ line);
var columns = line.Split(
','
);
var sql =
"insert into result(datetime,name)values(@datetime,@name)"
;
var cmd6 =
new
NpgsqlCommand(sql, conn);
for
(
int
i = 0; i < columns.Length; i++) {
if
(i == 0) {
cmd6.Parameters.AddWithValue(
"datetime"
, Convert.ToDateTime(columns[i]));
string
Time = cmd6.Parameters[
"@datetime"
].Value.ToString();
Console.WriteLine(
"Record inserted successfully. datetime = "
+ columns[i]);
}
if
(i == 1) {
cmd6.Parameters.AddWithValue(
"name"
, Convert.ToString(columns[i]));
string
name = cmd6.Parameters[
"@name"
].Value.ToString();
Console.WriteLine(
"Record inserted successfully. name = "
+ columns[i]);
}
}
}
cmd6.ExecuteNonQuery();
Reply
Answers (
2
)
How to search and highlight the text in word document in iframe
assigning 2 textbox in c#