c# - 如何从我的 Windows 商店应用程序连接到 SQL Azure 数据库表?

标签 c# ado.net windows-8 azure-sql-database windows-store-apps

基于此链接:http://msdn.microsoft.com/en-us/library/windowsazure/ee336243.aspx

我正在尝试使用此代码连接到 SQL Azure 数据库并插入一行:

// The values being assigned to the StringBuilder members are set previously/not shown
    SqlConnectionStringBuilder connStringBuilder = new SqlConnectionStringBuilder();
    connStringBuilder.DataSource = dataSource;
    connStringBuilder.InitialCatalog = databaseName;
    connStringBuilder.Encrypt = true;
    connStringBuilder.TrustServerCertificate = false;
    connStringBuilder.UserID = userName;
    connStringBuilder.Password = password;

    using (SqlConnection conn = new SqlConnection(connStringBuilder.ToString()))
    {
        using (SqlCommand command = conn.CreateCommand())
        {
            conn.Open();
            command.CommandText =
                "INSERT INTO T1 (col1, col2) values (1, 'string 1'), (2, 'string 2'), (3, 'string 3')";
            int rowsAdded = command.ExecuteNonQuery();
        }
        conn.Close();
    }

尝试,即 - SqlConnectionStringBuilder、SqlConnectionSqlCommand 无法识别/解析。我是否需要安装单独的 ADO.NET 程序包才能使其正常工作,或者有什么关系?

更新

通过将 System.Data.dll 添加到我的项目引用中(在我的机器上,来自 C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5),我可以识别/解析这些类,但仍然会出现编译时错误,即:

Error 1 The base class or interface 'System.ComponentModel.Component' in assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' referenced by type 'System.Data.Common.DbConnection' could not be resolved c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\System.Data.dll

和:

Error 2 The base class or interface 'System.ComponentModel.Component' in assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' referenced by type 'System.Data.Common.DbCommand' could not be resolved c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\System.Data.dll

更新 2

添加 SQL.Data 作为引用允许解决各种类型,但是一个不同的问题阻止了应用程序编译,即:

在模块 mscorlib.dll 中找不到类型 System.SystemException

从引用中删除 SQL.Data 解决了这个问题。

最佳答案

您需要使用(或构建)服务接口(interface),您不能直接从 Windows 8 商店应用程序访问 Windows Azure SQL 数据库(nee SQL Azure),我认为您不应该,即使你可以。以下是两个主要原因:

  1. SQL 数据库仅支持 SQL Server 身份验证。这意味着每个客户端设备都将拥有数据库登录方面的王国 key 。如果该登录名遭到破坏,您将面临重大问题。

  2. 对 SQL 数据库的访问是通过服务器上的防火墙进行管理的,只有来自白名单 IP 地址的流量才允许进入服务器。这几乎意味着您必须对 0.0.0.0 到 255.255.255.255 之间的任何 IP 地址完全打开防火墙,因为您不知道您的客户端将进入的 IP 范围。

查看 Windows Azure Mobile Services,它为 Windows Azure SQL 数据库提供安全的 RESTful CRUD 前端。 I have a blog post使用移动服务来管理全局排行榜,这可能会有所帮助。

关于c# - 如何从我的 Windows 商店应用程序连接到 SQL Azure 数据库表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13637746/

相关文章:

c# - 如何在提交按钮点击时自动生成优惠券代码

c# - 迁移会影响我的数据库吗?

c# - 正则表达式和 While 循环 - 为真时仍显示假

c# - Execute Scalar、Execute Reader 和 Data Set 在哪里使用?

windows-8 - <元名称 ="msapplication-config"内容 ="none"> 用于 browserconfig.xml 不起作用

.net - 如何在 Windows 8 中自动运行需要管理员权限的应用程序

c# - 用C#读取Excel文件——选择工作表

c# - 使用 ADO.NET 库的 C++ 中的 SQL Server 2014 查询通知

entity-framework - Entity Framework 4 选择性延迟加载属性

xaml - Windows 8 Metro 应用程序 XAML - 让自定义按钮在单击时闪烁?