简体   繁体   中英

DataTable to SQL CE 4 Server

I have a DataTable in C# and would like to send it to my SQL CE 4 server. One thing that makes it a bit more complicated is that when it encounters an duplicate, it should either ignore it and move on to the next row in the DataTable. I've looked around but a lot of information I find doesn't seem to work with the CE version of SQL Server. What's an efficient way of doing this?

Filter your DataTable to exclude the duplicate rows before uploading, using the DataTable.Select Method

eg

    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(); 

Try this Logic.

 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"); 
          }  
    }

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM