基于此链接: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 程序包才能使其正常工作,或者有什么关系?
更新
通过将 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),我认为您不应该,即使你可以。以下是两个主要原因:
SQL 数据库仅支持 SQL Server 身份验证。这意味着每个客户端设备都将拥有数据库登录方面的王国 key 。如果该登录名遭到破坏,您将面临重大问题。
对 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/