DataTable like API in .Net Core
System.Data.DataTable is present in .Net core as an empty class to complete the interfaces implementation.
This issue is to track the work needed to bring in an API to provide DataTable like API in .Net Core.
cc @ajlam
@saurabh500 does this mean you guys are bringing back DataTable to .NET Core? Or are you planning to introduce a new API the performs some (or all) of DataTable's tasks?
FWIW I do think that dropping DataTable/DataSet is a good idea. I'm wondering if you guys are considering bringing it back purely for backwards-compatibility reasons or is there something else?
Hopefully the right place to post this. Will this include the ability to use a DataTable as a parameter in a stored procedure call? I have lots of SP's that take a TableValue parameter. In ADO.NET I fill a DataTable with data and pass it using SqlDbType.Structured as the parameter type. Unless there is currently another way to do this?
I do not think that sending a whole bunch of insert/update/delete statements is a viable alternative.
@nmunday maybe there already is an option to do that. SqlBulkCopy now accepts DbDataReader as an input instead of DataTable so I expect that this could work similar way, try pass DbDataReader instead of DataTable as the parameter. If not, I second that it should be possible somehow.
You can just use an IEnumerable<SqlDataRecord> in place of a DataTable
@sqmgh where would I find such a class (SqlDataRecord) in .NET Core? I tried the docs and intelisense in VS and it does not seem to be there. In the full framework SqlDataReader implements IDataRecord but not so in .NET Core.
@kenticomartinh that may work for some of the SP's that do straight inserts. Many SP's contain other logic that is executed at the same time, some SP's do not even end up inserting data at all. I will look into SqlBulkCopy for sure and use it when it does the job. Thanks.
@nmunday Add System.Data.SqlClient to your dependencies, and it's in Microsoft.SqlServer.Server. IntelliSense will recommend adding that using if you try and construct one. It's been in Core for quite a while.
DataTable/DataSet is rich in features, bit bloated but it do the job well. Not sure the real reason why this object should be dropped.
Some notable features are:
- Deserialize/Serialize schema and data from/to xml
- Column expression
- Change tracking via DataRowState
- Copyable and cloneable object
- Column's default value
- DataView
- etc
If there is something bad is that its store the data with object array which promote boxing and unboxing, this hopefully solved by tuple in c# 7 maybe Accept/Reject Change bit tricky to use but it works
Not everyone use ORM, not everyone use EF. Those who stay with ADO.NET should be punished like this. Those who battle with database query optimizer understand the pain losing control over your query because your query is automatically generated.
You use Stored Procedure, you change your select statement, you don't want to change the application just because your schema is changing.
With DataSet and DataTable I feel like having in memory database.
Optimize the object, make it lighter, faster but dropping its features.
@sqmgh thanks for the pointers. I did indeed find the class once I added the SqlClient reference. Not sure how I missed it. The code is now working. I was always unsatisfied with loading a DataTable just to then send it to a stored procedure. Seemed like a waste of memory and resources. Now by streaming with SQLDataRecord I can avoid the load altogether. Thanks again.
@nmunday No problem, and glad I could help. I was doing the exact same thing in the past, and I felt the exact same way before I discovered that I could just yield SqlDataRecords.
After that point I basically never used DataTable again. Now they just seem like a janky and overly complex for what you get ORM.
I suppose could potentially see some use for them in a very quick application where you have to load tabular data without knowing what columns will exist. However, that doesn't ever apply to me personally. I only ever deal in situations where I at least know which columns might be there. In that case I think it's more than worth the effort to just generate a record class. Dealing with hard typed columns tend to make the codes way easier to read or write, and that's without even looking at things on the performance and reliability side.
It seems many people are asking for something lightweight between EF Core and low-level System.Data.Common interfaces. I'm the author of the NI.Data library (.NET 4.x) that offers abstract (db-independent) queries and simple db-independent interface for CRUD-operations. I can port most demanded parts to a new corefx-oriented library (queries, dataview-abstractions, SQL-generation components) with a simple interface for high-performance operations with POCO and/or IDictionary instead of DataTable/DataRow. If someone is interested in this library don't hesitate to contact me.
UPD: just finished first version of data access library (supports both .NET Core and .NET 4.x) that implements SQL command builder and data adapter for CRUD operations: NReco.Data library. I hope it will be helpful for everyone who looking for DataRow/DataTable/SqlCommandBuilder/DbDataAdapter alternative in .NET Core.
This Microsoft article clearly demonstrates the advantages of using DataSets/DataTables to improve performance when connecting to an Azure SQL Database.
https://azure.microsoft.com/en-gb/documentation/articles/sql-database-use-batching-to-improve-performance/
corefx needs this to work nicely with Microsoft's own SQL Database service.
@j055 I don't think there's any performance optimization provided by DataSets/DataTables that isn't possible with regular commands. You can batch multiple inserts into a single DbCommand just as easily.
As easily as this?
https://msdn.microsoft.com/library/aadf8fk2.aspx
Please do tell.
How about new SqlCommand("INSERT ...; INSERT ...; INSERT ...;", conn)?
@j055 in my comment above I mentioned that I've just published NReco.Data library that may generate SQL commands like old ADO.NET DbCommandGenerator/DbDataAdapter. Command batching (generating several SQL commands into one DbCommand) can be easily added; if you really need this feature feel free to add an issue with batching feature request. I believe that with NReco.Data you'll get definitely better performance than with DataTable/DataAdapter in old .NET Framework.
@roji I don't know how many people want to build up their commands with a big concatenated string. The term 'SQL Injection attack' also springs to mind. Of course it's doable. I just like a more elegant approach.
@VitaliyMF I'm sure your library is good for new projects but we have 10years worth of data access code using DataSets/DataTables.
@j055 SQL injection attacks are about concatenating external input into your SQL, this has nothing to do with the batching question. In other words you can simply do:
new SqlCommand("INSERT INTO mytable (myfield) VALUES (@p1); INSERT INTO mytable (myfield) VALUES (@p2)", conn)
and be completely safe.
Whether you like this API or find DataTable/DataSet more elegant is a different (and not necessarily relevant) question. I happen to agree that there should be a structured batching ADO.NET API, which is why I've opened https://github.com/dotnet/corefx/issues/3688, but DataTable/DataSet definitely aren't the answer to that problem.
The main point here regarding what you said is that batching is entirely possible without DataTable/DataSet, so no optimization is being taken away by removing them.
DataTable/DataSet is a layer on top of basic ADO.NET (which is about connections and commands). IMHO it's neither here nor there: not quite an ORM, not strongly-typed and generally not so good. There may be a place for something like this out in the world, but it definitely shouldn't be a part of the basic ADO.NET layer in any way. The separation is important.
@roji DataSets are good for other things too and I guess we are not the only people using them in our applications. I think a lot of people will find it difficult to migrate without them. Yes, put it in a different project, that's fine.
The fact that they are not strongly-typed is actually very useful when transporting SQL data from stored procs to the business layer and back again. ORMs, yuck!