Sample code for 30+ languages & platforms
C# Requires Chilkat v11.6.0+

Azure Table: Atomically Merge and Delete Entities

See more Azure Table Service Examples

Reads two entities and captures their exact ETags, then submits a conditional MERGE for one and DELETE for the other. Concurrent changes fail the whole changeset. Disabled by default. Uses exactly two entities with one fixed table/partition, unique Content-IDs, CRLF multipart framing, and a byte-count check against the 4 MiB request limit. Dynamic batches must additionally preserve the 100-operation, single-partition and unique-entity constraints.

Parses nested MIME responses using Chilkat.Mime, validates inner HTTP status and Content-ID uniqueness/completeness, and requires 204 for both operations. Outer 202 alone is not success. A lost or malformed response has an uncertain outcome; inspect state before replaying. Separate batches are not atomic together.

Targets Azure Storage Table service in the public cloud, not Azure Cosmos DB for Table. Uses Chilkat 11.6.0 or later and an unlocked library. Direct data requests pin x-ms-version 2026-04-06 and OData 3.0. Shared Key examples read private qa_data/azure/storage.json with account and access_key string fields. Authentication examples explain their additional fields. Create private local directories before running; keep credentials, tokens, SAS, entity values, checkpoints and diagnostics outside public content and source control.

HTTPS certificates are verified. Transport completion and HTTP status are checked separately. Writes are not automatically retried after an uncertain outcome. Entity keys, ETags and continuation tokens remain strings. Examples operate on the existing ExampleCustomers table unless noted; review the targets before adapting them. Microsoft Entra ID with suitable Table data roles is preferred where available.

Microsoft reference.

Chilkat C# Downloads

C#
bool success = false;

//  StringBuilder compares text by value in every generated language.
Chilkat.StringBuilder textCompare = new Chilkat.StringBuilder();
bool applyChange = false;
if (applyChange != true) {
    Debug.WriteLine("This example writes an atomic transaction. Review the target and deliberately enable applyChange.");
    return;
}

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
Chilkat.JsonObject config = new Chilkat.JsonObject();
config.Strict = true;
success = config.LoadFile("qa_data/azure/storage.json");
if (success != true) {
    Debug.WriteLine("Could not load private Azure configuration.");
    return;
}

textCompare.Clear();
textCompare.SetString(config.StringOf("account"));
bool sameText1 = textCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("account") != 1) || (sameText1 == true)) {
    Debug.WriteLine("Supply the storage account name.");
    return;
}

//  access_key is the base64 account key, not a connection string or SAS.
textCompare.Clear();
textCompare.SetString(config.StringOf("access_key"));
bool sameText2 = textCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("access_key") != 1) || (sameText2 == true)) {
    Debug.WriteLine("Supply the base64 account access key.");
    return;
}

Chilkat.StringBuilder accountText = new Chilkat.StringBuilder();
success = accountText.Append(config.StringOf("account"));
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

if ((accountText.Length < 3) || (accountText.Length > 24)) {
    Debug.WriteLine("Storage account names need 3-24 lowercase letters or digits.");
    return;
}

Chilkat.StringBuilder allowedAccountChars = new Chilkat.StringBuilder();
success = allowedAccountChars.Append("abcdefghijklmnopqrstuvwxyz0123456789");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

int charIndex = 0;
while ((charIndex < accountText.Length)) {
    if (allowedAccountChars.Contains(accountText.GetRange(charIndex,1,false),true) != true) {
        Debug.WriteLine("Storage account names need lowercase letters or digits.");
        return;
    }

    charIndex = charIndex + 1;
}

Chilkat.StringBuilder endpoint = new Chilkat.StringBuilder();
success = endpoint.Append(config.StringOf("account"));
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = endpoint.Append(".table.core.windows.net");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

//  Table names contain only letters and digits and are case-insensitive.
//  Entity PartitionKey and RowKey values are case-sensitive.
string resourcePath = "/$batch";
Chilkat.Rest rest = new Chilkat.Rest();
rest.ConnectTimeoutMs = 20000;
rest.IdleTimeoutMs = 60000;
Chilkat.AuthAzureStorage azAuth = new Chilkat.AuthAzureStorage();
azAuth.Account = config.StringOf("account");
azAuth.AccessKey = config.StringOf("access_key");
azAuth.Service = "Table";
azAuth.Scheme = "SharedKey";
azAuth.XMsVersion = "2026-04-06";
success = rest.SetAuthAzureStorage(azAuth);
if (success != true) {
    Debug.WriteLine("Could not configure Shared Key signing.");
    return;
}

