如何让Silverlight从MySQL获取数据 [英] How to have silverlight get its data from MySQL

查看:88
本文介绍了如何让Silverlight从MySQL获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Silverlight中编写了一个小型的hello world测试应用程序,我想在Linux / Apache2服务器上托管。我希望数据来自MySQL(或其他一些linux兼容数据库),以便我可以将数据绑定到数据库中的东西。



我已经设法使其工作使用 MySQL Connector / .NET

  MySqlConnection conn = new MySqlConnection(Server = the.server.com; Database = theDb; User = myUser; Password = myPassword;); 
conn.Open();
MySqlCommand命令= new MySqlCommand(SELECT * FROM test;,conn);
using(MySqlDataReader reader = command.ExecuteReader())
{
StringBuilder sb = new StringBuilder();
while(reader.Read())
{
sb.AppendLine(reader.GetString(myColumn));
}
this.txtResults.Text = sb.ToString();
}

如果我发布发布的ClickOnce应用程序完全信任(或至少SocketPermission)和在本地运行



我希望这可以在服务器上运行,我无法让它工作,总是以权限异常结束(不允许使用SocketPermission)。



数据库托管在与silverlight应用程序相同的服务器上,如果有任何差异。



编辑
好​​的,我现在明白为什么在客户端应用程序中显示db凭据是个坏主意。那么人们怎么做呢?如何保护代理Web服务,以便以安全的方式将数据中继到客户端/数据库?有没有在网上的例子?



当然,我不能第一个想要使用数据库来为Silverlight应用程序供电的人?

解决方案

最简单的方法来做你想要的(现在看过你的))将公开可以消费的服务。微软现在真正推出的模式是公开WCF服务,但事实是,您的Silverlight客户端可以使用WCF来消费大量不同类型的服务。



现在最简单的方法是在Web服务器或PHP REST服务上使用.NET服务,然后将Silverlight应用程序指向该服务。通过这样做,您不仅可以通过人们窥探数据库来保护数据库,更重要的是,您限制了人们可以对数据库执行任何操作。如果您的数据应该是只读的,并且您的服务的合同只允许读取操作,那么您将被设置。或者,您的服务可以通过WCF协商会话,再次通过WCF进行设置。



WCF可以是仅客户端,仅服务器或客户端 - 服务器连接器平台。你所选择的将会影响你编写的代码,但这一切都将独立于你的数据库。您的代码可以被结构化,使其与数据库表的一对一映射,或者它可以是更抽象的(如果您选择,可以设置代表完整逻辑视图的类)。


I've written a small hello world test app in Silverlight which i want to host on a Linux/Apache2 server. I want the data to come from MySQL (or some other linux compatible db) so that I can databind to things in the db.

I've managed to get it working by using the MySQL Connector/.NET:

MySqlConnection conn = new MySqlConnection("Server=the.server.com;Database=theDb;User=myUser;Password=myPassword;");
conn.Open();
MySqlCommand command = new MySqlCommand("SELECT * FROM test;", conn);
using (MySqlDataReader reader = command.ExecuteReader())
{
     StringBuilder sb = new StringBuilder();
     while (reader.Read())
     {
         sb.AppendLine(reader.GetString("myColumn"));
     }
     this.txtResults.Text = sb.ToString();
}

This works fine if I give the published ClickOnce app full trust (or at least SocketPermission) and run it locally.

I want this to run on the server and I can't get it to work, always ending up with permission exception (SocketPermission is not allowed).

The database is hosted on the same server as the silverlight app if that makes any difference.

EDIT Ok, I now understand why it's a bad idea to have db credentials in the client app (obviously). How do people do this then? How do you secure the proxy web service so that it relays data to and from the client/db in a secure way? Are there any examples out there on the web?

Surely, I cannot be the first person who'd like to use a database to power a silverlight application?

解决方案

The easiest way to do what you want (having read through your edits now :)) will be to expose services that can be consumed. The pattern that Microsoft is REALLY pushing right now is to expose WCF services, but the truth is that your Silverlight client can use WCF to consume a lot of different types of services.

What may be easiest for you to do right now would be to use a .NET service on a web server or maybe a PHP REST service, and then point your Silverlight app at that service. By doing so, you're protecting your database not only from people snooping through it, but more importantly, you're restricting what people can do to your database. If your data is supposed to be read-only, and your service's contract only allows reading operations, you're set. Alternatively, your service may negotiate sessions with credentials, again, set up through WCF.

WCF can be a client-only, server-only, or client-server connector platform. What you choose will affect the code you write, but it's all going to be independent of your database. Your code can be structured such that it's a one-to-one mapping to your database table, or it can be far more abstract (you can set up classes that represent full logical views if you choose).

这篇关于如何让Silverlight从MySQL获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