如何反击/交叉检查excel数据上传到sql数据库?

时间:2018-03-19 07:11:01

标签: c# sql sql-server excel database

string fpath;
using (OpenFileDialog ofd = new OpenFileDialog())
{
    ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    ofd.Title = "Select an excel file";
    ofd.Filter = "Excel Files | *.xls";
    if (ofd.ShowDialog() == DialogResult.OK)
    {
        fpath = ofd.FileName;
        DataTable dt = new DataTable();
        using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;
                                        Data Source="+ fpath +"; Extended Properties='Excel 8.0; HDR=YES'"))
        {
        conn.Open();
        using (OleDbCommand cmd = new OleDbCommand(@"SELECT * FROM [Sheet1$]",conn))
        {
            using (OleDbDataAdapter oda = new OleDbDataAdapter(cmd))
            {
                oda.SelectCommand = cmd;
                oda.Fill(dt);
            }
        }
        conn.Close();
    }
    if (dt != null)
    {
        using (SqlConnection conn = new SqlConnection(@"Data Source=(local);
                                               Initial Catalog=inventory;
                                               Integrated Security=True"))
        {
            conn.Open();
            using (SqlBulkCopy sbc = new SqlBulkCopy(conn))
            {
                sbc.DestinationTableName = "dbo.product";
                sbc.ColumnMappings.Add("Item", "Item");
                sbc.ColumnMappings.Add("Brand", "Brand");
                sbc.ColumnMappings.Add("Part", "Part");
                sbc.ColumnMappings.Add("Description", "Description");
                sbc.ColumnMappings.Add("Manufacturer", "Manufacturer");
                sbc.ColumnMappings.Add("Car", "Car");
                sbc.ColumnMappings.Add("Year", "Year");
                sbc.ColumnMappings.Add("Price", "Price");
                sbc.WriteToServer(dt);
                MessageBox.Show("Import Successfull.", "Import Data", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                conn.Close();
            }
        }
    }
}

上面的代码允许用户上传要复制到的scel数据库的excel文件(97-2003格式)。问题是如何检查数据库中是否已存在Excel数据?

excel文件与sql数据库具有相同的列名。

1 个答案:

答案 0 :(得分:0)

我使用CTE解决了这个问题,CTE将删除重复项并保留单个数据。每次上传excel时都会运行。

using (SqlCommand cmd = new SqlCommand(@"WITH CTE_DELETE AS
                                         (SELECT ROW_NUMBER() OVER
                                         (PARTITION BY Item,
                                                       Brand, 
                                                       Part, 
                                                       Description, 
                                                       Manufacturer,
                                                       Car, 
                                                       Year, 
                                                       Price ORDER BY Item)
                                          RN FROM dbo.product)
                                          DELETE FROM CTE_DELETE WHERE RN > 1", conn))
{
    cmd.ExecuteNonQuery();
}