Chilkat.Socket connection = new Chilkat.Socket();
connection.RequireSslCertVerify = true;
connection.MaxReadIdleMs = 60000;
connection.MaxSendIdleMs = 60000;
success = connection.Connect(endpoint.GetAsString(),443,true,20000);
if (success != true) {
    Debug.WriteLine("TLS connection failed; inspect diagnostics privately.");
    return;
}

//  Do not replay a write automatically after losing its response.
success = rest.UseConnection(connection,false);
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

string responseText;
Chilkat.JsonObject result = new Chilkat.JsonObject();
result.Strict = true;
success = rest.AddHeader("DataServiceVersion","3.0;NetFx");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = rest.AddHeader("MaxDataServiceVersion","3.0;NetFx");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = rest.AddHeader("Content-Type","application/json");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = rest.AddHeader("Accept","application/json;odata=minimalmetadata");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

resourcePath = "/ExampleCustomers(PartitionKey='customers-west',RowKey='customer-001')";
responseText = rest.FullRequestNoBody("GET",resourcePath);
if (rest.LastMethodSuccess != true) {
    Debug.WriteLine("Request did not complete; inspect the target before retrying a write.");
    return;
}

if (rest.ResponseStatusCode != 200) {
    Debug.WriteLine("HTTP status: " + Convert.ToString(rest.ResponseStatusCode));
    Debug.WriteLine("Request ID: " + rest.ResponseHdrByName("x-ms-request-id"));
    //  404 is missing, 409 is conflict, and 412 is a stale ETag: none are success.
    //  Keep response bodies private. Reconcile uncertain writes before retrying.
    return;
}

success = result.Load(responseText);
if (success != true) {
    Debug.WriteLine("Expected well-formed JSON.");
    return;
}

textCompare.Clear();
textCompare.SetString(result.StringOf("PartitionKey"));
bool sameText3 = textCompare.ContentsEqual("customers-west",true);
textCompare.Clear();
textCompare.SetString(result.StringOf("RowKey"));
bool sameText4 = textCompare.ContentsEqual("customer-001",true);
if ((sameText3 != true) || (sameText4 != true)) {
    Debug.WriteLine("Unexpected entity in batch preflight.");
    return;
}

string etag1 = rest.ResponseHdrByName("ETag");
textCompare.Clear();
textCompare.SetString(etag1);
bool sameText5 = textCompare.ContentsEqual("",true);
textCompare.Clear();
textCompare.SetString(etag1);
bool sameText6 = textCompare.ContentsEqual("*",true);
if ((sameText5 == true) || (sameText6 == true)) {
    Debug.WriteLine("Batch needs exact entity ETags.");
    return;
}

rest.Disconnect(10);
success = connection.Connect(endpoint.GetAsString(),443,true,20000);
if (success != true) {
    Debug.WriteLine("TLS reconnection failed.");
    return;
}

success = rest.UseConnection(connection,false);
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

resourcePath = "/ExampleCustomers(PartitionKey='customers-west',RowKey='customer-002')";
responseText = rest.FullRequestNoBody("GET",resourcePath);
if (rest.LastMethodSuccess != true) {
    Debug.WriteLine("Request did not complete; inspect the target before retrying a write.");
    return;
}

if (rest.ResponseStatusCode != 200) {
    Debug.WriteLine("HTTP status: " + Convert.ToString(rest.ResponseStatusCode));
    Debug.WriteLine("Request ID: " + rest.ResponseHdrByName("x-ms-request-id"));
    //  404 is missing, 409 is conflict, and 412 is a stale ETag: none are success.
    //  Keep response bodies private. Reconcile uncertain writes before retrying.
    return;
}

success = result.Load(responseText);
if (success != true) {
    Debug.WriteLine("Expected well-formed JSON.");
    return;
}

textCompare.Clear();
textCompare.SetString(result.StringOf("PartitionKey"));
bool sameText7 = textCompare.ContentsEqual("customers-west",true);
textCompare.Clear();
textCompare.SetString(result.StringOf("RowKey"));
bool sameText8 = textCompare.ContentsEqual("customer-002",true);
if ((sameText7 != true) || (sameText8 != true)) {
    Debug.WriteLine("Unexpected entity in batch preflight.");
    return;
}

string etag2 = rest.ResponseHdrByName("ETag");
textCompare.Clear();
textCompare.SetString(etag2);
bool sameText9 = textCompare.ContentsEqual("",true);
textCompare.Clear();
textCompare.SetString(etag2);
bool sameText10 = textCompare.ContentsEqual("*",true);
if ((sameText9 == true) || (sameText10 == true)) {
    Debug.WriteLine("Batch needs exact entity ETags.");
    return;
}

rest.Disconnect(10);
success = connection.Connect(endpoint.GetAsString(),443,true,20000);
if (success != true) {
    Debug.WriteLine("TLS reconnection failed.");
    return;
}

success = rest.UseConnection(connection,false);
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

resourcePath = "/$batch";
//  Exactly two distinct entities in the same table and partition, by construction.
//  Dynamic plans must also enforce <=100 operations, one operation per entity, and one partition.
Chilkat.Prng random = new Chilkat.Prng();
string nonce = random.GenRandom(16,"hex");
if (random.LastMethodSuccess != true) {
    Debug.WriteLine("Could not create MIME boundaries.");
    return;
}

Chilkat.StringBuilder batchBoundary = new Chilkat.StringBuilder();
Chilkat.StringBuilder changesetBoundary = new Chilkat.StringBuilder();
success = batchBoundary.Append("batch_");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = batchBoundary.Append(nonce);
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = changesetBoundary.Append("changeset_");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = changesetBoundary.Append(nonce);
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

Chilkat.StringBuilder wire = new Chilkat.StringBuilder();
success = wire.Append("--");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append(batchBoundary.GetAsString());
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append("\r\nContent-Type: multipart/mixed; boundary=");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append(changesetBoundary.GetAsString());
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append("\r\n\r\n");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append("--");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append(changesetBoundary.GetAsString());
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append("\r\nContent-Type: application/http\r\nContent-Transfer-Encoding: binary\r\nContent-ID: 1\r\n\r\nMERGE https://");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append(endpoint.GetAsString());
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append("/ExampleCustomers(PartitionKey='customers-west',RowKey='customer-001') HTTP/1.1\r\nAccept: application/json;odata=minimalmetadata\r\nDataServiceVersion: 3.0;NetFx\r\nMaxDataServiceVersion: 3.0;NetFx\r\n");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append("If-Match: ");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append(etag1);
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append("\r\n");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

Chilkat.JsonObject item1 = new Chilkat.JsonObject();
success = item1.UpdateString("PartitionKey","customers-west");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = item1.UpdateString("RowKey","customer-001");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = item1.UpdateString("Address","Batch address 1");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append("Content-Type: application/json\r\nPrefer: return-no-content\r\n\r\n");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append(item1.Emit());
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append("\r\n");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append("--");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append(changesetBoundary.GetAsString());
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append("\r\nContent-Type: application/http\r\nContent-Transfer-Encoding: binary\r\nContent-ID: 2\r\n\r\nDELETE https://");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append(endpoint.GetAsString());
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append("/ExampleCustomers(PartitionKey='customers-west',RowKey='customer-002') HTTP/1.1\r\nAccept: application/json;odata=minimalmetadata\r\nDataServiceVersion: 3.0;NetFx\r\nMaxDataServiceVersion: 3.0;NetFx\r\n");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append("If-Match: ");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append(etag2);
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append("\r\n");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append("\r\n");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append("--");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append(changesetBoundary.GetAsString());
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append("--\r\n");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append("--");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append(batchBoundary.GetAsString());
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = wire.Append("--\r\n");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

Chilkat.BinData payloadBytes = new Chilkat.BinData();
success = payloadBytes.AppendString(wire.GetAsString(),"utf-8");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

if (payloadBytes.NumBytes > 4194304) {
    Debug.WriteLine("Batch exceeds the 4 MiB limit including multipart overhead.");
    return;
}

Chilkat.StringBuilder contentType = new Chilkat.StringBuilder();
success = contentType.Append("multipart/mixed; boundary=");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = contentType.Append(batchBoundary.GetAsString());
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = rest.AddHeader("Content-Type",contentType.GetAsString());
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = rest.AddHeader("Accept","multipart/mixed");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

responseText = rest.FullRequestString("POST",resourcePath,wire.GetAsString());
if (rest.LastMethodSuccess != true) {
    Debug.WriteLine("Request did not complete; inspect the target before retrying a write.");
    return;
}

if (rest.ResponseStatusCode != 202) {
    Debug.WriteLine("HTTP status: " + Convert.ToString(rest.ResponseStatusCode));
    Debug.WriteLine("Request ID: " + rest.ResponseHdrByName("x-ms-request-id"));
    //  404 is missing, 409 is conflict, and 412 is a stale ETag: none are success.
    //  Keep response bodies private. Reconcile uncertain writes before retrying.
    return;
}

