[英]Connect to a SQL DB hosted on azure through a windows store app
[英]How can I connect to a SQL Azure DB table from my Windows store app?
基于此链接: 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, SqlConnection
和SqlCommand
无法识别/可解析。 我是否需要安装一个单独的ADO.NET软件包才能使用,或者是什么交易?
UPDATE
通过将System.Data.dll
添加到我的项目引用(在我的机器上,从C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\.NETFramework\\v4.5
),我可以让这些类被识别/已解决,但仍然遇到编译时错误,即:
错误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
和:
错误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
添加SQL.Data作为引用允许解析各种类型,但是不同的问题阻止了应用程序的编译,即:
在模块mscorlib.dll中找不到类型System.SystemException
从References中删除SQL.Data消除了这个问题。
您需要使用(或构建)服务接口,您无法直接从Windows 8商店应用程序访问Windows Azure SQL数据库(即SQL SQL),我认为即使您可以也不应该这样做。 以下是两个主要原因:
SQL数据库仅支持SQL Server身份验证。 这意味着每个客户端设备将在数据库登录方面拥有该王国的密钥。 如果该登录遭到入侵,您手上就会遇到严重问题。
通过服务器上的防火墙管理对SQL数据库的访问,只允许来自列入白名单的IP地址的流量进入服务器。 这几乎意味着您必须完全打开防火墙到任何IP地址0.0.0.0到255.255.255.255,因为您不会知道您的客户端将进入的IP范围。
查看Windows Azure移动服务,它为Windows Azure SQL数据库提供安全的RESTful CRUD前端。 我有一篇博客文章 ,它使用移动服务来管理全球排行榜,这可能会有所帮助。
Metro应用程序无法使用System.Data。 如果您仍想使用SQL,则可以使用Sqlite,或将Azure SQL DB作为数据服务器。 否则,您可以使用LocalStorage
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.