DataSet or the DataTable are the offline version of your original Database. Usually, you need only either the DataSet or the DataTable - whereas mostly the DataTable. Once, you fill your Component, all the selected Data will be stored in your memory (in fact). Everytime, you need the current Data from the Database or you have to upload the Data in the Database, you need some steps to realize it. Of course, there are several ways but I will show you the mostly used one and it is really as easy as it sounds in the title

Note, that I am using the SQL Provoder.
The Components, you need are:
- a connection object
- a connection string
- a command object
- a command string
- a DataAdapter object
- and last but not least the DataSet or the DataTable
First of all, you define the connecton object and the connection string, both provided by the System.Data.SqlClient, and assign the string to the object.
SqlConnection SqlConn =
new SqlConnection
();
SqlConnStr =
"Data Source=<Computername>\\>Servername>;Initial Catalog=<DatabaseName>;" +
"Integrated Security=True;Pooling=False";
SqlConn.
ConnectionString = SqlConnStr;
// alternate (whereas the connection string is commited by a parameter)SqlConnection SqlConn =
new SqlConnection
("Data Source=<Computername>\\>Servername>;Initial Catalog=<DatabaseName>;" +
"Integrated Security=True;Pooling=False";
);
The connection string is divided into 4 Tags (in this case). The first is the Data Source - it means the computer, where the Server is running and, of course, the Servername on it. The second one is the name of your Database in the logged server (Initial Catalog). The integrated security means wheter the authentification is provided by the windows account usage or the server account (true is recommended and represents the windows account). Pooling is optional and says wether the pooling will be activated or not.
The second step of our goal is to define the command object and the command text (string). Both, the command text and the connection object, we created above, have to be assigned to the command object.
SqlCommand SqlSelectCmd =
new SqlCommand
();
string SqlCmdTxt =
"SELECT * FROM northwind WHERE Acc_Name = 'Paul' ";
SqlCommand.
CommandText = SqlCmdTxt;
SqlCommand.
Connection = SqlConn;
At this point, lets create the DataAdapter and the DataTable.
DataAdapter da =
new DataAdapter
();
DataTable dtAccount =
new DataTable
();
Note, that the DataAdapter associate the DataTabe with the Database.
DataTable / DataSet <----- DataAdapter ------> DataBase
The DataTable will be filled by the DataAdapter with data from the Database. The data is, what you define in the command text.
Now, we attach the command object to the DataAdapter:
da.SelectCommand = SqlSelectCmd;
Well, there is one step required to finish our lesson. We fill our created DataTable with data.
da.fill(dtAccount);
Thats it!
The fill-method of the DataAdapter needs a DataTable or a DataSet (or another tablecomponent) that will be filled after you invoke the method. Notice that the fill method opens and closes the connection automaticaly (we attached the connection to the command object). Feel free to open a connection manually, but don't forget to close it after the transaction! In case, you open a connection manually, the fill-mehtod will not manage the connection automatically. In fact it can be usefull to open and close a connection manually. Look at the code below:
da.fill(dtAccount);
da.fill(dtArticle);
da.fill(dtShop);
Well, this will open and close the connection three times!!!
In this case, we open and close the conneciton one times:
Try
{
SqlConn.Open();
da.fill(dtAccount);
da.fill(dtArticle);
da.fill(dtShop);
SqlConn.Close();
}
catch (SqlException SqlEx)
{
System.Windows.Forms.MessageBox.Show("A problem occurs while connecting to the server!" + SqlEx.ToString());
}
Why so, I have already told you - becouse if you open a connection manually, the fill method doesen't care about the connections anymore (only for the current usage). Becouse there can be caused lots of errors while connecting to the server, you should place the commands in a try-catch block.