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