How can I connect to my SQL Azure-database using Microsoft Silverlight? - silverlight

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

Related

ASP.NET MVC 4 Membership Database

I have an ASP.NET MVC 4 app. My database is hosted on Windows Azure. My solution has a new SQL Server Database project. I want to start off by adding adding the ASP.NET Membership functionality to this Database [project]. My question is, how do I do that?
I thought it would be a straight forward drag and drop operation. However, I cannot figure out how to do this basic task. All of the online videos I find demonstrate how to to implement the login via the ASP.NET MVC side. However, I don't see anything that shows setting up the Membership tables/sprocs/views and getting them in a SQL Server Database project.
Thank you for any insights.
You can get the script(s) necessary for adding membership to a database using aspnet_regsql in your framework folder (usually %windows%\Microsoft.NET\Framework\v4.0.30319\). An example usage for producing the .sql file would be:
...\v4.0.30319>aspnet_regsql -sqlexportonly MembershipScript.sql
From there, run that script against your database. You may also need to play with the -d <database> flag, but all options can be shown using aspnet_regsql -?. (This includes exporting the necessary tables for session state and other options).
I hope that's what you were asking but if I've misunderstood the question please clarify and I'll do my best to revise the answer
The template for creating an MVC 4 Internet application with forms-based authentication uses both the traditional ASP.NET Membership database with an additional layer on top called SimpleMembership, which has its own database. The SimpleMembership database uses Entity Framework code-first model which creates the database automatically the first time you start the application. Since SimpleMembership uses code-first the database schema can be modified by changing the UserProfile class in AcccountModels.cs. What is your objective for putting these databases that are handled by the application into a separate database project? Unless you are going to bypass SimpleMembership any solution will have to handle two databases.

Migrating a Windows Forms Application with SQL Server back end to Silverlight

Presently I have a Windows Forms application that obtains data from a SQL Server database on a separate server in our LAN. Basically we want to re-use as much as possible our source code that interacts with the SQL Server database and change the forms portion to a thin client silverlight solution. The problem is that our Windows Forms application is a fat client application; however, our company recently added employees working several thousand miles away, so they have a long delay in working with our application as it retrieves data from the database server which is a long ways away from the employee's client forms application.
The ideal solution for me would allow the developer to display data based on various database tables or views dynamically at runtime say based on what treeview item a user clicks and not having to hard code the database schema at design time. This is the way our windows forms application presently works.
One aspect of silverlight I am wrestling with right now is that if you want to access data from a SQL Server database on the web server side you have to use web services or WCF RIA, which of course involves creating a design time EDMX file or generating LINQ to SQL classes. The problem is that our database schema changes quite frequently, so that means I would have to keep manually re-updating the web services along with the EDMX and/or LINQ to SQL. What I would really like to do would be to just connect to the SQL Server database using ADO.NET to populate the various silverlight datagrids without having to deal with web services. Please note that I am pretty new to Silverlight, so perhaps I am missing something obvious.
Here is one of the many links I have checked as I've been working on this solution; however, this just migrates a Windows Forms application that already has a web service to a silverlight application that has a similar web service, so it doesn't seem to apply to my situation:
http://www.silverlight.net/learn/advanced-techniques/moving-from-windows-forms/migrating-a-windows-forms-application-to-silverlight
Here is another website that I have been looking at closely; however, the database that I am working with is so huge and has such a large schema that whenever I attempt to open or work with the Data->Show Data Sources or Data->Add Data Source window in Visual Studio it takes about an hour of the CPU running at full throttle before it displays the values in visual studio. Of course this makes Visual Studio almost unusable if this hour long wait happens every time I try to make a change in the silverlight XAML designer:
http://msdn.microsoft.com/en-us/gg315272
Also, the website example above is not an acceptable solution because we want the web server and the database server to be two separate machines, so we would not be able to put the database into the App_Data folder in the silverlight solution.
If anyone has any suggestions or guidance in terms of migrating this application, they would be most appreciated. TIA.
Roger
Basically I found out that since I'm used to writing desktop applications that communicate directly with a database (such as SQL Server), I was surprised to find that there is no object in Silverlight to enable you to do (SqlConnection, OdbcConnection, etc.). This is because Silverlight is a client platform, designed to be run from within a browser anywhere in the world, and so it does not make sense for it to be able to access databases directly because databases are generally hidden behind a firewall. The purpose of a service is to provide an interface for exposing data publicly from the server, acting as a conduit between the data in the database and external applications. (source: "Pro Business Applications with Silverlight 5" by Chris Anderson)
Please note that I also had made a mistake when I created a DomainService and assigned to it all the tables, queries, and stored procedures in the entire database, when in fact one needs to create a separate domain service for each individual table or query. This explains why I had the problem before where I had to wait about an hour for the Data Sources window to populate.

WPF with arbitrary, unknown databases - Client/Server or Desktop app?

