System.Data.SqlClient.SqlCommandBuilder.CreateDeleteCommand C# (CSharp) Method

CreateDeleteCommand() private method

private CreateDeleteCommand ( DataRow row, DataTableMapping tableMapping ) : SqlCommand
row System.Data.DataRow
tableMapping System.Data.Common.DataTableMapping
return SqlCommand
		private SqlCommand CreateDeleteCommand (DataRow row, DataTableMapping tableMapping) 
		{
			// If no table was found, then we can't do an delete
			if (QuotedTableName == String.Empty)
				return null;


			CreateNewCommand (ref deleteCommand);

			string command = String.Format ("DELETE FROM {0} ", QuotedTableName);
			StringBuilder columns = new StringBuilder ();
			StringBuilder whereClause = new StringBuilder ();
			string dsColumnName = String.Empty;
			bool keyFound = false;
			int parmIndex = 1;

			foreach (DataRow schemaRow in dbSchemaTable.Rows) {
				if (!IncludedInWhereClause (schemaRow)) 
					continue;

				if (whereClause.Length > 0) 
					whereClause.Append (" AND ");

				bool isKey = (bool) schemaRow ["IsKey"];
				SqlParameter parameter = null;

				if (!isKey) {
					parameter = deleteCommand.Parameters.Add (CreateParameter (parmIndex++, schemaRow));

					dsColumnName = tableMapping.ColumnMappings [parameter.SourceColumn].DataSetColumn;
					if (row != null)
						parameter.Value = row [dsColumnName, DataRowVersion.Current];
					whereClause.Append ("(");
					whereClause.Append (String.Format (clause1, GetQuotedString (parameter.SourceColumn), parameter.ParameterName));
					whereClause.Append (" OR ");
				}
				else
					keyFound = true;
					
				parameter = deleteCommand.Parameters.Add (CreateParameter (parmIndex++, schemaRow));

				dsColumnName = tableMapping.ColumnMappings [parameter.SourceColumn].DataSetColumn;
				if (row != null)
					parameter.Value = row [dsColumnName, DataRowVersion.Current];

				whereClause.Append (String.Format (clause2, GetQuotedString (parameter.SourceColumn), parameter.ParameterName));

				if (!isKey)
					whereClause.Append (")");
			}
			if (!keyFound)
				throw new InvalidOperationException ("Dynamic SQL generation for the DeleteCommand is not supported against a SelectCommand that does not return any key column information.");

			// We're all done, so bring it on home
			string sql = String.Format ("{0} WHERE ( {1} )", command, whereClause.ToString ());
			deleteCommand.CommandText = sql;
			return deleteCommand;
		}