How to bulk insert data into db using Linq to SQL? - c#

In the past, I always use Class SqlBulkCopy to complete bulk insert. But I don't know how to implement it using Linq to SQL. If inserted one by one, Efficiency will be very low.
Any good ideas?
Thanks in advance and sorry for my poor English.

Simple: you don't. You just use SqlBulkCopy. LINQ-to-SQL is simply a tool. SqlBulkCopy is a tool. Use the right tool for each job. Sometimes that means using something that isn't LINQ-to-SQL. This might mean creating a DataTable (or a spoof IDataReader if you are feeling ambitious) to represent the data; look perhaps at Convert generic List/Enumerable to DataTable? to get from your typed objects to a DataTable you can feed to SqlBulkCopy.

See this article on SubmitChanges().
Your changes are not transmitted to the server until you explicitly call SubmitChanges on the DataContext.

Related

Map SQL Query results into entities (C#)

Is there a way to automap a query into a class in C# in order to strong type the query results? I don't want to access to query result with a DataTable, specifying manually the Column Name anymore.
There is couple of options:
EntityFramework - powerful and heavyweight. https://msdn.microsoft.com/en-us/library/aa937723(v=vs.113).aspx
Dapper - lightweight - mostly mapper https://www.codeproject.com/Articles/212274/A-Look-at-Dapper-NET
If you need only mapping of stored procedures, queries etc, Dapper is good way to go, as it's basically adding extension methods to connection object. It cannot create database etc as Entity Framework but it all really depends on needs.
My favourite question. Use QueryFirst. Write your SQL in the SQL editor window, syntax validated as you type, then QueryFirst will generate the C# wrapper to execute it, with strongly typed inputs and outputs. And there are numerous other benefits :-) SqlServer, MySql and Postgres support built in. Others easy to add. Disclaimer : I wrote QueryFirst.
You should take a look at AutoMapper. It's an easy and user-friendly way to map a DataTable to a List<object> of your specification.
You can find an article which talks about it (including sample code) here.
If you want to automize the process, you can use dynamic objects which don't require you to create specific classes for every DataTable that you're using.Hope this helped!
There is a helpful library on LINQ to DB
"LINQ to DB is the fastest LINQ database access library offering a simple, light, fast, and type-safe layer between your POCO objects and your database."
https://github.com/linq2db/linq2db

Inserting a datatable into SQL Server

I currently have a populated datatable, but I'm having trouble inserting it into my existing (empty) database.
I have looked into sqlbulkcopy as well, but haven't had much luck.
Although using Entity Framework I would expect:
_db.TableName.AddRange(dt);
To properly insert the new data from my datatable.
Where am I going wrong here?
You are missing a few steps. First you need to create the table on the database, this is generally done not at run time for lots of complex reasons. Research Entity Framework Code First for more details on how to do this.
Secondly if you truly want to use Entity Framework you will need to convert your data table into a set of objects of the correct type.
Honestly though I would avoid Entity Framework and embed a CREATE TABLE script (or even better define it before you execute) and then use SqlBulkCopy, just note that SqlBulkCopy needs the table defined ahead of time.

update same datatable using Linq without loop

I have one datatable with 26 columns in it.
I need to update specific column based on filter.
but I dont want to do it using iteration because it's having thousands of records. It waill affects performance.
is there any way to do that.
I am new for linq so I searched for that but not getting proper solution.
There are some solutions but I can not understand it.
Please if anyone having solution?
This is where you have to either drop into ADO or seriously customize linq or EF.
Bulk inserts and updates are not something it does nicely.
Is batch or bulk insert possible in Linq 2 Sql ?
the same goes for EF.
Multiple row update is not supported by EF. For this better you use stored procedure. This is the reason EF has provided support for executing stored procedure. Use it and enjoy :)

Update table : Dataset or LINQ

I am trying to perform insert/update/delete operations on a SQL table based on the input csv file which is loaded into data table from an web application. Currently, I am using DataSet to do CRUD operations but would like to know if there will be any advantages of using LINQ over DataSet. I am assuming code will be reduced and more strongly typed but not sure if I need to switch to LINQ. Any inputs appreciated.
Edit
It is not a bulk operation, CSV might contain 200 records max.
I used the LumenWorks CSV reader which is very fast. It has it's own API for extracting data, using the IDataReader interface. Here is a brief example taken from codeplex.com. I use it for all my CSV projects, as it's very fast at reading CSV data. I was surprised at how fast it actually was.
If you were to go from a reader like this, you're essentially going from a data reader API and as such, would probably work with a data table more easily (you could create a DataTable matching the result set and easily copy data over matching column to column).
A lot of updates can be slower with LINQ, depending on whether you are using Entity Framework or something else, and what flavor you are using. A DataTable, IMHO would probably be faster. I had issues with LINQ and change tracking with a lot of objects (if you are using attached entities, not using POCOs). I've had pretty good performance taking a CSV file from Lumenworks and copying it to a DataTable.

writing SQL queries - methods to write complicated ones!

Hey guys, does anyone have any suggestions that might help with the following?
I am rewriting some software, which I did for a prototype for where I work, I am turning it into a more OOP compliant program :)
I have just written a custom database handler class to deal with my connections, my queries etc. The idea is that this database handler does everything needed to deal with the DB and only returns the result set of the query being run.
Anyways, I have just written a few methods which write my SQL queries for me - the idea being that I pass it some arguments in the form of an Array and the class writes the SQL String needed to query, which removes SQL injection problems.
The problem I have is; with normal selects (with where arguments and order/group by ) and insert and update, These all work fine. But if I want to pass a query which might have a join, or a multi-table join or a where-clause that contains a like or a sub select on the where (this one might be doable with running the select method twice!)
I can't work out how to get the method to produce these queries. Does anyone have any suggestions? - Might have to build custom ones where there is no way around not writing the query myself.
The other idea is over complicating things and to just perform a call that removes slashes contained in the passed string.
Thanks in advance,
vade
btw if it doesnt make much sense, been coding since 7 this morning, brain dying slowly! :)T
I would encourage you to consider writing stored procedures for the functionality you need rather than trying to write some kind of generic query building mechanism.
You could just use sqlcommand with parameters instead http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx
If you need a complex query-building mechanism, consider one of the many ORM frameworks already developed, such as NHibernate or MSEF. These allow you to create some pretty complex queries using Linq (compiler-checked; gotta love it) that then are translated to SQL.
If you have the option on the table you could look into doing a LINQ to SQL datalayer. That will let you work with your tables and query results as classes.
Its very easy to get started making a .dbml of your database, check out a walk through on MSDN
It also leaves the dynamic SQL authoring to the MS Language team, so thats nice.

Categories

Resources