I have a small program that imports a csv file into a sql server database but when you press the button twice or something it adds duplicates. I need it so that when there are duplicate it skips that one. if someone can help me with the code, it would be awesome.
EDIT: i noticed some saying i need to disable the button while it is working while that is a solution for one thing i also want that if there is already something in the database that it will skip that part when it is the same in csv file as in the database.
code:
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"server=localhost;Initial Catalog=klantbestand;Integrated Security=SSPI;");
string filepath = @"C:\clients TEST.csv";
StreamReader sr = new StreamReader(filepath);
string line = sr.ReadLine();
string[] value = line.Split(';');
DataTable dt = new DataTable();
DataRow row;
foreach (string dc in value)
{
dt.Columns.Add(new DataColumn(dc));
}
while (!sr.EndOfStream)
{
value = sr.ReadLine().Split(';');
if (value.Length == dt.Columns.Count)
{
row = dt.NewRow();
row.ItemArray = value;
dt.Rows.Add(row);
}
}
SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
bc.DestinationTableName = "GegevensCSV";
bc.BatchSize = dt.Rows.Count;
con.Open();
bc.WriteToServer(dt);
bc.Close();
con.Close();
}