C# 4.0 で 名前付き引数と省略可能な引数が追加されました。

省略可能な引数では、既定値を指定することによって引数を省略することができ、名前付き引数では、引数名を付けることによって引数の順番を変えることができますので、下記のような感じで使うことができます。

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Hoge());
            Console.WriteLine(Hoge(x: 1));
            Console.WriteLine(Hoge(y: 2));
            Console.WriteLine(Hoge(y: 2, x: 1));

            Console.ReadLine();
        }

        static string Hoge(int x = 0, int y = 0)
        {
            return string.Format("{0}, {1}", x, y);
        }
    }

 結果

20100531

 参考

名前付き引数と省略可能な引数 (C# プログラミング ガイド)
http://msdn.microsoft.com/ja-jp/library/dd264739%28VS.100%29.aspx

Windows Azure Table での前方一致検索を行う場合、CompareToを使い検索文字列以上、検索文字列 +「'\uFFFF'」未満というクエリを実行することで可能です。

RowKeyに名前を入れる事はないかと思いますが・・・サンプルコードではRowKeyに日本語の名前入れて検索しています。

 サンプルコード

using System;
using System.Linq;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.Data.Services.Client;

namespace WebRole1.Storage
{
    public partial class table1 : System.Web.UI.Page
    {

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            CloudStorageAccount account = CloudStorageAccount.FromConfigurationSetting("StorageConnectionString");

            CloudTableClient tableClient = account.CreateCloudTableClient();
            tableClient.RetryPolicy = RetryPolicies.Retry(
                2,
                TimeSpan.FromMilliseconds(100));

                TableServiceContext svc = tableClient.GetDataServiceContext();

                string name = txtQuery.Text;
                string name_n = txtQuery.Text + '\uFFFF';

                var q = from t in svc.CreateQuery<Table1>("SampleTable")
                        where (t.PartitionKey == "Sample") &&
                              (t.RowKey.CompareTo(name) >= 0 && t.RowKey.CompareTo(name_n) < 0)
                        select t;

                GridView1.DataSource = q.ToList();
                GridView1.DataBind();
        }

        private class Table1 : TableServiceEntity
        {
            public Table1(string name)
            {
                base.PartitionKey = "Sample";
                base.RowKey = name;
            }

            public Table1() : base() { }
        }

    }
}

 

結果

100529-02

環境:Windows 7, Windows Azure SDK v1.1

Development Storage で使用するSQL Server インスタンスを 既定のインスタンス に変更したいなあ。と思っていったら、MSDN Forums にありました。

MSDN Forums - AnswerDefault SQL Server instance for the local Development Storage

 

Development Storage で使用する SQLインスタンスやアカウントは、DSInit.exe で変更することが可能です。

DSInit [/sqlinstance:] [/forceCreate] [/user:<Windows account name>]

例)ローカルPCの既定のインスタンスを指定する場合

DSInit.exe /sqlInstance:.

 

Windows Azure SDK v1.1 のデフォルトでは Development Storage で使用するSQL Server インスタンスの指定は「localhost\SQLExpress」になっています。

Windows Azure Table に日本語のデータを登録すると例外が発生するので、Base64でエンコードしていたのですが、先日行われたVSUG DAY で 「本番環境では登録できる」という感じの話が聞こえてきたので確かめてみました。

下記のようなコードを実行したところ

using System;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.Data.Services.Client;

namespace WebRole1.Storage
{
    public partial class sample3 : System.Web.UI.Page
    {
        private CloudStorageAccount account = CloudStorageAccount.FromConfigurationSetting("StorageConnectionString");

        protected void Page_Load(object sender, EventArgs e)
        {
            CloudTableClient tableClient = account.CreateCloudTableClient();
            tableClient.RetryPolicy = RetryPolicies.Retry(
                2,
                TimeSpan.FromMilliseconds(100));

            tableClient.CreateTableIfNotExist("SampleTable");

            Table1 t1 = new Table1();
            t1.Name = "田中太郎";

            TableServiceContext svc = tableClient.GetDataServiceContext();
            svc.AddObject("SampleTable", t1);
            try
            {
                svc.SaveChanges();
            }
            catch (DataServiceRequestException ex)
            {
                TextBox1.Text = "失敗\r\n" + ex.ToString();
                return;
            }
            Response.Write("成功");
        }

        private class Table1 : TableServiceEntity
        {
            public string Name { get; set; }

            public Table1()
            {
                base.PartitionKey = "Sample";
                base.RowKey = Guid.NewGuid().ToString(); ;
            }

        }
    }
}

 

Development Storageでは、やはり例外が発生しますが

100510-001

 

本番環境では、日本語のデータも登録できました。

100510-003

Windows Azure Drive を使う で Azure Drive を作成したので、Azure Drive で SQLiteを試してみました。

例外処理をしていないこともありますが、CloudDrive のマウントを行っているくらいで、.NETからSQLiteを使う場合と、ほとんど同じような感じです。

※サンプルコードは、Windows Azure Drive を使う でAzure Driveを作成済みの環境です。

準備

.NETで書き直しされたSQLite用クラスライブラリである System.Data.SQLite を使っていますので、System.Data.SQLite をダウンロードして、参照設定に追加します。

System.Data.SQLite
http://sqlite.phxsoftware.com/

 

サンプルコード(テーブル作成)

using System;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;

using System.Data.SQLite;

namespace WebRole1.Storage
{
    public partial class sample2 : System.Web.UI.Page
    {
        private CloudStorageAccount account = CloudStorageAccount.FromConfigurationSetting("StorageConnectionString");

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            int cacheSize = 500;

            LocalResource localCache = RoleEnvironment.GetLocalResource("LocalStorage1");
            CloudDrive.InitializeCache(localCache.RootPath + "cache", localCache.MaximumSizeInMegabytes);

            CloudDrive drive = account.CreateCloudDrive("vhd/drive1.vhd");
            string driveLetter = drive.Mount(cacheSize, DriveMountOptions.None);

            string dbConnectionString = string.Format("Data Source={0}", driveLetter + @"\\azure.db");

            using (SQLiteConnection cn = new SQLiteConnection(dbConnectionString))
            using (SQLiteCommand cmd = cn.CreateCommand())
            {
                cn.Open();

                cmd.CommandText = "CREATE TABLE Personal (id INTEGER PRIMARY KEY, name TEXT)";
                cmd.ExecuteNonQuery();

                cn.Close();
            }
        }
    }
}

 

当たり前ですが、INSERTやSELECTもエンコードなく日本語が使えます。

INSERT

cmd.CommandText = "INSERT INTO Personal (name) VALUES('田中一郎')";
cmd.ExecuteNonQuery();

SELECT

cmd.CommandText = "SELECT * FROM Personal";
using (SQLiteDataReader reader = cmd.ExecuteReader())
{
	while (reader.Read())
	{
		Response.Write(String.Format("id = {0}, name = {1} <br />", reader[0], reader[1]));
	}
}