Friday, 25 November 2011

SQL Server - Summary

Congratulations for reaching the end of this SQL Server tutorial!
SQL Server is a powerful application, and this tutorial has provided an overview of the main concepts involved in administering SQL Server 2008 Express.
To recap briefly, we've:
  • Created databases
  • Created database tables
  • Inserted data into those tables
  • Used the SSMS to run a query
  • Created a login account
  • Looked at server roles
  • Learned about database schemas
  • Looked at linked servers
  • and more

What Next?

There were a couple of areas we didn't touch on during this tutorial. This is mainly due to the fact that it is aimed at the free SQL Server Express version, which has its limitations.
All is not lost though. My SQL Server 2000 tutorial covers some of the areas that weren't included here. For example, you can learn about the SQL Server Agent for scheduling jobs to run automatically. You can also read about the SQL Profiler, which allows you to monitor events within your SQL Server.
The most logical next step from learning SQL Server is to learn SQL itself (if you haven't already done this that is). SQL stands for Structured Query Language and enables you to perform database related tasks programatically. Most of the tasks in this tutorial can be done programatically using SQL.
Probably the most common use for SQL is to work with the data in the database. For example, to insert data, select data, update data, or to delete data. Any database driven website will have many SQL statements doing this stuff.
To learn more about SQL, check out the SQL tutoria

SQL Server Integration Services (SSIS)

SQL Server Integration Services (SSIS) allows you to integrate smoothly with other applications and data sources.
You can use Integration Services to copy or download files, send e-mail messages in response to events, update data warehouses, clean and mine data, and manage SQL Server objects and data.
In SQL Server 2005 and higher, Data Transformation Services (DTS) is no longer installed with SQL Server. All DTS related tasks are now performed with SSIS.

Where Has DTS Gone?

If you have been working with earlier versions of SQL Server (SQL Server 2000 and earlier), you are probably familiar with DTS. DTS allowed you to transfer data between disparate sources into one or more destinations.
Well, starting from SQL Server 2005, DTS has been deprecated. DTS is no longer included with your SQL Server installation.
If you need DTS-type functionality in SQL Server 2008, you will need to use SQL Server Integration Services.

Backwards Compatibility

For backwards compatibility, Microsoft has provided installation files that provide run-time support for DTS packages. This will be useful if you have already created DTS packages in an earlier version of SQL Server. For more information see Support for Data Transformation Services (DTS) in SQL Server 2008 on the MSDN website.

SQL Server Express & SSIS

SQL Server Integration Services is not available in the Express or Workgroup editions of SQL Server. If you need to use Integration Services, you need at least SQL Server Standard edition. And, if you need to use advanced features (such as data mining, text mining and cleansing or fuzzy transforms), you will need SQL Server Enterprise edition.
For more information on SQL Server Integration Services, check out SQL Server Integration Services on the MSDN website.

SQL Server - Linked Servers

If you've been navigating around SQL Server Management Studio (SSMS), you may have come across the Linked Servers option.
The Linked Servers option allows you to connect to another instance of SQL Server running on a different machine, perhaps remotely in a different city/country. This can be useful if you need to perform distributed queries (query a remote database). Setting up a linked server is quite straight forward in SSMS, all you need is details of the remote server, and the database that you need to query.

Creating a Linked Server

To create a linked server:
  1. Navigate to Server Objects > Linked Servers
  2. Right click on Linked Servers and select New Linked Server.... Like this:
    Creating a linked server in SQL Server Management Studio
  3. Complete the details for the linked server. In this example, Data source refers to the name of the SQL Server machine ("Barts_database_server"), Catalogue refers to the name of the database ("Barts_database"). You can also configure options in the other two tabs depending on your requirements.
    Creating a linked server in SQL Server Management Studio

Distributed Queries

Once you have configured your linked server, you will be able to run queries etc against it. When you run a query against a linked server, it is referred to as a distributed query.
When you execute a distributed query against a linked server, you must include a fully qualified, four-part table name for each data source to query. This four-part name should be in the form linked_server_name.catalog.schema.object_name.
Here's an example:
Barts_database_server.Barts_database.Person.Enemy
This example is based on the linked server example above. It assumes that the remote database has a schema called "Person" and a table called "Enemy".

SQL Server - Database Schemas

Ever since SQL Server 2005 was released, each object in a database has belonged to a database schema. SQL Server 2008 has continued with database schemas, and an explanation follows.

What is a Database Schema?

A database schema is a way to logically group objects such as tables, views, stored procedures etc. Think of a schema as a container of objects.
You can assign a user login permissions to a single schema so that the user can only access the objects they are authorized to access.
Schemas can be created and altered in a database, and users can be granted access to a schema. A schema can be owned by any user, and schema ownership is transferable.

Creating a Database Schema

To create a database schema in SQL Server 2008:
  1. Navigate to Security > Schemas
  2. Right click on Schemas and select New Schema.... Like this:
    Creating a database schema in SQL Server Management Studio
  3. Complete the details in the General tab for the new schema. In this example, the schema name is "person" and the schema owner is "Homer".
    Creating a database schema in SQL Server Management Studio
  4. Add users to the schema as required and set their permissions:
    Creating a database schema in SQL Server Management Studio
  5. Add any extended properties (via the Extended Properties tab)
  6. Click OK.

Add a Table to the New Schema

Now that we have a new schema, we can add objects such as tables, views, and stored procedures to it. For example, we could transfer the table that we created in the earlier lesson to the new schema.
When we created that table (called "Individual"), it was created in the default database schema ("dbo"). We know this because it appears in our object browser as "dbo.Individual".
To transfer the "Individual" table to the person "schema":
  1. In Object Explorer, right click on the table name and select "Design":
    Changing database schema for a table in SQL Server Management Studio
  2. From Design view, press F4 to display the Properties window.
  3. From the Properties window, change the schema to the desired schema:
    Changing database schema for a table in SQL Server Management Studio
  4. Close Design View by right clicking the tab and selecting "Close":
    Closing Design View
  5. Click "OK" when prompted to save
Your table has now been transferred to the "person" schema.

Confirm your Change

To confirm the change:
  1. Refresh the Object Browser view:
    Refreshing the view in Object Browser
  2. You will now see that Object Browser displays the new schema for the table (person.Individual):
    Screenshot of the table in Object Browser

SQL Server - Server Roles

When creating a new user login in SQL Server, you get the option of assigning the login one or more server roles.
Server roles (not to be confused with database roles) are available for various database administration tasks. Not everyone should be assigned to a server role. In fact, only advanced users such as database administrators should be assigned a server role.

Accessing the Server Roles

To access the server roles in SQL Server Management Studio, expand the Security folder:
Screenshot of accessing server roles You view the properties of a server role by right clicking on it. You can then add users to the server role by clicking Add. In the screenshot below, Homer has been added to the securityadmin role.
Adding a user to a server role

Explanation of Server Roles

Here's an explanation of the server roles defined in SQL Server 2008 during setup:
Server RoleDescription
sysadminCan perform any task in SQL Server.
serveradminCan set server-wide configuration options, can shut down the server.
setupadminCan manage linked servers and startup procedures.
securityadminCan manage logins and database permissions, read logs, change passwords.
processadminCan manage processes running in SQL Server.
dbcreatorCan create, alter, and drop databases.
diskadminCan manage disk files.
bulkadminCan execute BULK INSERT statements.
publicEvery SQL Server user account belongs to this server role. When a server principal has not been granted or denied specific permissions on a securable object, the user inherits the permissions granted to public on that object. Only assign public permissions on an object when you want the object to be available to all users.
As you can see, some of these roles allow very specific tasks to be performed. If you don't have many technical users, it's likely that you'll only use one or two of these roles (including sysadmin).

SQL Server - User Logins

SQL Server allows for the creation of user logins. Each individual who needs access to SQL Server can be given their own user account.
When the administrator configures these user logins, he/she can assign them to any number of roles and schemas, depending on the access that the individual is entitled to.
In this lesson, we will walk through the steps in creating a user login.

To Create a New User Login

  1. Using SQL Server Management Studio, expand the "Security" option and right click on "Logins"
  2. Click on "New Login"
    Creating a new login in SQL Server
  3. Complete the login properties in the "General" tab by providing a name for the login, choosing the Authentication method (providing a password if you choose "SQL Server authentication"), and selecting the database to use as a default. If you don't choose a language, it will use the default for the current installation of SQL Server.
    If you get an error that reads "The MUST_CHANGE option is not supported by this version of Microsoft Windows", simply uncheck the "User must change password at next login" option. The error occurs because your operating system doesn't support this option.

    Creating a new login in SQL Server - General tab
  4. Click the "Server Roles" tab if you need to apply any server-wide security privileges.
    Creating a new login in SQL Server - Server Roles tab
  5. Click the "User Mapping" tab to specify which databases this user account is allowed to access. By default, the login will be assigned to the "Public" role, which provides the login with basic access. If the login needs more access in one or more databases, it can be assigned to another role with greater privileges.Note that these roles are "Database Roles" and are different to the server roles in the previous tab. Server roles are for administering the SQL Server. Database roles are created within each database and specify what the login can do within that database.
    Creating a new login in SQL Server - User Mapping tab

SQL Server - Stored Procedures

Stored procedures are a powerful part of SQL Server. They can assist programmers and administrators greatly in working with the database configuration and its data.
A stored procedure is a precompiled group of Transact-SQL statements, and is saved to the database (under the "Stored Procedures" node). Programmers and administrators can execute stored procedures either from the SQL Server Management Studio or from within an application as required.
Transact-SQL, which is based on SQL (Structured Query Language), is the programming language used to interface between applications and their databases. Transact-SQL is a relatively easy language to learn and I highly recommend becoming familiar with it.

Benefits of Stored Procedures

Here are some key benefits in using stored procedures:
BenefitExplanation of benefit
Modular programmingYou can write a stored procedure once, then call it from multiple places in your application.
PerformanceStored procedures provide faster code execution and reduce network traffic.
  • Faster execution: Stored procedures are parsed and optimized as soon as they are created and the stored procedure is stored in memory. This means that it will execute a lot faster than sending many lines of SQL code from your application to the SQL Server. Doing that requires SQL Server to compile and optimze your SQL code every time it runs.
  • Reduced network traffic: If you send many lines of SQL code over the network to your SQL Server, this will impact on network performance. This is especially true if you have hundreds of lines of SQL code and/or you have lots of activity on your application. Running the code on the SQL Server (as a stored procedure) eliminates the need to send this code over the network. The only network traffic will be the parameters supplied and the results of any query.
SecurityUsers can execute a stored procedure without needing to execute any of the statements directly. Therefore, a stored procedure can provide advanced database functionality for users who wouldn't normally have access to these tasks, but this functionality is made available in a tightly controlled way.

Creating a Stored Procedure

You create stored procedures in the SQL Server Management Studio using the CREATE PROCEDURE statement, followed by the code that makes up the stored procedure.
CREATE PROCEDURE StoredProcedureName AS
...
The following code creates a stored procedure called "MyStoredProcedure":
CREATE PROCEDURE MyStoredProcedure AS
SET ROWCOUNT 10
SELECT Products.ProductName AS TenMostExpensiveProducts, Products.UnitPrice
FROM Products
ORDER BY Products.UnitPrice DESC
Once you run this code in the SQL Server Management Studio, the stored procedure is created and appears under the "Stored Procedures" node.

Modifying a Stored Procedure

If you need to modify an existing stored procedure, you simply replace the CREATE with ALTER.
ALTER PROCEDURE MyStoredProcedure AS
...

Running a Stored Procedure

You can run a stored procedure by using EXECUTE or EXEC. For example, to run the above stored procedure, type the following:
EXEC MyStoredProcedure
If the stored procedure has spaces in its name, enclose it between double quotes:
EXEC "My Stored Procedure"
If your stored procedure accepts any parameters, they are placed after the procedure name:
EXEC MyStoredProcedure @ParameterName="MyParameter"
So, here's an example:
EXEC SalesByCategory @CategoryName ="Beverages"

Using The GUI

You can also use the graphical user interface to initiate the execution of a stored procedure.
To initiate a stored procedure this way:
  1. Navigate to the stored procedure
  2. Right click on the stored procedure and select "Execute Stored Procedure...":
    Initiating the execution of a stored procedure
  3. A dialog will appear. Enter your chosen parameter values etc:
    Initiating the execution of a stored procedure
  4. Click "OK"
  5. SQL Server will now generate the SQL code and execute the stored procedure:
    Executing a stored procedure

Parameters

A parameter is a value that your stored procedure uses in order to perform it's task. When you write a stored procedure, you can specify the parameters that need to be supplied from the user. For example, if you write a stored procedure to select the address details about an individual, your stored procedure needs to know which individual to select. In this case, the user can provide an IndividualId or UserId to tell the stored procedure which individual to look up.

System Stored Procedures

SQL Server includes a large number of system stored procedures to assist in database administration tasks. Many of the tasks you can perform via SQL Server Management Studio can be done via a system stored procedure. For example, some of the things you can do with system stored procedures include:
  • configure security accounts
  • set up linked servers
  • create a database maintenance plan
  • create full text search catalogs
  • configure replication
  • set up scheduled jobs
  • and much more.

SQL Server - Views

In SQL Server, a view is a pre-written query that is stored on the database. A view consists of a SELECT statement, and when you run the view, you see the results of it like you would when opening a table. Some people like to think of a view as a virtual table. This is because a view can pull together data from multiple tables, as well as aggregate data, and present it as though it is a single table.

Benefits of Views

A view can be useful when there are multiple users with different levels of access, who all need to see portions of the data in the database (but not necessarily all of the data). Views can do the following:
  • Restrict access to specific rows in a table
  • Restrict access to specific columns in a table
  • Join columns from multiple tables and present them as though they are part of a single table
  • Present aggregate information (such as the results of the COUNT function)

Accessing Views

Any view that you create ends up being located under the "Views" folder of your database.
The following screenshot shows a number of views that are located within the "AdventureWorks2008" database:
Accessing a view

Creating a View

You create a view by using the CREATE VIEW statement, followed by the SELECT statement.
CREATE VIEW ViewName AS
SELECT ...
Example:
CREATE VIEW "Alphabetical list of products" AS
SELECT Products.*, Categories.CategoryName
FROM Categories INNER JOIN Products ON Categories.CategoryID = Products.CategoryID
WHERE (((Products.Discontinued)=0))

Modifing a View

You can modify an existing view by using using ALTER instead or CREATE.
Example:
ALTER VIEW "Alphabetical list of products" AS
SELECT Products.*, Categories.CategoryName
FROM Categories INNER JOIN Products ON Categories.CategoryID = Products.CategoryID
WHERE (((Products.Discontinued)=0))
You can also right click on the view and select "Design".

Running a View

You run a view by using a SELECT statement.
SELECT TOP 1000 *  
FROM [AdventureWorks2008].[Sales].[vIndividualCustomer]
You can also right-click on the view and select "Select Top 1000 Rows".
Running the above view results in this:
Results of a view As you can see, it looks just like you've selected rows from a table. The difference is that, each column could potentially be coming from a different table.

SQL Server - Query Designer

In the previous lesson, we created a SQL script using SQL Server Management Studio (SSMS). In this lesson, we will look at how to write SQL scripts using the graphical query designer.

About The Query Designer

The graphical query designer is a graphical user interface that allows you to build queries to run against your SQL Server database. This can be particularly useful when building complex queries that involves many tables, views etc.
The query designer can also be beneficial for those who are learning how to write SQL. This is because you don't need to remember the SQL syntax in order to write queries against your database - the query designer generates the SQL for you.

Building Your Queries

To build a query with the query gesigner:
  1. Select Query > Design Query in Editor...:
    Accessing the Query Designer
  2. Add the tables you want to run the query against. In this case, we only have one table to choose from.
    Accessing the Query Designer
  3. Select the column/s you want to display in your query:
    Query Designer
  4. Click "OK"
Once you've clicked OK, you will find the query has been added to your workspace. You can then run it as you would any other query.

SQL Server - Adding Data

In the previous lesson, we created a new table in our database. We now have a database table, columns and all, but with no data.

Editing Table Rows

We can use the "Edit Top 200 Rows" option to add data to our table.
  1. To use this option, right click on the table you wish to open, and select "Edit Top 200 Rows":
    Screenshot of editing the top 200 rows of a table in SSMS
  2. You can now start entering the data directly into your table.
    Note that you don't need to enter data into the IndividualId and DateCreated columns. This is because the they will be populated automatically (remember, we set IndividualId to "Is Identity" and DateCreated to "GetDate()"))
    Screenshot of editing the top 200 rows of a table in SSMS

Disadvantages of Entering Data Directly to your Table

The above method is fine if you only have a small amount of data to enter or update. If you have a lot of data to enter, this could become very tedious. Also, if you have multiple environments (for example, a development environment, staging environment, and production environment), with duplicate databases configured in each environment, you will need to re-enter the same data into each environment.
When you're first learning SQL Server, this may not be a major concern. However, in an environment such as described, entering data directly into the table becomes quite inefficient.

A Better Method - SQL Scripts

In most cases, you will probably find it more efficient to write a SQL script. Using a script enables you to re-enter the data as many times as you like. If you need to rebuild your database for any reason, you can simply run your script to enter the data. If you have multiple environments, once again you can run your script against each environment.
Once you get used to writing and running scripts, you will probably find it quicker than entering data directly into the table.

Database Driven Websites

When you create a database driven website, you actually write SQL scripts to insert data into the database. You also use SQL scripts to read data, update data, and delete data. These SQL scripts can be placed directly into your website code (PHP, ColdFusion etc), or they can be stored in the database as Stored Procedures or Views.
In the next lesson, we will look at how to write and execute SQL scripts within SQL Server.

SQL Server - Create a Table

This lesson demonstrates how to create a table in a SQL Server database using SQL Server Management Studio (SSMS).
  1. Ensuring you have the right database expanded, right click on the "Tables" icon and select "New Table...":
    Creating a table in SQL Server - step 1
  2. While you have this screen open, do the following:
    1. Using the values in the screenshot, complete the details in the "Column Name" column, the "Data Type" column, "Length" column, and "Allow Nulls" column.
    2. Make the IndividualId column an "identity column", by setting "Is Identity" to "Yes" (this option is under the "Identity Specification" section in the bottom pane). Note that to set values in the bottom pane, you need to select the column name in the top pane first). This column is going to be an auto-number column - it will contain an incrementing number for each record that is created.
    3. Set the "Default Value" of the DateCreated column to (getdate()). (This will automatically insert the current date into that field for each new record).
    Creating a table in SQL Server - step 2 What we are doing at this stage, is creating the column names, specifying the type of data that can be entered into them, and setting default values. Restricting the data type for each column is very important and helps maintain data integrity. For example, it can prevent us from accidentally entering an email address into a field for storing the current date.
  3. Save the table by selecting File > Save Table_1:
    Save table as...
  4. When prompted, name your table:
    Creating a table in SQL Server - step 3

Your New Table

Now that you've created a new table, it will appear under your database in the "Tables" section.
Viewing the contents of your table

SQL Server - Create a Database

One of the first things we should look at with SQL Server/Management Studio is how to create a database. After all, most of the tasks you perform with SQL Server will evolve around one or more databases.

System Databases

If you've only just installed SQL Server, you might notice that some databases have already been created. These are system databases.
DatabaseTypeDescription
masterSystem databaseStores system level information such as user accounts, configuration settings, and info on all other databases.
modelSystem databaseThis database is used as a template for all other databases that are created.
msdbSystem databaseUsed by the SQL Server Agent for configuring alerts and scheduled jobs etc
tempdbSystem databaseHolds all temporary tables, temporary stored procedures, and any other temporary storage requirements generated by SQL Server.
We will now create another database for our own use.

Creating a New Database

The following steps demonstrate how to create a database in SQL Server using SQL Server Management Studio.
  1. Right click on the "Databases" icon and select "New Database...":
    Creating a new database in SQL Server - step 1
  2. Name your database and click "OK":
    Creating a new database in SQL Server - step 2

Your New Database

You will now notice your new database appears under the "Databases" section of SQL Server Management Studio.
Your new database is based on the "Model" database. The Model database is a system database which is used as a template whenever a new database is created. If you use the left pane to navigate to your database and expand the tree, you will notice that your database already contains a number of objects. For example, it already contains system functions, system views, system stored procedures, and (hidden) system tables. These are system objects which provide information about the database.
Creating a new database in SQL Server - step 3

