【问题标题】:Linking WPF project to Windows Azure Database将 WPF 项目链接到 Windows Azure 数据库
【发布时间】:2013-04-16 01:30:13
【问题描述】:

我对 Windows Azure 比较陌生。到目前为止,我的 C# WPF 项目(Visual Studio 2012)一直在访问本地数据库。最近设置了一个 Windows Azure 帐户,我想将本地数据库迁移到 Azure。

我在 Azure 上创建了一个 SQL 数据库。

我正在添加 C# 代码,以便将数据从本地应用程序传递到云端。为了测试此功能,我添加了一个随机用户,我想将其插入 Azure DB。

这是我目前所拥有的:

在 App.config 中:

<connectionStrings>
   <add name="STAREntites"
        connectionString="Server=tcp:xxxxx.database.windows.net;
                     Database=xxxxx;
                     Uid=xxxxx.database.windows.net;
                     Pwd=xxxxx;
                     Encrypt=Yes;" />
</connectionStrings>

我有一个包含PersonRepository.cs 的“模型”文件夹:

public class PersonEntity : TableEntity
{
    public PersonEntity()
    {
    }
    public PersonEntity(int id, string name, double sal)
    {
        Id = id;
        Name = name;
        Salary = sal;
        PartitionKey = id.ToString();
        RowKey = name;
    }
    public int Id { get; set; }
    public string Name { get; set; }
    public double Salary { get; set; }
}

}

在我的注册页面中,我还有以下代码可以将数据发送到 Azure DB:

private void RegisterUser()
    {
             string connStr = ConfigurationManager.ConnectionStrings["STAREntites"].ConnectionString;                           
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connStr);
            CloudTableClient client = storageAccount.CreateCloudTableClient();
            CloudTable table = client.GetTableReference("PersonTable");
            table.CreateIfNotExists();
            var emp = new PersonTable
            {
                FirstName = "Cor",
                LastName = "Mky",
                EmailAddress = "cm@email.com",
                Password = "password",
            };

            TableOperation insertOp = TableOperation.Insert(emp);
            table.Execute(insertOp);

    }

目前我可以通过服务器资源管理器看到从RegisterUser 方法添加的测试用户已插入到 Windows Azure 存储中的人员表中。但是,当我检查 Azure SQL DB 时,实体不存在。

谁能帮忙解释为什么实体没有被发送到 Azure?

我检查了我的端口正在侦听并且 SQL 服务器正在运行。我在 Visual Studio 中没有收到任何错误。

【问题讨论】:

  • 你的 UID 看起来很奇怪。应该是“uuuuu@xxxxx”。

标签: c# wpf database visual-studio azure


【解决方案1】:

您似乎对存储类型感到困惑。 CloudStorageAccount 用于 Azure 存储:表、Blob 和队列,不适用于 Windows Azure SQL 数据库。

我不太确定CloudStorageAccount.Parse 在传递看起来像 SQL Server 连接字符串的内容时如何不会引发错误。

如果您想将 Entity Framework 与 Windows Azure SQL 数据库一起使用,只需使用常规的DbContext 代码并向 WASD 提供 ADO.NET 连接字符串,除了 Uid 应该是 @ 987654326@.

如果您想使用表存储,那么您应该在表存储中查看插入的实体,使用类似Zudio 的内容。

Information on the available Azure storage options.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    相关资源
    最近更新 更多