Griffin.MvcContrib.SqlServer.Localization.SqlLocalizedTypesRepository.Import C# (CSharp) Méthode

Import() public méthode

Import prompts into the repository.

Batch for inserting/updating several rows.

This method is not database engine independent (it uses SqlServers MERGE INTO). You must override it to add support for other databases than SqlServer (for instance using REPLACE INTO in MySQL)

public Import ( IEnumerable prompts ) : void
prompts IEnumerable Prompts to import
Résultat void
        public virtual void Import(IEnumerable<TypePrompt> prompts)
        {
            using (var transaction = _db.Connection.BeginTransaction())
            {
                var sql =
                    @"MERGE LocalizedTypes AS target
    USING (SELECT @lcid, @TextKey, @TypeName, @TextName, @value, @updat, @updby) AS source (LocaleId, TextKey, TypeName, TextName, Value, UpdatedAt, UpdatedBy)
    ON (target.LocaleId = source.LocaleId AND target.[Key] = source.TextKey)
    WHEN MATCHED THEN 
        UPDATE SET Value=source.Value, UpdatedAt=source.UpdatedAt, UpdatedBy=source.UpdatedBy
	WHEN NOT MATCHED THEN	
	    INSERT (LocaleId, [Key], TypeName, TextName, Value, UpdatedAt, UpdatedBy)
	    VALUES (source.LocaleId, source.TextKey, source.TypeName, source.TextName, source.Value, source.UpdatedAt, source.UpdatedBy);
";
  /*                  @"MERGE INTO LocalizedTypes 
WHERE [Key] = @TextKey AND LocaleId = @lcid
WHEN matched THEN
    UPDATE SET Value=@value, updat=@updat, updby=@updby
WHEN NOT matched THEN
     INSERT (LocaleId, [Key], TypeName, TextName, Value, UpdatedAt, UpdatedBy)
        VALUES (@lcid, @TextKey, @TypeName, @TextName, @value, @updat, @updby)";
                */
                foreach (var prompt in prompts)
                {
                    
                    //var key = new TypePromptKey(type, name);
                    using (var cmd = _db.Connection.CreateCommand())
                    {
                        cmd.Transaction = transaction;
                        cmd.AddParameter("lcid", prompt.LocaleId);
                        cmd.AddParameter("TextKey", prompt.Key.ToString());
                        cmd.AddParameter("TypeName", prompt.TypeFullName);
                        cmd.AddParameter("TextName", prompt.TextName);
                        cmd.AddParameter("value", prompt.TranslatedText);
                        cmd.AddParameter("updat", DateTime.Now);
                        cmd.AddParameter("updby", Thread.CurrentPrincipal.Identity.Name);
                        cmd.CommandText = sql;
                        cmd.ExecuteNonQuery();
                    }
                }

                transaction.Commit();
            }

        }