Other Options

We have just created a database using the default options. When we created the database, a "Data File" and a "Transaction Log" were created. They were created in the default location for our server.
If we'd wanted to, we could have specified a different location for these files. We also could have changed specifications on whether to allow the file to grow automatically (as it stores more and more data), and if so, how that growth should be managed. We could have done that at step 2. But all is not lost. We can still do it now that we've created the database. We can do it via the Properties dialog box.
To view or change the database properties, simply right click on the database and select "Properties":
Creating a new database in SQL Server - step 4 The Properties dialog contains a large number of options for changing the configuration of your database. For now, we can leave everything at its default setting.
Creating a new database in SQL Server - step 5

SQL Server Management Studio (SSMS)

SQL Server Management Studio (SSMS) is the main administration console for SQL Server.
SSMS enables you to create database objects (such as databases, tables, views etc), view the data within your database, you can configure user accounts, transfer data between databases, and more.
Here's what SQL Server Management Studio looks like when you first open it up:
Screenshot of SQL Server Management Studio The left pane contains the Object Explorer. The Object Explorer provides navigation to databases, server objects (such as triggers), log files, and more.
The right pane allows you to write queries against the database and view the results. In this screenshot I have opened a blank query by clicking the "New Query" button. You can also bring up other windows, such as the Properties window.
Note that I have minimized the size of the window for this screenshot. Once maximized, you have much more room to play with.
You can use SQL Server Management Studio to create as many databases as you like. You can also connect to as many databases on as many servers as you like.
Most of the tasks performed with SQL Server Management Studio are initiated either from the top menu, or by right-clicking on an icon/object.
Throughout most of this tutorial, we'll be looking at the various things you can do via SQL Server Management Studio.

SQL Server 2008 Editions

If you are serious about installing (or upgrading) SQL Server, this page provides a quick overview of your options.
SQL Server 2008 comes in many different editions. The edition you choose will depend on your requirements. If you are looking for a free database management system, you will need to choose one of the Express editions or the Compact edition. You could also try the Evaluation edition, which allows you to trial SQL Server 2008 for 180 days.

The Editions

Here are the different editions available for SQL Server 2008.
  • Enterprise Edition
    Data management and business intelligence platform providing enterprise class scalability, high availability, and security for running business-critical applications
  • Standard Edition
    Data management and business intelligence platform providing ease of use and manageability for running departmental applications
  • Workgroup Edition
    Data management and reporting platform providing secure, remote synchronization, and management capabilities for running branch applications
  • Developer Edition
    May be installed and used by one user to design, develop, test, and demonstrate your programs on as many systems as needed
  • Web Edition
    A low-TCO, scalable, and manageable database option for web hosters and end customers looking to deploy publicly facing web applications and services
  • Express Edition
    A free edition of SQL Server ideal for learning and building desktop and small server applications and for redistribution by ISVs
  • Compact Edition
    A free, SQL Server embedded database ideal for building stand-alone and occasionally connected applications for mobile devices, desktops, and web clients
  • Evaluation Edition
    This edition may be installed for demonstration and evaluation purposes until an expiration period of 180 days.

SQL Server Edition in this Tutorial

The examples in this tutorial were made using the (free) Express edition of SQL Server 2008. To be more precise, it is SQL Server 2008 Express with Advanced Services.
SQL Server 2008 Express with Advanced Services includes the following features:
  • SQL Server database engine - create, store, update and retrieve your data
  • SQL Server Management Studio Basic - visual database management tool for creating, editing and managing databases
  • Full-text Search - powerful, high-speed engine for searching text-intensive data
  • Reporting Services - integrated report creation and design environment to create reports
The Express edition also comes as SQL Server 2008 Express with Tools, which only includes the SQL Server database engine and the SQL Server Management Studio Basic, and SQL Server 2008 Express (Runtime Only) which doesn't include the SQL Server Management Studio.
While the free version of SQL Server does have its limitations, it is a good start for those starting out with SQL Server.
You can download SQL Server 2008 Express from Microsoft's website.

About SQL Server

Microsoft SQL Server is a Relational Database Management System (RDBMS) designed to run on platforms ranging from laptops to large multiprocessor servers. SQL Server is commonly used as the backend system for websites and corporate CRMs and can support thousands of concurrent users.
SQL Server comes with a number of tools to help you with your database administration and programming tasks.
SQL Server is much more robust and scalable than a desktop database management system such as Microsoft Access. Anyone who has ever tried using Access as a backend to a website will probably be familiar with the errors that were generated when too many users tried to access the database!
Although SQL Server can also be run as a desktop database system, it is most commonly used as a server database system.

Server Database Systems

Server based database systems are designed to run on a central server, so that multiple users can access the same data simultaneously. The users normally access the database through an application.
For example, a website could store all its content in a database. Whenever a visitor views an article, they are retrieving data from the database. As you know, websites aren't normally limited to just one user. So, at any given moment, a website could be serving up hundreds, or even thousands of articles to its website visitors. At the same time, other users could be updating their personal profile in the members' area, or subscribing to a newsletter, or anything else that website users do.
Generally, it's the application that provides the functionality to these visitors. It is the database that stores the data and makes it available. Having said that, SQL Server does include some useful features that can assist the application in providing its functionality.

UI Automation with WPF

How to make TextBlock within a DataTemplate visible to UI Automation

WPF intentionally hides TextBlocks that are inside a DataTemplate to improve performance. To make them visible, you have to replace them by a Label which can be a performance hit, or you make a special UiAutomationTextBlock that overrides this behavior:
 
public class UiAutomationTextBlock : TextBlock
{
    protected override AutomationPeer OnCreateAutomationPeer()
    {
        return new ModifiedTextBlockAutomationPeer(this);
    }
 
    private class ModifiedTextBlockAutomationPeer : TextBlockAutomationPeer
    {
        public ModifiedTextBlockAutomationPeer(TextBlock textBlock)
            : base(textBlock)
        { }
 
        protected override bool IsControlElementCore()
        {
            return true;
        }
    }
}
 
 
The Idea for that solution was found here

Graphical WPF Designers

Debugging and Development Utilities

XAML Converters

How to Import Photoshop Files into WPF

Introduction

In a WPF project you often have to deal with assets that are created by a designer. They are often part of Adobe Illustrator or Photoshop files. There are two ways to achieve this goal. You can
  • Directly Import Photoshop Files in Expression Blend
    This is the most simple approach if you have Expression Blend 3. Drawbacks are, that you can only import vector shapes as Path elements. If you want to add these shapes into the resources, you have to manually extract the geometries.
  • Import Photoshop Files in Expression Design and Export to XAML
    This approach requires Expression Design to be installed but works already with Version 2.0. The workflow is a bit more complex, but you can choose if you want to export your shapes as Paths or Geometries.

Preparing the Photoshop File

If you create graphical assets that should be integrated to a WPF project you should think of the following rules to make the import easier.
  • Combine multiple vector shapes to one shape
  • Don't use layer effects, because they are mostly ignored by the converter
  • Set the color dept to 8Bit. Higher color depts cause errors on import

Directly Import Photoshop Files in Expression Blend

The following tutorial explains how to directly import an Adobe Photoshop file into WPF.
  • 1. Start Expression Blend and open an empty WPF project
  • 2. Choose "Import Adobe Photoshop File..." from the "File" menu.
  • 3. Select the file you want to import to Blend
  • 4. Check or uncheck the layers you want to import and click [OK].
  • 5. Blend imports the file now. Each layer will create a new Image or Path element. The images will automatically be added to a separate folder in your project.

