Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
SqlCommandBuilder class
Message
 
 
À
14/04/2014 12:23:15
Information générale
Forum:
ASP.NET
Catégorie:
ADO.NET
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01598513
Message ID:
01598607
Vues:
20
>>>>If you use select * from myTable where Pk = @Pk, will your update command include PK column?
>>>
>>>No
>>
>>Can you show your sample code - looks like I need to set up several properties for this to work the way I need.
>
>This Select command :
SqlDataAdapter da = new SqlDataAdapter("Select Id, CustId,Method FROM SCHeaders WHERE Id=23 ", conn);
generates:
>
UPDATE [SCHeaders] SET [CustId] = @p1, [Method] = @p2 WHERE (([Id] = @p3) AND ((@p4 = 1 AND [CustId] IS NULL) OR ([CustId] = @p5)) AND ((@p6 = 1 AND [Method] IS NULL) OR ([Method] = @p7)))
>
>Using 'Select *' adds more fields but still doesn't include the PK (Id)

Your where clause includes more than just ID column.

In the meantime I changed my update method to be
  /// <summary>
        /// Update information
        /// </summary>
        /// <returns></returns>
        public Boolean UpdateRows(Boolean generateCommands = false)
        {
            Boolean results = false;
            try
            {
                if (generateCommands)
                {
                    SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
                    builder.ConflictOption = ConflictOption.OverwriteChanges;
                    builder.SetAllValues = false; // Set only changed values
                    adapter.UpdateCommand = builder.GetUpdateCommand(true);
                }
                adapter.Update(this.table);
                results = true;
            }

            catch (Exception ex)
            {
                results = false;
                Logging.LogFormat(2, ex.Message);  // at least log the problem
            }

            return results;
        }
However, I am afraid it will not work as I want as the first option requires PrimaryKey to be set. I am not exactly sure how can I do this.
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform