使用SqlBulkCopy时如何防止重复条目?

时间:2012-10-24 15:27:22

标签: c# asp.net sql sql-server duplicates

我使用SqlbulkCopy将excel数据导入sql server,但问题是我想阻止输入任何重复记录。有没有办法可以自动忽略或删除副本?

protected void Button1_Click(object sender, EventArgs e)
{
    string strFileType = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();
    string strFileName = FileUpload1.PostedFile.FileName.ToString();

    FileUpload1.SaveAs(Server.MapPath("~/Import/" + strFileName + strFileType));
    string strNewPath = Server.MapPath("~/Import/" + strFileName + strFileType);

    string excelConnectionString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strNewPath + "; Extended Properties=Excel 8.0;");

    // Create Connection to Excel Workbook
    using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
    {
        var command = new OleDbCommand("Select ID,Data FROM [Sheet1$]", connection);

        connection.Open();

        // Create DbDataReader to Data Worksheet
        using (DbDataReader dr = command.ExecuteReader())
        {
            // SQL Server Connection String
            string sqlConnectionString = "Data Source=ARBAAZ-1B14C081;Initial Catalog=abc;Integrated Security=True";

            con.Open();
            DataTable dt1 = new DataTable();
            string s = "select count(*) from ExcelTable";
            string r = "";
            SqlCommand cmd1 = new SqlCommand(s, con);

            try
            {
                SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
                da1.Fill(dt1);
            }
            catch
            {
            }

            int RecordCount;
            RecordCount = Convert.ToInt32(cmd1.ExecuteScalar());
            r = RecordCount.ToString();
            Label1.Text = r;
            con.Close();
            int prv = Convert.ToInt32(r);

            // Bulk Copy to SQL Server
            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
            {
                bulkCopy.DestinationTableName = "ExcelTable";

                SqlBulkCopyColumnMapping mapping1 = new SqlBulkCopyColumnMapping("excelid", "id");
                SqlBulkCopyColumnMapping mapping2 = new SqlBulkCopyColumnMapping("exceldata", "data");

                bulkCopy.ColumnMappings.Add(mapping1);
                bulkCopy.ColumnMappings.Add(mapping2);

                bulkCopy.WriteToServer(dr);
            }

            con.Open();

            DataTable dt = new DataTable();
            s = "select count(*) from ExcelTable";
            r = "";
            SqlCommand cmd = new SqlCommand(s, con);

            try
            {
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
            }
            catch
            {
            }

            RecordCount = Convert.ToInt32(cmd.ExecuteScalar());
            r = RecordCount.ToString();
            Label1.Text = r;
            con.Close();
            int ltr = Convert.ToInt32(r);

            if (prv == ltr)
            {
                Label1.Text = "No records Added";
            }
            else
            {
                Label1.Text = "Records Added Successfully !";
            }
        }
    }
}

通过创建删除重复项的触发器可以解决此问题吗?如果有,怎么样?我是新手,我从来没有创造过触发器。

另一个问题是无论我尝试什么,我都无法让列映射工作。当Excel工作表和数据库中的列名不同时,我无法上传数据。

3 个答案:

答案 0 :(得分:9)

您可以创建INDEX

CREATE UNIQUE INDEX MyIndex ON ExcelTable(id, data) WITH IGNORE_DUP_KEY

当您将批量数据插入数据库时​​,不会插入所有重复值。

答案 1 :(得分:0)

不,您在将代码加载到数据库中之前在代码上的dr对象上管理它(如运行DISTINCT操作),或者在数据库上创建触发器以进行检查。触发器将减少批量插入的性能。

另一种选择是批量插入临时表,然后使用select DISTINCT

将临时表中的目标表插入到目标表中

答案 2 :(得分:0)

据我记得, 从excel文件本身导入时,可以过滤掉冗余行。

应该在OleDbCommand构造函数中使用以下SQL查询。

var command = new OleDbCommand("Select DISTINCT ID,Data FROM [Sheet1$]", connection);