My company is planning to turn an older Winforms application into a WPF/Silverlight Client/Server app.
The idea of having a small server app is to have a list of the accessible data bases combined with the user type that may access each of the databases, instead of having to manage databases in each client's admin control. Additionally, it would be great if the SQL request would be handled by the server which would then return the result.
The app is supposed to work on a arbitrary set of databases which will be "registered" with the server and users get a list of databases according to their authentication rights. They can then do practically everything on those databases what one can imagine. The system should be able to handle up to 2 million rows.
The databases are very different, there can be many of them, they can be MS Access, Oracle, SQL Server etc., so no way for me to specify them all before. On top of that, communication with a SQLite cache is needed.
I already have everything I need for the SQL queries from the Winforms app.
I was thinking:
1) A simple WCF server specifying in a config file the available databases per user type.
2) Interface that specifies all necessary SQL queries that can be made to the server.
3) Client...
The idea is:
a client-server application, where the client uses WCF services to execute SQL queries (INSERT, UPDATE, SELECT, etc.) on tables by invoking services methods.
The service should ideally be consumable for both the WPF and the Silverlight app.
Is that the way to go? Which exisiting technologies might I want to make use of regarding formats, communication, services etc.
If that is problematic, I would consider going back to a desktop app, but then how to ease the user type/database access problem for each client?
I would stick with ADO.NET and start with the DbProviderFactory class. This will let you determine the proper database access based on information supplied by the provider using the Factory Design Pattern. So instead of having to create a specialized objects for each database type and database, you can abstract that logic with the DbProviderFactory.
Here's a link that shows some examples: http://msdn.microsoft.com/en-us/library/wda6c36e(v=VS.100).aspx

How to allow user to select an existing database or create a new one?

We have a desktop application built with WinForms in 4.0 framework. A single user can install the application and use a local SQLExpress installation. Or, the SQLExpress can be on another machine on the network. We want to give users ability to create new database(which will execute a sql script which will create the required structure) on any server instance. Is there a control which we can used to accomplish this? Or, what are the ways to accomplish the aim?
if you're using an ORM (such as nHibernate)- then these abilities come out-of-the-box.
otherwise- you can write stored procedures that build the desired database.
this is a little more tricky, but possible.
here is a good start on DDL.

Where can I find resources for setting up a Silverlight app that accesses sql server 2008 Express DB?

I am trying to set up a basic Silverlight application to run behind the firewall on my Windows XP machine with IIS 5.1. We have a MySQL server for production purposes, but I would like to set up SQL Server 2008 Express Edition on my Windows XP machine to host the database part of the application.
I'm completely new to a lot of .NET develoment but would like to find the resources to get started. I've checked out Silverlight.net/GetStarted/, but unless I missed something there, this step wasn't explicitly there.
Does anyone know where I could go to read about how to do this and maybe a simple walthrough of a sample Silverlight application written to access SQL Server 2008 Express?
Any suggestions and links would be much appreciated.
There are 3 options that I know of for connecting to a database from Silverlight:
WCF: Create your own WCF service with methods like GetCustomers(), UpdateCustomers() and DeleteCustomers(). Those methods are implemented on the server side, so in the background you can use LINQ to SQL or just open an old-fashioned SqlConection and run some SqlCommands against the database.
ADO.NET Data Services: This is a custom WCF service implemented by MS. You point the service at an Entity Framework class (auto generated from your custom database, super easy to create) and in turn it'll accept standard http rest commands that that do things like GetCustomers/Update/Delete. You can then add a service reference to this guy from your Silverlight app and it will auto generate 1 class per table, etc along with a client to connect to the server (http port 80 so no firewall concerns here). On the client side you're interacting with rich classes that look just like your database structure and work well with LINQ. This is not Silverlight specific and could be used from any client, even a console app.
.NET RIA Services: This is like ADO.NET Data Services but adds support for many things Silverlight including property change notification on the client side (note that the bleeding edge CTP2 of ADO.NET Data Services also implements change notifications), easy to hook events for browser navigation (i.e. you get an event when the user clicks back or forward) and code sharing between the server and the client (just name your file MyClass.shared.cs in your ASP.NET application and it magically shows up in your Silverlight client).
All of these options use http port 80 so they'll work well with your firewall. I'd really encourage you to try all 3 if you've got the time. If you don't have the time go directly to ADO.NET Data Services (grab the CTP2 you'll appreciate the new features).
Plain vanilla SL + WCF + SQL, but it's a little old:
http://msdn.microsoft.com/en-us/magazine/cc794260.aspx
Shawn Wildermuth's MSDN article is a good starting point for ADO.NET Data Services:
http://msdn.microsoft.com/en-us/magazine/dvdarchive/cc794279.aspx
This will get you up and running with good results in short time but note that it uses beta software. You'll see how many improvements have been made to ADO.NET DS since Shawn wrote that previous article:
http://blogs.msdn.com/astoriateam/archive/2009/09/01/introduction-to-data-binding-in-ctp2.aspx
And here's Brad Abram's mother of all data connectivity posts (RIA Services focus here - I think the series is up to 24 parts now):
http://blogs.msdn.com/brada/archive/2009/08/02/business-apps-example-for-silverlight-3-rtm-and-net-ria-services-july-update-summary.aspx

Resources