duplicate products added when importing csv files

c# csv import sqlbulkcopy

Question

I am using the code below to import the csv files onto sqlserver 2008, the issue is , its adding duplicate records onto the table , how can i check to avoid duplicate records import , below is the code which i am using , i have copied from somewhere , and have forgotten the link so cant reference it , the code works fine as it imports the data , but it doesnt checks before importing , the primary key i am using for my table is productid, below is the code, any assistance or suggestion will be appreciated:

SqlConnection con = new SqlConnection(@"Data Source=xxxxxxxx;Initial Catalog=xxxx;Persist Security Info=True;User ID=xx;Password=xxx");
    string filepath = Server.MapPath(FileUpload1.FileName);//"C:\\params.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 = "products";
        bc.BatchSize = dt.Rows.Count;
        con.Open();
        bc.WriteToServer(dt);
        bc.Close();
        con.Close(); 

}

Popular Answer

Normally, you should be able to set the PK field to be the productid. Insert would fail automatically if it's duplicated...

You could also sort the DataTable, pass through and process the duplicated rows with some logic.

Another solution would be to add the productid in some sort of searchable array and check against it before adding a new row.

HTH,



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why