VERTRIEB: 1-800-867-1380

CloudTableClient.CreateTableIfNotExist-Methode

Creates the table if it does not already exist.

Namespace: Microsoft.WindowsAzure.StorageClient
Assembly: Microsoft.WindowsAzure.StorageClient (in microsoft.windowsazure.storageclient.dll)

'Usage
Dim instance As CloudTableClient
Dim tableName As String
Dim returnValue As Boolean

returnValue = instance.CreateTableIfNotExist(tableName)

public bool CreateTableIfNotExist (
	string tableName
)
public boolean CreateTableIfNotExist (
	String tableName
)
public function CreateTableIfNotExist (
	tableName : String
) : boolean

Parameter

tableName

The table name.

Rückgabewert

true if table was created; otherwise, false.

The following code example creates a table if it does not already exist, then inserts a new entity.

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.CreateTableIfNotExist(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; }
        }
    }
}


Alle öffentlichen statischen (freigegebenen in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Entwicklungsplattformen

Windows XP Home Edition, Windows XP Professional, Windows Server 2003, Windows Server 2008 und Windows 2000

Zielplattformen

Fanden Sie dies hilfreich?
(1500 verbleibende Zeichen)
Vielen Dank für Ihr Feedback.

Community-Beiträge

Anzeigen:
© 2014 Microsoft