1

I have some code in C# I'm trying to move to a stored procedure - Sql Azure does not like multiple calls. So everything needs to get moved to a proc.

Here's the C# code:

    List<string> addList = new List<string>(EmailDomains);
    List<string> deleteList = new List<string>();
    using (IDbCommand cmd = conn.CreateCommand())
    {
        cmd.Transaction = transaction;
        cmd.CommandText = "select * from CompanyDomains where CompanyDomains.CompanyId = @CompanyId";
        DbUtilities.AddParam(cmd, "@CompanyId", CompanyId);
        using (IDataReader reader = cmd.ExecuteReader())
        {
            int ordinal = reader.GetOrdinal("EmailDomain");
            while (reader.Read())
            {
                string domain = reader.GetString(ordinal).Trim();
                if (! addList.Remove(domain))
                {
                    deleteList.Add(domain);
                }
            }
        }
    }
    foreach (string domain in deleteList)
    {
        using (IDbCommand cmd = conn.CreateCommand())
        {
            cmd.Transaction = transaction;
            cmd.CommandText =
                "delete from CompanyDomains where CompanyDomains.CompanyId = @CompanyId and CompanyDomains.EmailDomain = @EmailDomain";
            DbUtilities.AddParam(cmd, "@CompanyId", CompanyId);
            DbUtilities.AddParam(cmd, "@EmailDomain", domain);
            cmd.ExecuteNonQuery();
        }
    }

    // deleted domains get added/updated to DeletedCompany
    foreach (string domain in deleteList)
    {
        DeletedCompany.InsertOrUpdate(conn, transaction, domain, CompanyName);
    }

    foreach (string domain in addList)
    {
        using (IDbCommand cmd = conn.CreateCommand())
        {
            cmd.Transaction = transaction;
            cmd.CommandText = "insert into CompanyDomains (CompanyId, EmailDomain) " +
                              "values (@CompanyId, @EmailDomain)";
            DbUtilities.AddParam(cmd, "@CompanyId", CompanyId);
            DbUtilities.AddParam(cmd, "@EmailDomain", domain.Trim());

            cmd.ExecuteNonQuery();
        }
    }

    // added domains get removed from DeletedCompany
    foreach (string domain in addList)
    {
                    using (IDbCommand cmd = conn.CreateCommand())
    {
        if (transaction != null)
            cmd.Transaction = transaction;
        cmd.CommandText = "delete from DeletedCompany where DeletedCompany.Domain = @Domain";
        DbUtilities.AddParam(cmd, "@Domain", domain);
        cmd.ExecuteNonQuery();
    }

    }

What I'm totally stopped on is I don't see any equivalent of a List is SQL. Is there a way to build all this up in a list?

Or is there a delete/insert/update I can do where the select uses the list I pass in? I am passing in a list in the form of a defined type(table) that is the new list of domains. From that list:

Any domain in the passed in list that is not in CompanyDomains is added to CompanyDomains.

Any domain in the CompanyDomains table that is not in the passed in list is 1) removed from CompanyDomains and 2) added/updated to the DeletedCompany table.

Any domain that is in the liast and in the DeletedCompany table is deleted from the DeletedCompany table.

The DeletedCompany.Domain column is unique in that table. So additions to it must be an upsert.

The CompanyDomains table also has a CompanyId column and everything there is where CompanyDomains.CompanyId = @CompanyId (passed in parameter to the proc).

This is for Sql Server 2008 & Sql Azure. It does not need to run on anything else.

thanks - dave

3

1 Answer 1

1

What I'm totally stopped on is I don't see any equivalent of a List is SQL. Is there a way to build all this up in a list?

So a list in SQL Server is basically a table. You can use table-valued parameters to pass in your complete list from C#. You basically just create a table type to define that list you are passing in, see the example in the MSDN link.

You would just need to take each requirement you list in your question or that the C# is performing, and write the query to pull the correct values from your tabled-values list and create your INSERT, DELETE, and UPDATE statements based on those values.

So for

Any domain in the passed in list that is not in CompanyDomains is added to CompanyDomains.

Something like:

INSERT INTO CompanyDomains (<column list>
SELECT <column list>
FROM @TableParameter t
WHERE NOT EXISTS ( SELECT domain FROM CompanyDomains cd WHERE t.domain = cd.domain );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.