Example #1
0
        /// <summary>
        /// Performs a lookup for the PK value of a given entity by querying the table using a specific field mapping
        /// </summary>
        /// <param name="connection">
        /// The database connection to the microsoft access database file.
        /// </param>
        /// <param name="description">
        /// The source description including the table name to be used.
        /// </param>
        /// <param name="fieldMapping">
        /// The field mapping.
        /// </param>
        /// <param name="contact">
        /// The contact for find.
        /// </param>
        /// <returns>
        /// the primary key value of the entity or null if not in database
        /// </returns>
        private static string GetPrimaryKeyForEntity(
            OleDbConnection connection, SourceDescription description, ColumnDefinition fieldMapping, StdContact contact)
        {
            var value = FormatForDatabase(Tools.GetPropertyValue(contact, fieldMapping.Selector), fieldMapping);

            if (value == SqlDatabaseNullString)
            {
                return(null);
            }

            using (var cmd = connection.CreateCommand())
            {
                var text = string.Format(
                    CultureInfo.InvariantCulture,
                    SqlStatementSelectPk,
                    description.GetPrimaryKeyName(),
                    description.MainTable,
                    fieldMapping.Title,
                    FormatForDatabase(Tools.GetPropertyValue(contact, fieldMapping.Selector), fieldMapping));

                cmd.CommandText = text;
                var result = cmd.ExecuteScalar();
                return((result ?? string.Empty).ToString());
            }
        }