简体   繁体   English

从silverlight应用程序访问SQL Server数据库

[英]Accessing SQL Server database from silverlight application

Our ASP.NET website allows user to perform a variety of queries and displays a network diagram (like a UML diagram) based on the result queried from database. 我们的ASP.NET网站允许用户执行各种查询,并根据从数据库查询的结果显示网络图(如UML图)。 At present, we are generating a Bitmap and displaying it. 目前,我们正在生成一个位图并显示它。 But since we needed to support a feature to allow user to show/hide certain blocks interactively, we plan to use Silverlight for rendering the graphic. 但由于我们需要支持允许用户以交互方式显示/隐藏某些块的功能,因此我们计划使用Silverlight来渲染图形。 Also we plan to add more interactions in the future. 我们还计划在未来添加更多互动。

I have two questions: 我有两个问题:

  1. Is it possible to an ASP.NET application to 'send' parameters to silverlight application. ASP.NET应用程序是否可以将参数“发送”到silverlight应用程序。
  2. Is it possible for a silverlight application to query a SQL server database. Silverlight应用程序是否可以查询SQL Server数据库。

PS. PS。 If there are other better alternatives than Silverlight, please do suggest. 如果还有比Silverlight更好的替代品,请建议。

  1. Yes, by using query string parameters in your aspx markup where you define your object tag. 是的,通过在aspx标记中使用查询字符串参数来定义对象标记。
  2. No, at least not directly. 不,至少不是直接的。 You can connect your silverlight application to a WCF application that can query your database. 您可以将silverlight应用程序连接到可以查询数据库的WCF应用程序。

You should use proper layered architecture in general and with SL you cannot, in fact, reference any class library which is not a SL control library. 您应该使用适当的分层体系结构,而对于SL,您实际上不能引用任何不是SL控制库的类库。

Once you have your DAL and your BL layers available you can expose part of the logic of the BL as needed with a WCF service layer and use it from SL application. 一旦您的DAL和BL层可用,您可以根据需要使用WCF服务层公开BL的部分逻辑,并在SL应用程序中使用它。 See my suggested layered approach here which works with any UI framework not only with MVC and applies also if you are not using EF in fact. 请参阅我在此处建议的分层方法,该方法不仅适用于任何UI框架,而且适用于MVC,如果您实际上不使用EF,也适用。

MVC3 and Entity Framework MVC3和实体框架

Simply put: 1 yes, 2 directly: no 简单地说:1是,2直接:不

Not so simply put: 不那么简单地说:

1: 1:

You can use the Initparams to pass multiple (string) parameters 您可以使用Initparams传递多个(字符串)参数


EDIT: If you put the following in your object code on your aspx page: 编辑:如果您将以下内容放在您的aspx页面上的目标代码中:

<param name="InitParams" value="keyOne=valueOne, keyTwo=valueTwo" />

And in your App.cs you add the following to the constructor: 在App.cs中,将以下内容添加到构造函数中:

this.Startup += this.Application_Startup;

Then in that function you can get to the Dictionary of the init params. 然后在该函数中,您可以访问init params的Dictionary。

private void Application_Startup(object sender, StartupEventArgs e)
{
    foreach (var data in e.InitParams)
    {
        if(data.Key.Equals("keyOne"))
        {
            //data.Value now equals valueOne
        }
    }
}

2: You can use a WCF service to communicate with a server and send and receive data (for example database data) 2:您可以使用WCF服务与服务器通信并发送和接收数据(例如数据库数据)


EDIT: 编辑:

In this link is explained how to host and consume a WCF service (even hosting in IIS is explained) the consume part works the same for WPF, Silverlight and any other .NET program. 此链接中解释了如何托管和使用WCF服务(甚至解释了IIS中的托管)消费部分对WPF,Silverlight和任何其他.NET程序的工作原理相同。


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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM