Windows Azure Series--CRUD Operations for Azure table

Source: Internet
Author: User
Tags bulk insert

1. First or in accordance with the pkg of Azure Storage:


2. Azurestorage Explorer can be downloaded to manage the status of Azure storage
https://azurestorageexplorer.codeplex.com/

If there are many files to upload to the blob, it is recommended to use the Azure version of Cloudberry.


To test the entity class:


public class student:tableentity    {public        Student ()        {        } public        Student (string ID, string name, DateTime joineddate, bool hasgraduated)        {            id = ID;            name = name;            Joindate = joineddate;            hasgraduated = Hasgraduated.tostring ();            RowKey = ID;            PartitionKey = ID;        }        public string Id {get; set;}        public string Name {get; set;}        Public DateTime joindate {get; set;}        public string hasgraduated {get; set;}    }



The point to be aware of is to inherit from Tableentity.




Storageaccount Properties:


private static Cloudstorageaccount Storageaccount        {            get            {                var creds = new Storagecredentials ( AccountName, Key);                var account = new Cloudstorageaccount (creds, usehttps:true);                return account;            }        }




AccountName and key need to be replaced by your own.


1. Create a table (TableName is replaced by a specific case):


public static void Createtableifnotexsit ()        {            Cloudtableclient tableclient = Storageaccount.createcloudtableclient ();            Create the table if it doesn ' t exist.            cloudtable table = tableclient.gettablereference (TableName);            Table. Createifnotexists ();        }






2. Inserting records


public static void Insert (Student s)        {            cloudtableclient tableclient = storageaccount.createcloudtableclient () ;            Create the Cloudtable object that represents the "student" table.            cloudtable table = tableclient.gettablereference (TableName);            Create the tableoperation that inserts the entity.            Tableoperation insertoperation = Tableoperation.insert (s);            Execute the insert operation.            Table. Execute (insertoperation);        }




BULK Insert-Partitionkey needs to be consistent (Partitionkey is optimized for queries, as you can see here: https://msdn.microsoft.com/en-us/library/azure/hh508997.aspx)


<summary>//        if want to insert in the same patch must has the same partition key///        </summary>< c2/>///<param name= "Students" ></param> public        static void Insert (ienumerable<student> Students)        {            var tableclient = storageaccount.createcloudtableclient ();            Create the Cloudtable object that represents the "student" table.            var table = tableclient.gettablereference (TableName);            Create the batch operation.            var batchoperation = new Tablebatchoperation ();            Add both entities to the batch insert operation.            foreach (var s in students)            {                Batchoperation.insert (s);            }            Execute the batch operation.            Table. ExecuteBatch (batchoperation);        }



Inquire


public static ienumerable<student> Getbyname (string name)        {            var tableclient = Storageaccount.createcloudtableclient ();            var table = tableclient.gettablereference (TableName);            var query = new tablequery<student> (). Where (tablequery.generatefiltercondition ("name", Querycomparisons.equal, name));            foreach (Student entity in table. ExecuteQuery (query))            {                yield return entity;            }        }






Combination Criteria Query


public static ienumerable<student> Getgraduatedstudentsbyname (string name, BOOL graduated) {var t            Ableclient = Storageaccount.createcloudtableclient ();            Create the Cloudtable object that represents the "student" table.            var table = tableclient.gettablereference (TableName);            Create the table query. var rangequery = new tablequery<student> (). Where (Tablequery.combinefilters (tablequery.generatefiltercondition ("Name", Querycompar Isons.  Equal, name), Tableoperators.and, Tablequery.generatefiltercondition ("hasgraduated", Querycomparisons.equal,graduated?            "True": "False")));            Loop through the results, displaying information about the entity. foreach (Student entity in table.            ExecuteQuery (rangequery)) {yield return entity; }        }




Get a single record


public static Student single (string id)        {            Cloudtableclient tableclient = Storageaccount.createcloudtableclient ();            Create the Cloudtable object that represents the "student" table.            cloudtable table = tableclient.gettablereference (TableName);            Tableoperation retrieveoperation = tableoperation.retrieve<student> (ID, id);            Execute the retrieve operation.            var ret = table. Execute (retrieveoperation);            if (ret = = null)            {                return null;            }            Return (Student) ret. Result;        }






Modify a record


public static void Update (Student Student) {var tableclient = storageaccount.createcloudtableclient ();            Create the Cloudtable object that represents the table.            var table = tableclient.gettablereference (TableName);            Create a retrieve operation that takes a entity. var retrieveoperation = tableoperation.retrieve<student> (Student. Id, student.            ID);            Execute the operation. var retrievedresult = table.            Execute (retrieveoperation);            Assign the result to a object.            var updateentity = (Student) Retrievedresult.result; if (updateentity! = null) {Updateentity.name = student.                Name; Updateentity.joindate = student.                Joindate; updateentity.hasgraduated = student.                                hasgraduated;                var updateoperation = Tableoperation.replace (updateentity);                Execute the operation. TAble.            Execute (updateoperation); }        }




Deleting records


public static void Delete (string id)        {            var tableclient = storageaccount.createcloudtableclient ();            var table = tableclient.gettablereference (TableName);            var retrieveoperation = tableoperation.retrieve<student> (id,id);            var retrievedresult = table. Execute (retrieveoperation);            var deleteentity = (Student) retrievedresult.result;            Create the Delete tableoperation.            if (deleteentity! = null)            {                var deleteoperation = Tableoperation.delete (deleteentity);                Execute the operation.                Table. Execute (deleteoperation);            }        }




This article is just to demonstrate Azuretable's crud operations, and in a specific scenario, you should not use static methods, which should be objects.


The Complete test code:




[TestMethod] public void Azuretable_crud_test () {azuretablecrud.createtableifnotexsit ();            Azuretablecrud.insert (New Student ("1", "Stua", DateTime.Parse ("2015-3-11 12:00:00 PM"), false));            Azuretablecrud.insert (New Student ("2", "StuB", DateTime.Parse ("2015-3-11 12:00:00 PM"), false));            Azuretablecrud.insert (New Student ("3", "StuB", DateTime.Parse ("2015-3-11 12:00:00 PM"), false));            Azuretablecrud.insert (New Student ("4", "Gra_stu_a", DateTime.Parse ("2011-3-11 12:00:00 PM"), true));            Azuretablecrud.insert (New Student ("5", "Gra_stu_a", DateTime.Parse ("2011-3-11 12:00:00 PM"), true)); -get many var Stu = Azuretablecrud.getbyname ("StuB").            ToList (); Assert.istrue (Stu.            Count = = 2); Assert.istrue (Stu[0].            Name = = "StuB"); Assert.istrue (Stu[1].            Name = = "StuB");            -Get single var Stua = Azuretablecrud.single ("1"); Assert.istrue (STUA.NAme = = "Stua"); -Combine condition get many var graduates = Azuretablecrud.getgraduatedstudentsbyname ("Gra_stu_a", true).            ToList (); Assert.istrue (graduates.            Count = = 2); Assert.istrue (Graduates[0].            Id = = "4"); Assert.istrue (Graduates[1].            Id = = "5");            Azuretablecrud.update (New Student ("2", "Stub_modified", DateTime.Parse ("2015-4-11 12:00:00"), false));            var StuB = Azuretablecrud.single ("2");            Assert.istrue (Stub.name = = "Stub_modified");            Assert.istrue (StuB.JoinDate.Month = = 4);            Azuretablecrud.delete ("4");            var Trygetforth = Azuretablecrud.single ("4");        Assert.isnull (Trygetforth); }




To view the results in Azure Explorer:



Windows Azure Series--CRUD Operations for Azure table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.