Azure表存储错误请求-查询语法错误

Kir*_*eed 1 azure-storage

以下用于工作。

      public void CreateTableIfMissing()
    {
        var info = new StorageInfo(); // initialized with tablename and connectionstring
        var storageAccount = CloudStorageAccount.Parse(info.ConnectionString);
        var tableClient = storageAccount.CreateCloudTableClient();
        var table = tableClient.GetTableReference(info.TableName);
        try
        {
            table.CreateIfNotExists();
            var batchOperation = new TableBatchOperation();
            var s = DateTime.Now.ToString();
            var entry = new TableEntity("partkey"+s,"rowkey"+s);
            batchOperation.Insert(entry);
            table.ExecuteBatch(batchOperation);

        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }
Run Code Online (Sandbox Code Playgroud)

错误信息是

{Microsoft.WindowsAzure.Storage.StorageException: 
ErrorCode "InvalidInput"
Element 0 in the batch returned an unexpected response code.
StatusMessage:0:Bad Request - Error in query syntax
Run Code Online (Sandbox Code Playgroud)

该表用于通过带有Azure同步的Serilog进行错误日志记录。我可以看到,如果我连接到Azure Storage Explorer,它仍在获取日志记录。

我尚未更改连接字符串

[更新]

我正在尝试一项手术,但遇到了麻烦

'TableOperation' does not contain a constructor that takes 2 arguments
Cannot access internal constructor 'TableOperation' here
Run Code Online (Sandbox Code Playgroud)

单次操作

更多信息

[更新]

如果我遵循Ivan的建议但忽略了ToString(“ o”)参数,则错误为

ErrorMessage:The 'PartitionKey' parameter of value 'partkey3/7/2019 8:33:25 PM' is out of range.
Run Code Online (Sandbox Code Playgroud)

这很有道理。

我不知道为什么能奏效!

Iva*_*ang 5

更新:

对于先前代码中的错误消息(不是更新代码):

{Microsoft.WindowsAzure.Storage.StorageException: 
ErrorCode "InvalidInput"
Element 0 in the batch returned an unexpected response code.
StatusMessage:0:Bad Request - Error in query syntax
Run Code Online (Sandbox Code Playgroud)

原因是表存储中的partkey和rowkey不接受“ /”之类的字符。当您使用包含字符“ /”的DateTime.Now.ToString()作为partkey和rowkey的后缀时,将导致错误。请格式化日期时间并删除“ /”,您可以DateTime.Now.ToString("o")在代码中使用它(或其他正确格式)。

对于更新的代码:

错误是因为TableOperation class没有构造函数(参数或无参数)。您可以导航至TableOperation类,并查看其用法。

在此处输入图片说明

在这种情况下,您应该使用静态Insert method样式,var op = TableOperation.Insert(entry)而不是var op = new TableOperation(entry,TableOperationType.Insert)

另外,您还需要了解一件事,表存储中的partkey和rowkey不接受“ /”之类的字符,因此当您使用datetime.nowpartkey和rowkey的后缀时,应该使用var s = DateTime.Now.ToString("o")。否则会导致错误。

示例代码对我来说很好用:

        public void CreateTableIfMissing()
        {
           var info = new StorageInfo(); // initialized with tablename and connectionstring
           var storageAccount = CloudStorageAccount.Parse(info.ConnectionString);
           var tableClient = storageAccount.CreateCloudTableClient();
           var table = tableClient.GetTableReference(info.TableName);

            try
            {
                table.CreateIfNotExists();

                var s = DateTime.Now.ToString("o");
                var entry = new TableEntity("partkey" + s, "rowkey" + s);
                var op = TableOperation.Insert(entry);
                table.Execute(op);               

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Run Code Online (Sandbox Code Playgroud)

有关表存储的更多代码示例,请参考本文

  • 这是一个很好的代码示例,但它并没有完全回答问题。:) @kirsten-greed ,请按照代码操作并查看 `StorageException.RequestInformation.ExtendedErrorInformation` 中的错误详细信息。 (2认同)