CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    May 2008
    Posts
    7

    UpdateCommand vs. InsertCommand

    Using .NET v. 1.1.

    Why is it, that using UpdateCommand i am able to add rows to a table, but the process is painfully slow. When trying to use InsertCommand, i get a strange error {"ERROR [07002] COUNT field incorrect" }, though there isn't such field in the DataSet/Table.

    Soo, this code works:
    Code:
    public void UpdateData(SelectConn i_enConn, DataSet i_dsTmp, string i_strTable)
        {
          OdbcCommandBuilder CommandBuilder = null; 
          OdbcDataAdapter daAdapter = null; 
          OdbcConnection connConnection = null; 
          OdbcTransaction trans = null;
          
          try {
            try {
              switch (i_enConn) {
                case SelectConn.From :
                  connConnection = m_connFrom;
                  break;
    
                case SelectConn.To :
                  connConnection = m_connTo;
                  break;
              } 
            }
            catch (System.Exception exx) {
              string foobar = exx.Message.Trim();
              connConnection = null;
            }
    
            if (connConnection != null) {
              trans = connConnection.BeginTransaction(IsolationLevel.Serializable); 
              daAdapter = new OdbcDataAdapter();
              OdbcCommand cmd1 = new OdbcCommand("SELECT * FROM " + i_strTable, connConnection, trans);
              daAdapter.SelectCommand = cmd1;
              CommandBuilder = new OdbcCommandBuilder(daAdapter); 
    
              daAdapter.UpdateCommand = CommandBuilder.GetUpdateCommand();
              
              OdbcCommand cmd2 = new OdbcCommand(daAdapter.UpdateCommand.CommandText, connConnection, trans);
              
              daAdapter.UpdateCommand = cmd2;
              daAdapter.Update(i_dsTmp);  
              trans.Commit();
            } 
          }
          catch (System.Exception ex) {
            throw ex;
          }
        }
    But this one doesn't:
    Code:
    public void UpdateData(SelectConn i_enConn, DataSet i_dsTmp, string i_strTable)
        {
          OdbcCommandBuilder CommandBuilder = null; 
          OdbcDataAdapter daAdapter = null; 
          OdbcConnection connConnection = null; 
          OdbcTransaction trans = null;
          
          try {
            try {
              switch (i_enConn) {
                case SelectConn.From :
                  connConnection = m_connFrom;
                  break;
    
                case SelectConn.To :
                  connConnection = m_connTo;
                  break;
              } 
            }
            catch (System.Exception exx) {
              string foobar = exx.Message.Trim();
              connConnection = null;
            }
    
            if (connConnection != null) {
              trans = connConnection.BeginTransaction(IsolationLevel.Serializable); 
              daAdapter = new OdbcDataAdapter();
              OdbcCommand cmd1 = new OdbcCommand("SELECT * FROM " + i_strTable, connConnection, trans);
              daAdapter.SelectCommand = cmd1;
              CommandBuilder = new OdbcCommandBuilder(daAdapter); 
    
              daAdapter.InsertCommand = CommandBuilder.GetInsertCommand();
              OdbcCommand cmd2 = new OdbcCommand(daAdapter.InsertCommand.CommandText, connConnection, trans);
              daAdapter.InsertCommand = cmd2;
              daAdapter.Update(i_dsTmp);  //{"ERROR [07002] COUNT field incorrect" }
              trans.Commit();
            } 
          }
          catch (System.Exception ex) {
            throw ex;
          }
        }
    Last edited by cokelite; May 14th, 2008 at 06:14 AM.

  2. #2
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: UpdateCommand vs. InsertCommand

    Update is not designed for insert.. it's designed for update, so your entire post doesnt make much sense.

    What DB are you using?
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  3. #3
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: UpdateCommand vs. InsertCommand

    Your code:
    Code:
    ... 
    daAdapter.InsertCommand = CommandBuilder.GetInsertCommand();
    connConnection, trans); // whats this ?
    OdbcCommand cmd2 = new OdbcCommand(daAdapter.InsertCommand.CommandText, connConnection, trans);
    // look into the debugger whats CommandText Value shows up here 
    daAdapter.InsertCommand = cmd2;
    daAdapter.Update(i_dsTmp); //{"ERROR [07002] COUNT field incorrect" }
    It seems me there is somehing lost by transfering it to here. ( red line )
    `Regarding debugging: look what command text shows up
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  4. #4
    Join Date
    May 2008
    Posts
    7

    Re: UpdateCommand vs. InsertCommand

    For cjard's information:
    "DataAdapter.Update Method
    Calls the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified DataSet from a DataTable named 'Table'."
    The DataSet is generated from SqlServer and is written to SolidServer.

    For JonnyPoet:
    The resulting CommandText seems to be valid.
    "INSERT INTO TableX(Field1, Field2, Field3 ) VALUES ( ? , ? , ?)"

    (connConnection, trans); // whats this ? That was a typo in my post, sorry.
    Last edited by cokelite; May 13th, 2008 at 01:23 AM.

  5. #5
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: UpdateCommand vs. InsertCommand

    Quote Originally Posted by cokelite
    For cjard's information:

    The DataSet is generated from SqlServer and is written to SolidServer.

    For JonnyPoet:
    The resulting CommandText seems to be valid.
    "INSERT INTO TableX(Field1, Field2, Field3 ) VALUES ( ? , ? , ?)"

    (connConnection, trans); // whats this ? That was a typo in my post, sorry.
    Trerrornummber 7007 says that the number of fields is incorrect so have you added all parameters correctly ? or are there fields missing which needs to get data ( must be written ) and you havn't inserted their data? I would look for nummber of fileds needed to insert and if all the data in the params are available
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  6. #6
    Join Date
    May 2008
    Posts
    7

    Re: UpdateCommand vs. InsertCommand

    You are right, cmd1.Parameters.Count == 0. I thought the parameters were generated by SelectCommand.

    This method i'm fighting with needs to be generic. Any idea how i'll get the parameters populated automatically ?

    The dataset i_dsTmp and the table i_strTable have identical structure, except that the table is empty when daAdapter.Update starts.

  7. #7
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: UpdateCommand vs. InsertCommand

    Quote Originally Posted by cokelite
    You are right, cmd1.Parameters.Count == 0. I thought the parameters were generated by SelectCommand.

    This method i'm fighting with needs to be generic. Any idea how i'll get the parameters populated automatically ?

    The dataset i_dsTmp and the table i_strTable have identical structure, except that the table is empty when daAdapter.Update starts.
    Its useless to try to insert an empty Tabels Data What do you mean with 'generic'? I cannot see any 'T' params here. Or do you mean it has to work independent of which data to be inserted. Tell a bit more what you want to achieve because often when you are stuck its simple the wrong approach. Have you thought about using an Interface for parts which should be usable in a more 'generic' way?

    Re automatically populated ? Where are the data stored which should be inserted to the database. Is there a specific DataSet ?
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  8. #8
    Join Date
    May 2008
    Posts
    7

    Re: UpdateCommand vs. InsertCommand

    When calling my method i'm passing it as parameters the connection to database, the DataSet filled with data and a name of an empty database table into which i want to copy the data from DataSet.
    Code:
    public void UpdateData(SelectConn i_enConn, DataSet i_dsTmp, string i_strTable)
    And yes, i do mean it has to work independent of which data to be inserted. The tables vary but the DataSet always has the same field names of same type as the table.

    When using UpdateCommand (see my first code example in the first post) it works and i don't have to specify the parameters, they are generated automatically but the performance is poor.
    Code:
              daAdapter.UpdateCommand = CommandBuilder.GetUpdateCommand();
              OdbcCommand cmd2 = new OdbcCommand(daAdapter.UpdateCommand.CommandText, connConnection, trans);
              daAdapter.UpdateCommand = cmd2;
              daAdapter.Update(i_dsTmp);
    But when i try to do the same with InsertCommand in hope of better performance, i end up dealing with missisng parameter definitions, or something..
    Code:
              daAdapter.InsertCommand = CommandBuilder.GetInsertCommand();
              OdbcCommand cmd2 = new OdbcCommand(daAdapter.InsertCommand.CommandText, connConnection, trans);
              daAdapter.InsertCommand = cmd2;
              daAdapter.Update(i_dsTmp);  //{"ERROR [07002] COUNT field incorrect" }

  9. #9
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: UpdateCommand vs. InsertCommand

    Hi !

    IMHO you simple need to Fill the dataadapter BEFORE you are using insert
    Code:
    dataAdapter.Fill(i_dsTmp);
    And after this using your Insert cmd. Because doing this in the update cycle is to late. There was nothing to insert obviously so it gets the error.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  10. #10
    Join Date
    May 2008
    Posts
    7

    Re: UpdateCommand vs. InsertCommand

    Uhh, it sounded promising but unfortunately filling DataAdapter before defining CommandBuilder didn't make any difference, i still get the same error
    Code:
    daAdapter.Update(i_dsTmp);  //{"ERROR [07002] COUNT field incorrect" }

  11. #11
    Join Date
    May 2008
    Posts
    7

    Re: UpdateCommand vs. InsertCommand

    I believe i kinda figured it out. One must define the InsertCommand but then assign that to UpdateCommand. BUT.. the performance still sucks.

    Here's the working version.
    Code:
        public void UpdateData(SelectConn i_enConn, DataSet i_dsTmp, string i_strTable)
        {
          OdbcCommandBuilder CommandBuilder = null; 
          OdbcDataAdapter daAdapter = null; 
          OdbcConnection connConnection = null; 
          OdbcTransaction trans = null;
          try {
            try {
              switch (i_enConn) {
                case SelectConn.From :
                  connConnection = m_connFrom;
                  break;
                case SelectConn.To :
                  connConnection = m_connTo;
                  break;
              } 
            }
            catch (System.Exception exx) {
              string foobar = exx.Message.Trim();
              connConnection = null;
            }
    
            if (connConnection != null) {
              trans = connConnection.BeginTransaction(IsolationLevel.Serializable); 
              daAdapter = new OdbcDataAdapter();
              daAdapter.SelectCommand = new OdbcCommand("SELECT * FROM " + i_strTable, connConnection, trans);
              CommandBuilder = new OdbcCommandBuilder(daAdapter); 
              daAdapter.UpdateCommand = CommandBuilder.GetInsertCommand();
              daAdapter.Update(i_dsTmp); 
              trans.Commit();
            } 
          }
          catch (System.Exception ex) {
            throw ex;
          }
        }
    Last edited by cokelite; May 14th, 2008 at 06:13 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured