sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
Tony Johansson's Avatar

DataSet


Question posted by: Tony Johansson (Guest) on August 20th, 2008 11:45 AM
Hello!

This is also a working console program.
It seems to me that a DataSet is actually the result of the query as in my
example
because the result is from two tables.
Is this correct understood ?

What will happen if you change some row and then call the update for
DataAdapter object ?


static void Main(string[] args)
{
//Specify SQL Server-specific connection string
SqlConnection thisConnection = new SqlConnection(
@"Server=UHT-DEMO1; Integrated Security=True;" +
"Database=northwind");

//Create DataAdapter object
SqlDataAdapter thisAdapter = new SqlDataAdapter(
"Select Customers.CustomerID, OrderID, ContactName from
Customers, Orders " +
"where Customers.CustomerID = Orders.CustomerID and " +
"OrderID = 10248", thisConnection);

//Create DataSet to contain related data tables, rows and
columns
DataSet thisDataSet = new DataSet();

//Fill DataSet using query defined previously for DataAdapter
thisAdapter.Fill(thisDataSet, "Test");

foreach (DataRow theRow in thisDataSet.Tables["Test"].Rows)
Console.WriteLine("Row = {0}", theRow["CustomerID"] + "\t" +
theRow["ContactName"]);

thisConnection.Close();
Console.WriteLine("Program finished, press Enter/Return to
continue");
Console.Read();
}


3 Answers Posted
Ignacio Machin ( .NET/ C# MVP )'s Avatar
Ignacio Machin ( .NET/ C# MVP ) August 20th, 2008 03:15 PM
Guest - n/a Posts
#2: Re: DataSet

On Aug 20, 6:43*am, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
Quote:
Originally Posted by
Hello!
>
This is also a working console program.
It seems to me that a DataSet is actually the result of the query as in my
example
because the result is from two tables.


It will have one table only. that is the result of the query

For updates you need to define the UpdateCommand of the dataadapter.
Alberto Poblacion's Avatar
Alberto Poblacion August 20th, 2008 03:25 PM
Guest - n/a Posts
#3: Re: DataSet

"Tony Johansson" <johansson.andersson@telia.comwrote in message
news:evq69$qAJHA.3756@TK2MSFTNGP04.phx.gbl...
Quote:
Originally Posted by
This is also a working console program.
It seems to me that a DataSet is actually the result of the query as in my
example
because the result is from two tables.
Is this correct understood ?


You are returnig the result of joining two tables on the server. From
the point of view of the DataSet, you only have a single table (even though
the contents of that table came from two tables on the server, the DataSet
never sees this detail).
Quote:
Originally Posted by
What will happen if you change some row and then call the update for
DataAdapter object ?


You will get an Exception, because your DataAdapter only contains a
SelectCommand, but not the UpdateCommand, InsertCommand, and DeleteCommand
that would be necessary if you needed to send changes to the database.

Even if you were to use a SqlCommandBuilder to try to build the missing
commands, it would still fail because those commands cannot be generated
automatically when the original query is a join. You would have to build
them yourself, and then "what would happen" would depend of what you wrote
into those commands.

Tony Johansson's Avatar
Guest - n/a Posts
#4: Re: DataSet

Hello!

The object SqlCommandBuilder will automatically create an update command so
DataAdapter.update will work for update.

//Tony

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin@gmail.comskrev i
meddelandet
news:4be10dc2-1a61-450a-ad66-0c182f3744e9@y21g2000hsf.googlegroups.com...
On Aug 20, 6:43 am, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
Quote:
Originally Posted by
Hello!
>
This is also a working console program.
It seems to me that a DataSet is actually the result of the query as in my
example
because the result is from two tables.


It will have one table only. that is the result of the query

For updates you need to define the UpdateCommand of the dataadapter.


 
Not the answer you were looking for? Post your question . . .
197,020 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,020 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors