How to Connect to Data
In LightSwitch, we can easily connect to existing data or to data tables that we create.
To provide data to a LightSwitch application, we can either connect to an existing data source such as a SQL Server database, or we can create tables in the SQL Server Express database that is included in LightSwitch.
We can connect to data by connecting:
- through a SQL Server Database
- to a SharePoint List
- to an Open Data (OData) Service
- to a Windows Communication Foundation (WCF) Rich Internet Application (RIA) Service
We can then use this data to create screens and queries in Visual Studio LightSwitch 2012.
How to Connect to a SQL Server Database
Step 1
Open the Solution Explorer.
Image 1
![solex.jpg]()
Step 2
In the Solution Explorer, click on the "Data Sources" option.
Image 2
![datasource.jpg]()
Step 3
On the Project Menu, click on Add Data Source option.
Image 3
![add data src.jpg]()
Step 4
The Attach Data Source Wizard appears.
Image 4
![attach data src wiz.jpg]()
Step 5
Choose a Data Source Type, in other words from where the application gets the data, then click on the "Database" option and then click "Next".
Image 5
![attach data src wiz.jpg]()
Step 6
The "Choose Data Source" dialog box appears.
Image 6
![choose data src.jpg]()
Step 7
Click on "Microsoft SQL Server" and click on the "Continue" button.
Image 7
![microsoft sql server.jpg]()
Step 8
The "Connection Properties" dialog box will appear.
Image 8
![connprop.jpg]()
Step 9
In the Server name box of the Connection Properties dialog box, choose the name of the SQL Server.
Image 9
![connprop1.jpg]()
Step 10
In order to logon to the server you can either click on Windows Authentication or click on "Use SQL Server Authentication". If you click on "Use SQL Server Authentication" then provide the user name and word of a user who has access to the database.
Image 10
![connprop2.jpg]()
Step 11
In order to connect to the Database click "Select" or enter a Database name. In the drop down list select the name of the database.
Image 11
![connprop3.jpg]()
Step 12
Test The Connection; for that click on the "Test Connection" button.
Image 12
![tests.jpg]()
Step 13
A Microsoft Visual Studio dialog box is shown and click "OK".
Image 13
![testconn.jpg]()
Step 14
When we click OK in the Connection Properties dialog box, "Choose Your Database Object" is shown.
Image 14
![wizard.jpg]()
Step 15
In the Choose your Database Objects page, select the tables that we want to use in our LightSwitch application, and then click "Finish".
Image 15
![wizard.jpg]()
Step 16
The database and tables that we selected appear in the Solution Explorer.
Image 16
![appear.jpg]()