protected Boolean ProcessDataTable(SqlCommand toSqlCommand, Boolean generateCommands = false) { Boolean results = false; try { this.table = new DataTable(); this.adapter = new SqlDataAdapter(toSqlCommand); this.adapter.SelectCommand = toSqlCommand; this.adapter.Fill(this.table); if (generateCommands) { SqlCommandBuilder builder = new SqlCommandBuilder(adapter); adapter.UpdateCommand = builder.GetUpdateCommand(true); } results = true; } catch (Exception ex) { results = false; Logging.LogFormat(2, ex.Message); // at least log the problem } return results; }and its update method:
/// <summary> /// Update information /// </summary> /// <returns></returns> public Boolean UpdateRows() { Boolean results = false; try { adapter.Update(this.table); results = true; } catch (Exception ex) { results = false; Logging.LogFormat(2, ex.Message); // at least log the problem } return results; }So, I understand it will be a good idea to move the generate update command into the UpdateRows method instead. How would it figure out the PrimaryKey?
String cParameter = ("SWIPE_NO" == this.cWhichKey) ? this.cCardNo : this.nPassNo.ToString(); String cSql = string.Format("SELECT * FROM dbo.{0} WHERE {1} = @param", this.passTable.ToString().ToLower(), this.cWhichKey.ToLower()); using (SqlCommand sqlCommand = new SqlCommand(cSql, database.sqlConnection)) { if (this.bIsCard || 0 != this.nPassNo) { String cMessage = ""; if (database.GetColumnType(this.passTable.ToString(), this.cWhichKey, ref cMessage).IsTypeNumericOrBit()) sqlCommand.Parameters.Add("@param", SqlDbType.BigInt).Value = this.nPassNo; else sqlCommand.Parameters.Add("@param", SqlDbType.VarChar, 22).Value = cParameter; } this.dthPass.Populate(sqlCommand);So, the call doesn't really know which table I am going to update.