2011-12-20 38 views
0

我在C#中有一个DataTable并想将它发送到我的SQL CE 4服务器。有一点让它更复杂一点是,当它遇到重复时,它应该忽略它并转到DataTable中的下一行。我环顾四周,但我发现很多信息似乎不适用于CE版的SQL Server。什么是这样做的有效方式?DataTable到SQL CE 4服务器

+1

你可以张贴什么您尝试。 – BizApps 2011-12-20 01:47:14

+0

http://stackoverflow.com/questions/8566784/invalid-option-specified-in-create-index-statement。几个小时前,我遇到了这个库http://sqlcebulkcopy.codeplex.com/,但它只是在抛出一个重复时抛出一个异常,并且似乎没有覆盖选项。 – Skoder 2011-12-20 01:50:07

+0

为什么不简单地从数据表中删除重复的行(ala The complicators gloves)在上传之前? – 2011-12-20 01:52:03

回答

1

过滤您的DataTable排除在上传之前,重复的行,使用DataTable.Select方法

例如

DataTable table = DataSet1.Tables["Orders"]; 

    // Presuming the DataTable has a column named Date. 
    string expression; 
    expression = "Date > #1/1/00#"; // you will need logic to remove your duplicates 
    DataRow[] foundRows; 

    // Use the Select method to find all rows excluding duplicates 
    foundRows = table.Select(expression); 

    // .NET 3.5 onwards 
    DataTable filteredDataTable = foundRows.copyToDataTable(); 
+0

谢谢,我还没有遇到DataTable.Select表达式。 – Skoder 2011-12-20 02:02:31

0

试试这个逻辑。

var dt = new DataTable(); //Supposed that this is your DataTable 

foreach(DataRow row in dt.Rows) 
    { 

     var find = MyFindMethod("Id"); 1. select statement that find if the id is on database 

     if(find.Rows > 0) 
      { 
      //Id exist do nothing 
      } 
     else 
      { 
      //Id not exist then 2. Do Insert to sql ce id I not exist 
      MyInsertMethod("Id"); 
      } 
    } 

问候