防止在C#中从Excel工作表导入重复记录

时间:2017-10-30 12:49:23

标签: c# excel

我需要阻止从Excel工作表中导入重复值。这是我的代码。这很好用。但我需要改进此代码以防止重复。 需要快速解决这个问题。

 private void btnGet_Click(object sender, EventArgs e)
                {
                    try
                    {
                        intMode = 1;
                        dsMain = new DataSet();
                        if (dgGrid.Columns.Count != 0)
                        {
                            for (int i = dgGrid.Columns.Count; i > 0; i--)
                            {
                                dgGrid.Columns.Remove(dgGrid.Columns[0].Name);
                            }
                        }
                        dgGrid.Columns.Clear();

                        odfExcelGet.Title = "Excel Upload";
                        odfExcelGet.FileName = "Excel";
                        odfExcelGet.Filter = "Excel File (*.xls;*.xlsx;)|*.xls;*.xlsx;";
                        odfExcelGet.InitialDirectory = @"c:\";
                        odfExcelGet.ShowDialog();
                        txtAddress.Text = odfExcelGet.FileName;

                        this.Cursor = Cursors.WaitCursor; 

                        string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
                        ConnectionString += odfExcelGet.FileName;
                        ConnectionString += @";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";";

                        OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", ConnectionString);
                        da.Fill(dsMain);

                        dgGrid.DataSource = dsMain.Tables[0];
        //rest of code

                }

0 个答案:

没有答案