【问题标题】:How can I connect to a SQL Azure DB table from my Windows store app?如何从我的 Windows 商店应用程序连接到 SQL Azure DB 表?
【发布时间】:2012-12-02 10:46:38
【问题描述】:

基于此链接: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),我可以让这些类被识别/解析,但仍然会出现编译时错误,即:

错误 1 ​​程序集 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' 中的基类或接口 'System.ComponentModel.Component' 由类型 'System.Data.Common.DbConnection' 引用' 无法解析 c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\System.Data.dll

和:

错误 2 类型“System.Data.Common.DbCommand”引用的程序集“System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中的基类或接口“System.ComponentModel.Component” ' 无法解析 c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\System.Data.dll

更新 2

添加 SQL.Data 作为参考可以解决各种类型,但是另一个问题导致应用无法编译,即:

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

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

【问题讨论】:

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


    【解决方案1】:

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

    1. SQL 数据库仅支持 SQL Server 身份验证。这意味着每个客户端设备都将拥有数据库登录方面的王国密钥。如果该登录名被泄露,您将面临严重的问题。

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

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

    【讨论】:

      【解决方案2】:

      System.Data 对 Metro 应用程序不可用。如果还想使用 SQL,可以使用 Sqlite,或者将 Azure SQL DB 作为数据服务器。否则,您可以改用 LocalStorage

      【讨论】:

      • 我已经将 SQLite 用于我的本地数据库,并使用本地存储来保存状态/设置。不过,我也需要使用 SQL DB(以前称为 SQL Azure)——我正在将它用作数据服务器——这就是我的应用程序套件的两个部分“通信”的方式。至少,我正在努力,因此提出了这个问题。
      猜你喜欢
      • 1970-01-01
      • 2018-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-28
      • 2017-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多