Database update after inserting rows into Dataset - c#

I am a newbie to Database programming. After inserting a couple of rows into a DataSet object, I am trying to write back the updated DataSet to the Database but cannot figure out how to do it.
Can you give an example for the following please?
what SQL insert command to use if DataSet is already updated with new rows
Databinding example - especially considering an initially empty table in the DB
I am using SQLExpress 2008 & its C# WinForms application.
thanks
ps: I have already looked at the related questions here. This one is different in that I am first adding my new data into a DataSet and want to update the DB then.

What you need to do is configure a DataAdapter or TableAdapter object that contains the proper Update command. Then when you are done updating the rows in your DataSet, you can call DataAdapter.Update(DataSet) and it will do all the hard work for you.
Since you're starting out, I'd suggest looking at the TableAdapter objects that are built using the XSD schema tool. They allow you to simply drop your tables into the XSD to create a schema, and then let the wizard generate the appropriate SQL commands (it'll even do Stored Procedures for you) to handle all the CRUD work. I've been using these for a while and love them.
EDIT: In response to Sesh's request, Scott Gu has a great tutorial series on using the Table Adapters here. I wanted to post this in the answer so others can find it easily.

Related

When and how to use DataSource instead of direct SqlConnection

I'm trying to learn SQL and how to use it in my C# projects, but I'm struggling to understand how to connect it. So far I've seen most people simply do
using (var connection = new SqlConnection(Properties.Settings.Default.TestConnectionString))
{
connection.Open();
/..
}
But in Visual Studio 2017, there is a tab for DataSource. Once I set my database connection there, I get a generated file DbNameTest, which I don't see to serve any purpose at all, there is no loaded data in it because I'm creating new instance of it every time I try to access it, I can save it, but I will still need to populate it the first time.
What's the point of using the DataSource tab and what's the purpose of the the DataSet?
This is a profound subject that has many answers and methods. I'll try to explain it simply.
The datasource tab consists of ready-made structures presented to developers. With the help of the components in this tab, you can do many operations with dragging and dropping the code.
However, many developers prefer to build a more extensible and manageable structure by writing code instead of drag and drop with ready components.
DataSet; It is a copy of the database in ADO.NET. A dataset can contain one or more DataTables. DataTables are database tables.

DevExpress Winforms XtraGrid and SQLite bound database, best way to insert rows?

I have successfully bound a SQLite database table to a DevExpress XtraGrid control, and can see the few test rows I have, and can also edit the values, and commit the changes back to the database with an Update command upon closing.
My question is what would be the best way for me to insert rows to the table? I have implemented and successfully used some example code for inserting rows into a SQLite table, however I am uncertain if the DevExpress XtraGrid has some type of method to allow me to skip all of the example code I have, and simply use the same functionality that seems to be already built into the control.
So should I use example code that connects to the database, builds the query then runs it on the database, or is there a better way, using something already built into the DevExpress WinForms suite?
Thanks.
You can use Embedded Navigator controls to insert rows !
https://www.devexpress.com/Support/Center/Question/Details/Q235790
After some research, I found that the best way to interact with the data in the grid, or with any database for that matter, is to use DevExpress's eXpress Persistent Objects for .NET. Great bit of technology. It allowed me to specify the database and table I was interested in, and it created all of the plumbing to allow me to deal with rows in the table like normal C# objects with properties.
If you are struggling with trying to mix in SQL queries and the like into your application, I highly recommend you make your life much easier and use XPO.
Here is a link to the documentation describing XPO: http://documentation.devexpress.com/#XPO/CustomDocument1998

connecting to different databases using .net datasets

I have many Databases with same structure and I have designed a dataset that matches the database design. It is easy to connect to database using connectionStrings which asked at design time and defined in app.config. But the problem arises when trying to change the database at runtime. I can not find any non-reflection solution to handle it. Is there any other way to change connection string of a dataset dynamically at run time or at least create dataset with different connection string!!!
You are filling DataSet using TableAdapter and you can easily modify TableAdapter connection string like this:
myTableAdapter.Connection.ConnectionString = connectionString;
Hope this helps :)
gzaxx answer will not work, simply because different DBMS work with different ADO.NET providers, which may or may not be compatible with each other. There's a lot of theory behind it and I won't type all of that in this textbox, but you need to understand that is the TableAdapters that is the main issue, and not the DataTable. Your business and UI layers normally only talk to DataTables which will mostly have the same structure for almost any DBMS given that you have correctly used corresponding data types when creating table columns. So, in theory, if Typed DataSets could provide a way to attach multiple Adapters per DataTable, you could add one adapter for each DBMS you support, while keeping the DataTable structure the same.
I myself had to deal with this issue in a somewhat large project and the only workable solution for me was to separate my Data Access into a separate project (a class lib) and then create one such DLL for each DBMS I was supporting. Hope that helps you get started with this.

ADO.net update without SQL?

This might be a dumb question. But does ADO.net support updating to a database without having to write SQL commands?
Example:
I have a method that reads from database and keeps the rows in memory. I then modify some of the rows. Is it then possible to make ADO.net update the newest changes to the database without having to write an update SQL statements but instead let ADO.net figure it out?
I am asking this because I might want to update at a much later point. I could just store the SQL statements in a list but then I would be doing many updates instead of just one big one which would take longer time.
What you need is some sort of ORM, and ADO is not an ORM. So, no. You must write the SQL. You could maybe simplify things by writing a stored procedure, though. Then you can use ADO parameters
If you want, you can save your changes as objects in memory until you need to actually persist them. Then you can have a mapper that will take the object and write the SQL for you. However, then you are redoing some of the work of what is already done in an ORM
Like the sql you used to get the data, you need sql to put the data. It also needs to update what column to update. I don't think it can be automatic. Or use the Entity Framework. Probably saving the objects to be updates (IDs) is the way to go or update instantly.
ADO.NET supports DataAdapters and DataSets which allow you to do the following:
Manipulate data within your DataSet.
Push changes to the database by passing your DataSet as a parameter to the Update method of the DataAdapter.
In order to get the DataAdapter to push the changes it is necessary to specify insert, update, and delete commands. You will have to specify some sql in your command configuration but it is like a template of the sql statement that will update each row that you operate upon rather than your having to manually track changes.
Once you have configured your commands, use the UPDATE method with the DataSet as the parameter and it will persist your changes based on your commands. You will not need to track the individual sql changes.
A sample of configuring commands can be found here.
A sample of calling the update can be found here.

Save a DataSet to a database

When I load from the database I use one store procedure which loads the DataItem and any Data associated with it. This comes back in one DataSet with two tables, the first table has one row and describes the DataItem and each row in the other table describing the related Data.
This DataSet is then used to populate my objects.
My problem comes when I have to save the objects back to the database. I am currently saving the DataItem and then looping through all of my Data and performing a save on each one. Completely horrible way to go about doing it, I know. It's both slow and it's not transactional.
So what I'd ideally like to do is convert my objects back into my DataSet and then save it all back to the database in one efficient transactional operation. What code do I need on the C# side to make this transactional and to allow me to pass back a DataSet. I presume this will involve using a TableAdapter. But given that I have two tables how will this work? What do I use on the SQL side - Can I use store procedures? (I would like to avoid having SQL in my C# project) Would I need to write something that will handle cycling through a datatable to save each record?
What's the best way to go about doing all this? This will form the lynchpin of a project I'm working on so I want it to be as fast and efficient as it can be!
(.NET 4.0 and SQL 2005)
Did not use TableAdapter in the end as it was more effort than it was worth.
From the comments:
http://msdn.microsoft.com/en-us/library/4esb49b4.aspx

Categories