سؤال

I have a database online at Windows Azure. I want to connect to the database and show some tables in Microsoft Silverlight. I have created a Silverlight Application and published the website on Azure here!

How do I connect to my SQL Azure database in Silverlight?

هل كانت مفيدة؟

المحلول

The same way as connecting to any other database! Personally, I use EntityFramework and create my own service. You can use EF and use RIA Services to create a lot of code for you though if you prefer.

Add an Entity Model to your project, create from existing db, point at your Azure Db, job done. Remember to add your existing IP to the Azure firewall while developing.

EDIT: Try following this tutorial instead: http://msdn.microsoft.com/en-us/library/ee707376(v=vs.91).aspx BUT one thing to watch out for. When you come to the step to add a DomainService, if it hasn't picked up your context you will need to follow the steps listed here: http://support.microsoft.com/kb/2745294 ... which are easy to follow. Delete the TT files and change the model code generation strategy from none to default.

نصائح أخرى

One thing that you can do is create a WCF project, which will act as your Web Role in Azure.

Ideally, you may have 2-3 projects in your solution: 1) Silverlight Project (Presentation Layer) 2) WCF Project (Business Layer) 3) C# Class Project for your Sql Connections (Data Layer)

What you would be able to do is use C# .Net libraries for Sql to connect to your database. You may either execute stored procedures (Can pass in variables), which are cached in memory for faster long-term performance, or Sql queries in a string variable, to pull the exact information you want in custom queries. This would be returned to the WCF Project in which you would be able to perform any custom business-logic and you may begin to package your data into class objects. These objects may be passed back to Silverlight via a Service Reference, and you may use many familiar Silverlight tools to display your information.

The above recommendation may be a more more in-depth than that of the other recommendations, but in cases where you really need control over your data and need to apply business logic, this is a good way.

The easiest way in the world I know of to hook up a SQL Database to a Silverlight application is to use a Visual Studio LightSwitch project. There are ton's of posts on how to do this - http://blogs.msdn.com/b/lightswitch/ . Basically if you make a LightSwitch application (available in VS Professional SKUs and up), it gives you several screen templates to choose from which are all Silverlight (or HTML 5 if you choose). Then you can "attach" to an existing SQL Database such as SQL Azure just fine. And make browse or edit screens around the SQL data. You can optionally model your own SQL Database and then "publish" it out to SQL Azure.

You can do all of this without writing any code too and provides some good basic functionality.

HTH - Matt

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top