DataSet

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Johansson

    DataSet

    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=north wind");

    //Create DataAdapter object
    SqlDataAdapter thisAdapter = new SqlDataAdapter(
    "Select Customers.Custo merID, OrderID, ContactName from
    Customers, Orders " +
    "where Customers.Custo merID = Orders.Customer ID 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.Fil l(thisDataSet, "Test");

    foreach (DataRow theRow in thisDataSet.Tab les["Test"].Rows)
    Console.WriteLi ne("Row = {0}", theRow["CustomerID "] + "\t" +
    theRow["ContactNam e"]);

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


  • Ignacio Machin ( .NET/ C# MVP )

    #2
    Re: DataSet

    On Aug 20, 6:43 am, "Tony Johansson" <johansson.ande rs...@telia.com >
    wrote:
    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.

    Comment

    • Alberto Poblacion

      #3
      Re: DataSet

      "Tony Johansson" <johansson.ande rsson@telia.com wrote in message
      news:evq69$qAJH A.3756@TK2MSFTN GP04.phx.gbl...
      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).
      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 SqlCommandBuild er 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.

      Comment

      • Tony Johansson

        #4
        Re: DataSet

        Hello!

        The object SqlCommandBuild er will automatically create an update command so
        DataAdapter.upd ate will work for update.

        //Tony

        "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin @gmail.comskrev i
        meddelandet
        news:4be10dc2-1a61-450a-ad66-0c182f3744e9@y2 1g2000hsf.googl egroups.com...
        On Aug 20, 6:43 am, "Tony Johansson" <johansson.ande rs...@telia.com >
        wrote:
        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.


        Comment

        Working...