划分DataTable的行

时间:2015-06-10 08:08:55

标签: c# sql-server datatable duplicates sql-insert

我正在表中插入Excel表的记录。问题是Excel工作表包含在某些列中具有duplicate值的行。但由于我在DataTable中阅读整个Excel,所有这些行都加载到我的DataTable中,因此当我插入SQL Table时,我得到重复的记录。任何人都可以建议任何解决方案吗?我怎样才能避免这种重复。我是否应该以某种方式打破行中的DataTable?

C#代码:阅读Excel并在DataTable中插入行

        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[42] { new DataColumn("Template", typeof(string)),
                new DataColumn("Cust_Name", typeof(string)),
                new DataColumn("Invoice_No", typeof(int)),
                new DataColumn("InvoiceDate",typeof(DateTime)),
                new DataColumn("SR_No", typeof(int)),
                .
                .
                .
                new DataColumn("ContactTel3", typeof(string))});

        foreach (GridViewRow row in GridView1.Rows)
        {
            int rowIndex = row.RowIndex;
            if (rowIndex > 0)
            {
                string Template = row.Cells[0].Text;
                string Cust_Name = row.Cells[1].Text;
                int Invoice_No = int.Parse(row.Cells[2].Text);
                //DateTime InvoiceDate = DateTime.ParseExact(row.Cells[3].Text, "d-MMM-yy", CultureInfo.InvariantCulture);
                string InvoiceDate = (row.Cells[3].Text);
                int Sr_No = int.Parse(row.Cells[4].Text);
                .
                .
                .
                string ContactTel3 = (row.Cells[41].Text);
                dt.Rows.Add(Template, Cust_Name, Invoice_No, InvoiceDate, Sr_No, Description1, Description2, Description3, Description4, Description5,
                    CurrencyCode, Amount, Subject, Reference, CustomerAddress1, CustomerAddress2, CustomerAddress3, CustomerAddress4, CustomerAddress5,
                    CustomerAddress6, CustomerTelephone, EmailIdTo, EmailIdCC, BankName, AccountTitle, AccountNo, CurrencyCode1, BankAddress1,
                    BankAddress2, BankAddress3, BankAddress4, SwiftCode, IBAN, ContactName1, ContactEmail1, ContactTel1, ContactName2, ContactEmail2,
                       ContactTel2, ContactName3, ContactEmail3, ContactTel3);
            }
        } 

将DataTable传递给存储过程的C#代码

string consString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
                SqlConnection con = new SqlConnection(consString);
                using (SqlCommand cmd = new SqlCommand("[spInsertExcel]"))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Connection = con;
                        cmd.Parameters.AddWithValue("@tblInvoice", dt);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();

这是我的存储过程:

Create PROCEDURE [dbo].[spInsertExcel]
@tblInvoice [tblInvoiceType] READONLY
AS
BEGIN
      SET NOCOUNT ON;
      IF NOT EXISTS(Select Invoice_No from Invoice)
      Begin
        INSERT into Invoice([Template],[Cust_Name],[Invoice_No] ,[InvoiceDate],[Sr_No] ,[CurrencyCode] ,[Subject] ,[Reference],[CustomerAddress1] ,
        [CustomerAddress2] ,[CustomerAddress3] ,[CustomerAddress4] ,[CustomerAddress5] ,[CustomerAddress6] ,[CustomerTelephone],[EmailIdTo] ,
        [EmailIDCC] ,[BankName] ,[AccountTitle] ,[AccountNo] ,[Bankcurrency] ,[BankAddress1] ,[BankAddress2] ,[BankAddress3] ,[BankAddress4] ,
        [SwiftCode],[IBAN],[ContactName1],[ContactEmail1],[ContactTel1],[ContactName2],[ContactEmail2],[ContactTel2],[ContactName3],[ContactEmail3],[ContactTel3])

        Select [Template],[Cust_Name],[Invoice_No],[InvoiceDate],Sr_No, 
        CurrencyCode,[Subject], Reference,CustomerAddress1,CustomerAddress2,CustomerAddress3,CustomerAddress4 ,
        CustomerAddress5,CustomerAddress6,CustomerTelephone,EmailIdTo , EmailIDCC,BankName, AccountTitle,
        AccountNo , Bankcurrency,BankAddress1,BankAddress2,BankAddress3,BankAddress4,SwiftCode,IBAN,ContactName1,ContactEmail1,ContactTel1,
        ContactName2 ,ContactEmail2,ContactTel2,ContactName3 ,ContactEmail3,ContactTel3 from @tblInvoice
    END
    Else
        Raiserror('You have already uploaded this file',16,1)
END

EDITED 正如用户在这里回答的那样,我包含了以下代码行

DataTable distinctDt = dt.DefaultView.ToTable(true, "Template", "Cust_Name"...);

但我仍然得到同一张桌子。调试中的distinctDt的附加图片

enter image description here

我想在Template表中插入重复的列,如Cust_NameInvoice_NoInvoice等,以及Amount,{{1}等非重复列在另一个表中。但这种方式Description1表将有多个记录。我怎样才能实现它?

3 个答案:

答案 0 :(得分:0)

通过在DataRow集合上调用Distinct方法,可以在将类似记录添加到DataTable后对其进行过滤:

var distinct = dataTable.AsEnumerable().Distinct(DataRowComparer.Default);

或者您可以在添加到DataTable之前过滤它们,如下所示:

GridView1.Rows.Select(x=> new 
{
                Template = row.Cells[0].Text;
                Cust_Name = row.Cells[1].Text;
                Invoice_No = int.Parse(row.Cells[2].Text);
                InvoiceDate = DateTime.ParseExact(row.Cells[3].Text, "d-MMM-yy", CultureInfo.InvariantCulture);
                nvoiceDate = (row.Cells[3].Text);
                Sr_No = int.Parse(row.Cells[4].Text);
}).Distinct();

答案 1 :(得分:0)

您可以在将表转储到数据库之前从表dt中选择不同的值。使用下面的代码。

DataTable distinctDt = dt.DefaultView.ToTable(true, "Template", "Cust_Name"...);

现在使用distinctDt将不同的数据转储到数据库中。 有关此内容的更多信息,请参阅MSDN

答案 2 :(得分:0)

我认为您需要在IDENTITY表中添加invoice列 如果您添加一个IDENTITY(1, 1)字段说明ID,您可以以索引方式对其进行自定义操作。
现在,您可以使用其他存储过程将数据从invoice表格复制到otherTable,如下所示:

INSERT INTO [otherTable]
SELECT 
    [Amount], [Description]
FROM
    [invoice]
GROUP BY
    [Amount], [Description]