简体   繁体   English

如何从Windows应用商店应用连接到SQL Azure数据库表?

[英]How can I connect to a SQL Azure DB table from my Windows store app?

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

I'm trying this code to connect to SQL Azure database and insert a row: 我正在尝试此代码连接到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();
    }

Trying to, that is - SqlConnectionStringBuilder, SqlConnection , and SqlCommand are not recognized/resolvable. 尝试,即 - SqlConnectionStringBuilder, SqlConnectionSqlCommand无法识别/可解析。 Do I need to install a separate ADO.NET package for this to work, or what's the deal? 我是否需要安装一个单独的ADO.NET软件包才能使用,或者是什么交易?

UPDATE UPDATE

By adding System.Data.dll to my project references (on my machine, from C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\.NETFramework\\v4.5 ), I could get those classes to be recognized/resolved, but still get compile-time errors, namely: 通过将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 错误1无法解析类型为“System.Data.Common.DbConnection”的程序集“System,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089”中的基类或接口“System.ComponentModel.Component” :\\ Program Files(x86)\\ Reference Assemblies \\ Microsoft \\ Framework.NETFramework \\ v4.5 \\ System.Data.dll

and: 和:

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无法解析类型为“System.Data.Common.DbCommand”的程序集“System,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089”中的基类或接口“System.ComponentModel.Component” :\\ Program Files(x86)\\ Reference Assemblies \\ Microsoft \\ Framework.NETFramework \\ v4.5 \\ System.Data.dll

UPDATE 2 更新2

Adding SQL.Data as a reference allowed the various types to be resolved, however a different problem then prevented the app from compiling, namely: 添加SQL.Data作为引用允许解析各种类型,但是不同的问题阻止了应用程序的编译,即:

Cannot find type System.SystemException in module mscorlib.dll 在模块mscorlib.dll中找不到类型System.SystemException

Removing SQL.Data from References eliminated that problem. 从References中删除SQL.Data消除了这个问题。

You'll need to use (or build) a service interface, you can't access Windows Azure SQL Database (nee SQL Azure) directly from a Windows 8 store app, and I'd contend you shouldn't even if you could. 您需要使用(或构建)服务接口,您无法直接从Windows 8商店应用程序访问Windows Azure SQL数据库(即SQL SQL),我认为即使您可以也不应该这样做。 Here are two primary reasons why: 以下是两个主要原因:

  1. SQL Database supports only SQL Server authentication. SQL数据库仅支持SQL Server身份验证。 That means each client device will have the keys to the kingdom in terms of the database login. 这意味着每个客户端设备将在数据库登录方面拥有该王国的密钥。 If that login gets compromised, you have a significant problem on your hands. 如果该登录遭到入侵,您手上就会遇到严重问题。

  2. Access to SQL Database is managed via a firewall on the server, and only traffic from whitelisted IP addresses is allowed to enter the server. 通过服务器上的防火墙管理对SQL数据库的访问,只允许来自列入白名单的IP地址的流量进入服务器。 That pretty much means you'd have to open up the firewall completely to ANY IP address 0.0.0.0 to 255.255.255.255 since you won't know that IP ranges your clients will come in on. 这几乎意味着您必须完全打开防火墙到任何IP地址0.0.0.0到255.255.255.255,因为您不会知道您的客户端将进入的IP范围。

Check out Windows Azure Mobile Services, it provides a secure RESTful CRUD front-end to a Windows Azure SQL Database. 查看Windows Azure移动服务,它为Windows Azure SQL数据库提供安全的RESTful CRUD前端。 I have a blog post that uses Mobile Services to manage a global leaderboard, which may be of help as an example. 我有一篇博客文章 ,它使用移动服务来管理全球排行榜,这可能会有所帮助。

System.Data is unavailable to Metro apps. Metro应用程序无法使用System.Data。 If you still want to use SQL, you can use Sqlite, or make the Azure SQL DB as a data server. 如果您仍想使用SQL,则可以使用Sqlite,或将Azure SQL DB作为数据服务器。 Otherwise you can use LocalStorage instead 否则,您可以使用LocalStorage

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过Windows Store应用连接到Azure托管的SQL DB - Connect to a SQL DB hosted on azure through a windows store app 在Windows应用商店中连接到SQL Lite数据库 - Connect to sql lite db in windows store app 将Windows Phone 8.1 Store应用连接到现有的Azure SQL数据库 - Connect a Windows Phone 8.1 Store App to an Existing Azure SQL Database 无法从Windows应用商店应用插入Azure db - Trouble inserting into Azure db from Windows Store app 如何连接windows phone app使用存储在sql azure中的数据库 - How to connect windows phone app to use database stored in sql azure 如何使用Windows Azure缓存服务在预览中将Linq存储到Sql对象中 - How can I store Linq to Sql objects in session using Windows Azure Cache Service (Preview) 如何使用已在Azure中注册的应用程序使用实体框架数据库第一种方法连接到Azure SQL DB - How to connect to Azure SQL DB using entity framework database first approach using already registered app in Azure 我们可以将 Microsoft Azure Bot 连接到 Azure SQL 数据库并从 DB 读取数据并将其用作机器人的回复吗? - Can we connect Microsoft Azure Bot to Azure SQL Database and read data from the DB and use it as a reply for the Bot? 我如何将 azure MySQL 服务器与我的 Xamarin 表单应用程序连接? - How i can connect azure MySQL server with my Xamarin forms app? 无法使用.net在给定的数据库连接中连接到表Azure SQL - Can't Connect to the table azure SQL in given DB connection using .net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM