English 中文(简体)
How to select records from SQL DB that contain the same ID as a .NET datatable records
原标题:

I m loading a CSV file containing many thousands of rows into a .NET DataTable that looks something like this:

MyLocalDataTable SequenceNo, Etc

I have a table in an SQL database and I d like to pull records into another datatable based on what I find in the CSV file.

MySQLBDTable IdentityID, SequenceNo, Etc

The SequenceNo is my common field. I d like to do a SELECT on MySQLBDTable that only returns the rows with a SequenceNo from MyLocalDataTable. I m not sure how to do this. I want to avoid a SELECT query with thousands of WHERE SequenceNo= clauses.

Is there a simple and efficient way to do this?

最佳回答

Dump the sequence number values into a staging table, a "temporary" table that contains only one column, of the right type to hold a sequence number.

If you re using SQL Server, you can do this using bulk insert, which is very fast.

Then, you execute a SQL statement that uses the contents of that staging table for a join or a IN-clause to find the existing rows in the database.

For instance:

UPDATE yourtable
SET does_exist = 1
WHERE sequence_no IN (SELECT sequence_no FROM staging_table)
问题回答

Going along with what Lasse said, you can use SQLBulkCopy to do a bulk insert from your local data table directly into SQL Server.

You can generate query like this:

var numbers = localTable.Rows.Cast<DataRow>()
    .Select(r => r.Field<string>("SequenceNo"))
    .ToArray();

var queryTemplate = @"select * from MySQLBDTable where IdentityID in ({0})";

var queryText = string.Format(queryTemplate, numbers);

Please note that if you have too many rows in your local table, you may hit the limit of items in IN - in this case change your query into something like this:

select * from MySQLBDTable where IdentityID in (1,2,3,4,5,6,7,8,9,10)
union
select * from MySQLBDTable where IdentityID in (11,12,13,14,15,16,17,18,19,20)




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签