//  Outer 202 only acknowledges the batch envelope, not transaction success.
Chilkat.StringBuilder responseMimeText = new Chilkat.StringBuilder();
success = responseMimeText.Append("Content-Type: ");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = responseMimeText.Append(rest.ResponseHdrByName("Content-Type"));
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = responseMimeText.Append("\r\nMIME-Version: 1.0\r\n\r\n");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = responseMimeText.Append(responseText);
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

Chilkat.Mime envelope = new Chilkat.Mime();
Chilkat.Mime changeset = new Chilkat.Mime();
Chilkat.Mime part = new Chilkat.Mime();
success = envelope.LoadMime(responseMimeText.GetAsString());
if (success != true) {
    Debug.WriteLine("Invalid MIME batch response; reconcile before retrying.");
    return;
}

textCompare.Clear();
textCompare.SetString(envelope.ContentType);
bool sameText11 = textCompare.ContentsEqual("multipart/mixed",true);
if ((sameText11 != true) || (envelope.NumParts != 1)) {
    Debug.WriteLine("Expected one changeset response.");
    return;
}

success = envelope.PartAt(0,changeset);
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

textCompare.Clear();
textCompare.SetString(changeset.ContentType);
bool sameText12 = textCompare.ContentsEqual("multipart/mixed",true);
if ((sameText12 != true) || (changeset.NumParts < 1) || (changeset.NumParts > 2)) {
    Debug.WriteLine("Invalid changeset response.");
    return;
}

Chilkat.Hashtable responseIds = new Chilkat.Hashtable();
Chilkat.StringBuilder inner = new Chilkat.StringBuilder();
Chilkat.StringBuilder statusLine = new Chilkat.StringBuilder();
Chilkat.Mime innerHeaders = new Chilkat.Mime();
int partIndex = 0;
while ((partIndex < changeset.NumParts)) {
    success = changeset.PartAt(partIndex,part);
    if (success != true) {
        Debug.WriteLine("Could not prepare the request.");
        return;
    }

    textCompare.Clear();
    textCompare.SetString(part.ContentType);
    bool sameText13 = textCompare.ContentsEqual("application/http",true);
    if (sameText13 != true) {
        Debug.WriteLine("Expected application/http response part.");
        return;
    }

    success = inner.SetString(part.GetBodyDecoded());
    if (success != true) {
        Debug.WriteLine("Could not prepare the request.");
        return;
    }

    if (inner.Contains("\r\n\r\n",true) != true) {
        Debug.WriteLine("Incomplete inner HTTP response.");
        return;
    }

    success = statusLine.SetString(inner.GetBefore("\r\n",true));
    if (success != true) {
        Debug.WriteLine("Could not prepare the request.");
        return;
    }

    if (statusLine.StartsWith("HTTP/1.1 ",true) != true) {
        Debug.WriteLine("Invalid inner HTTP status line.");
        return;
    }

    success = innerHeaders.LoadMime(inner.GetAsString());
    if (success != true) {
        Debug.WriteLine("Invalid inner response headers.");
        return;
    }

    string contentId = innerHeaders.GetHeaderField("Content-ID");
    textCompare.Clear();
    textCompare.SetString(contentId);
    bool sameText14 = textCompare.ContentsEqual("1",true);
    textCompare.Clear();
    textCompare.SetString(contentId);
    bool sameText15 = textCompare.ContentsEqual("2",true);
    if ((sameText14 != true) && (sameText15 != true)) {
        Debug.WriteLine("Unexpected or missing response Content-ID.");
        return;
    }

    if (responseIds.Contains(contentId) == true) {
        Debug.WriteLine("Duplicate response Content-ID.");
        return;
    }

    success = responseIds.AddStr(contentId,"seen");
    if (success != true) {
        Debug.WriteLine("Could not prepare the request.");
        return;
    }

    textCompare.Clear();
    textCompare.SetString(statusLine.GetNth(1," ",false,false));
    bool sameText16 = textCompare.ContentsEqual("204",true);
    if (sameText16 != true) {
        Debug.WriteLine("Atomic transaction did not report success. Operation Content-ID: " + contentId);
        Debug.WriteLine("Inner HTTP status: " + statusLine.GetNth(1," ",false,false));
        //  Keep the full error body private. A service-reported operation failure rolls back the changeset.
        return;
    }

    partIndex = partIndex + 1;
}

if ((changeset.NumParts != 2) || (responseIds.Contains("1") != true) || (responseIds.Contains("2") != true)) {
    Debug.WriteLine("Incomplete success response; inspect state before replaying.");
    return;
}

Debug.WriteLine("Both operations succeeded in one atomic transaction.");