Inserting a datatable into SQL Server - c#

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.

Related

data set in visual studio

i am creating an application using visual studio that use a database of course. i don't get why we create a data set as i tried some query without creating a data set and it worked perfectly. the queries i will be using are update, delete, insert and select (simple and complex ones).
so should i use the data set and why?
Note the database is a big one, and as i understood creating a data set will create a copy of the database, so will this make a storage (memory) problem?
You are looking for ways without a Dataset..
For INSERT, UPDATE and DELETE can use System.Data.Sql and System.Data.SqlClient, open your own SqlConnection and proceed https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection?view=dotnet-plat-ext-3.1 .... but for reads (SELECT), it is practical to use a DataSet. On this level (below Entity Framework !) the DataSet has a Fill() method, you can fill it with any data you want. The only class I know of that can read without a DataSet is DataReader, refer https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/retrieving-data-using-a-datareader
NOTE: as mensioned in the comments above, these low level methods to access a database are inherently unsafe, because you have to specify and pass explicit SQL to ADO, making user input vulnerable.. This can be avoided using a parametrized DataAdaper+Dataset, or Entity Framework, in either way you can avoid SQL injection.
If you have a big database you should consider using an ORM like Entity Framework. By using the old ADO .Net a dataset would help you handling your data. Inside it you will have a DataTable that represents your data and so, whenever you make a select the rows of your database will be stored temporarily in your dataset/datatable. That said, you don't need all your database there, but only the rows you need to work on.
Every time you add a new record to the datatable, it is inserted flagged as new. When you modify a row, it is flagged as modified or deleted, when it is the case. So after all handling you can save your dataset back to the database.

Update SQL Server database via Entity Framework

I am working with Entity Framework in a C# project to read/write data from/to a SQL Server database.
Until now, I read the data and did some calculations. Now I want to write the results to the database. What I essentially have is a list of [ID, Date, Value] tuples.
The update logic should be like an UPSERT, that is, an INSERT should occur, if in the target table for the combination of ID and Date no row is present. An UPDATE should be done, if the combination of ID and Date is already present in the target table.
Let's say I have ~1000 records in my result list. If I loop through the list and execute the UPSERT logic for each item in the list, that would result in ~1000 INSERT or UPDATE statements. I think that there should be a more efficient way to do this.
Hence, my two questions are:
How can I implement the described UPSERT logic without generating a separate SQL statement for each result item?
More generally: how would such an UPSERT logic look like in C# using Entity Framework?
I am unable to comment to ask if you have access to stored procedures, or if you have had other considerations, so apologies in advance.
Try a delete where [ID, Date] for all records you are 'upserting' and then a bulk insert. You wouldn't necessary need Entity Framework poco actions, but it would solve your issue.
Ideally, for performance, you want to generate a SQL Server MERGE statement, which does the INSERT and/or UPDATE as a single action. I don't believe that this is built into Entity Framework, unfortunately, but there are fairly straightforward workarounds. For one, see here:
https://www.flexlabs.org/2018/02/adding-upsert-support-for-entity-framework-core

Write data to two SQL databases from Entity Framework in c#

I am having an issue with my website(ASP.NET, c#, SQL, Code-first Entity Framework).
I have a project with an attached SQL database generated from code first entity framework.
Now I have imported another SQL database using model first database which looks almost same but the table and column names are different.
So now I would like to write data to two databases at the same time with just one click from my web application.
The newly attached database will be a backup and we should write data to both databases at the same time.
Any suggestions would be highly appreciated.
Thanks
As others have suggested, you need to do the mapping yourself, but one thing I would like to add, you may need to wrap your SaveChanges() into a transaction, you may find steps here: https://msdn.microsoft.com/en-us/data/dn456843.aspx

LINQ to SQL: Alter Table or Add New Table to Existing Database

Does LINQ to SQL allow tables to be altered similar to the way tables can be created DataContext.CreateDatabase()?
I know its bad practice to modify the database, however, new information may need to be added to our data structure. I wondered if LINQ to SQL allowed for a push system to alter existing tables?
If there is no built in LINQ to SQL class for this functionality, what isthe best way to approach this?
Update:
It appears as if this is not possible. I have requested a new feature to be added to the .NET Framework. Vote for it if you are also interested: connect.microsoft.com Feature Request.
Also see the question Is it possible to use Linq to ALTER a database table?.
I don't know of any way to do it...I think given that LINQ is trying to abstract the structure of the db away, that's probably not the right tool.
I would just open a connection to the database and execute ALTER TABLE statements against the raw database.
I think the term for what you're looking for is "database migrations", popularized by ruby on rails. For .Net there is MigratorDotNet. It's not tied in to Linq To SQL (or any other ORM) at all, but it might help with what you're looking to accomplish.
Here's some other implementations for .Net
Check out Paul Stovell's great article on How to deploy a database. I've also used SQL Deploy to much success in the past.
a way to do the transfer is making a tool
1.- the tool must know the old database schema
2.- the tool must know the new database schema
with that just make the methods to upgrade the database, here an example
table Person (old) table Person (new)
id id
name name
age
then select all from the old database and then create the new rows in the updated database
new person {
id=oldPerson.id;
name=oldPerson.name
age=defaultAge;
}

Datagrid: apply row changes to the database

I have a Datagrid in my app. This datagrid fetches some data from a MySQL DB. They are fetched from a List<> to be true, because I'm not able to fetch the data from a Dataset (and I don't know why).
Anyway, when I update a field in my app i want these changes to be reflected on the list and therefore on the table in my DB.
Any idea?
Also, it's a good option to save tables data on a List<> or it's better to save them on a DataSet/DataTable?
Thank you.
I'll answer your last question first. In general, you want to use DataSet/DataTable, because they have many methods and properties related to database functionality. A List<> may get you where you're going for now, but extending it in the future will be a nightmare.
I would focus on getting a DataSet properly filled from your Database (see: http://dev.mysql.com/usingmysql/dotnet/ if you don't know where to start), and then simply setting your DataGrid to use that DataSet as its DataSource. You can then use things like LINQ to SQL or Entity Framework to better model that DataSet in code. Assuming you have the proper ODBC drivers installed, it should be as simple as creating the correct Connection String and doing everything normally from there.
You can definitely do things the way you're doing now, but you will have to manually send any SQL update statements instead of relying on the automatic ways of doing it. But I would seriously consider reworking it to use proper .NET data objects.

Categories

Resources