How to convert Path to Resources

To convert imported Path objects to Geometry objects that can be consumes as resources you have to do the following steps:
  • 1. Copy the content of the "Data" attribute to the clipboard
  • 2. Create a StreamGeometry object and paste the content between the opening and closing tag. Give the geometry a unique key.

Import Photoshop Files in Expression Design and Export to XAML

The following tutorial explains how to import Adobe Photoshop Files in Expression Design and Export to XAML to use it in Expression Blend.
  • 1. Start Expression Blend and open the desired Photoshop File
  • 2. Check or uncheck the layers you want to import and click [OK].
  • 3. Open the Options Dialog in "Edit->Options->Clipboard(XAML)" and choose if you want to export shapes as Canvas or Resource Dictionaries.
  • 4. Select the objects you want to export and choose "Copy XAML" in the Edit menu.
  • 5. Go back to Expression Blend andpPaste the XAML snippet from the clipboard.

Expression Blend 2 AddIns

Introduction

Expression Blend has an add-in model that allows developers to write add-ins to extend its functionality. Unfortunately this is mainly unknown to most users of blend. A prominent example is the intellisense addin for blend.

Popular Addins

How to Load and Run an AddIn

To load and run an addin into expression, just pass a command parameter -addin followed the name of the add-in dll. You don't need any registration of plug-ins, but you can load only one addin on the same time. To get over this limitation you can use an add-in manager that dynamically loads multiple plugins even without restarting the tool.

C:\Program Files\Microsoft Expression\Blend 2\Blend.exe /addin:NameOfTheAddin.dll
 

Prototyping with Expression Blend 3 + SketchFlow

Introduction

Software is getting even more complex, interactive and dynamic. It's nearly impossible, for a developer to find the best solution at first time. If you start writing code without testing the concept, you can throw away a lot of valuable time by following a suboptimal approach.

The idea of SketchFlow

The idea of SketchFlow is to combine the simpleness and informality of sketching with paper and pencil with the dynamic of interactive prototypes. Most prototyping tools focus on creating high quality assets, but the main purpose of most prototypes is to proove the interaction design of your software and not to impress your management.
To address this point the Team of SketchFlow has created special styles for controls that look as if they where drawn by pencil. This focus the discussion on real interaction problems instead of discussing about details like colors or fonts and suggests that the screen design is still changeable with a small effort.

Workspace setup

Expression Blend 3 includes a workspace management feature that allows you to hide all panels you don't need to sketch your prototype. You can save your settings by choosing "Save as new Workspace" from the "Windows" menu.

Create a new SketchFlow project

In Blend 3.0 you find two new project types to create prototypes for WPF or Silverlight.

Create and connect screens



Drop sketchy controls onto screens

In the asset library you will find a new category "SketchFlow". It contains a section called "Sketch Styles". It contains special styles for most WPF or Silverlight controls that look like they are drawn by hand.




Extract common parts to component screens

If you have a part that is used on multiple screens, like a navigation or a login box, you can extract it to a component screen. This screen can be referenced on multiple screens. If you modify the component screen, the changes are reflected to all screens.






Wire-up navigation



Add sample data

A prototype needs to provide sample data to make it understandable. In flash prototypes, sample data is often just a bunch of textblocks and images. In SketchFlow we have a sample data generator, that creates real data sources that can be bound to lists and elements as real data. Selection, scrolling and sorting all works.


In the Data panel you find a database icon. It allows you to create a new sample data source.


Give the sample data source a name and specify the scope of visibility. This can be only on this screen or within the whole prototype. This can be useful, if you want to use the same demo data on multiple screens.


After creating the demo data source, you can define how a data record should look like. You can add properties or most popular types and choose from a variety of sample data types. There are URL's, e-mail adresses, bild text (lorem ipsum), images, and much mure...




Add assets from Photoshop, Illustrator or PowerPoint





Test prototype and get feedback









Generate prototype documentation

When you have finished your prototype, you can generate a design documentation by a single click. It includes the a table of content, the flow map and screenshots of all screens and components. It's not meant to use it as a complete documentation, but it's a good start to build on.