Exportar (0) Imprimir
Expandir todo
EN
Este contenido no se encuentra disponible en su idioma, pero aquí tiene la versión en inglés.

CloudTableClient.CreateTable Method

Updated: April 24, 2013

[This topic is part of the Microsoft Azure Storage Client Library 1.7, which has been deprecated. See Storage Client Library for the latest version.]

Creates a table with specified name.

Namespace: Microsoft.WindowsAzure.StorageClient
Assembly: Microsoft.WindowsAzure.StorageClient (in Microsoft.WindowsAzure.StorageClient.dll)

'Uso
Dim instance As CloudTableClient
Dim tableName As String

instance.CreateTable(tableName)

public void CreateTable (
	string tableName
)

Parameters

tableName

Type: System.String

The table name.

The following code example creates a table and inserts an entity to it.

using System;
using System.Configuration;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
namespace TableSamples
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get connection string from a configuration file.
            CloudStorageAccount storageAccount = 
                CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageAccountConnectionString"]);
            CreateTableAndAddData(storageAccount);
        }
        static void CreateTableAndAddData(CloudStorageAccount storageAccount)
        {
            // Create service client for credentialed access to the Table service.
            CloudTableClient tableClient = new CloudTableClient(storageAccount.TableEndpoint.ToString(), 
                storageAccount.Credentials);
            string tableName = "Products";
            try
            {
                // Create a new table.
                tableClient.CreateTable(tableName);
                // Get data context.
                TableServiceContext context = tableClient.GetDataServiceContext();
                // Create the new entity.
                ProductEntity entity = new ProductEntity();
                // Populate the entity's properties.
                entity.ProductName = "Gadget";
                entity.Category = "Widgets";
                entity.Price = 19.99;
                entity.InStock = true;
                entity.DateAdded = DateTime.Now;
                entity.Quantity = 50;
                // Partition key is the category name.
                entity.PartitionKey = entity.Category;
                // Row key is a GUID.
                entity.RowKey = Guid.NewGuid().ToString();
                // Add the entity.
                context.AddObject(tableName, entity);
                // Save changes to the service.
                context.SaveChanges();
            }
            catch (StorageClientException e)
            {
                Console.WriteLine("Error: {0}", e.Message);
                Console.WriteLine("Extended error info: {0} : {1}", 
                    e.ExtendedErrorInformation.ErrorCode, 
                    e.ExtendedErrorInformation.ErrorMessage);
            }
        }
        // Define a class that represents an entity.
        class ProductEntity : TableServiceEntity
        {
            public ProductEntity()
            {
            }
            public string ProductName { get; set; }
            public string Category { get; set; }
            public double Price { get; set; }
            public bool InStock { get; set; }
            public DateTime DateAdded { get; set; }
            public Int32 Quantity { get; set; }
        }
    }
}

For guidance about valid table names, see the "Table Names" section in Understanding the Table Service Data Model.

If a table with the specified name already exists, the CreateTable method throws a StorageClientException.


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Development Platforms

Windows Vista, Windows 7, Windows Server 2008, Windows 8.1, Windows Server 2012 R2, Windows 8 and Windows Server 2012

 

Mostrar:
© 2014 Microsoft