SQL Server Express for Web Developers

download SQL Server Express for Web Developers

of 14

Transcript of SQL Server Express for Web Developers

  • 8/19/2019 SQL Server Express for Web Developers

    1/30

    Microsoft® SQL Server® 2008 Express: A Free,

    Powerful Dataase for !e Develop"e#t

    Writer: Graeme Malcolm and Alistair Matthew (Content Master)

    Technical Reviewers: Eric King, Zachary Owens, Mike Wachal

    Note: A series of short “How To” videos complements this article, and

    can be found here.

    Introduction

    When you create a Web site, you frequently need a place to store data.

    It might be data input by visitors, information about products or

    services, news articles, discussions, or a whole host of other

    possibilities. Therefore most Web sites read and write data to and from

    a database and that doesn’t seem likely to change any time soon.

     There are a lot of database systems available: some cost thousands

    and others are free and there is a similarly broad range of features on

    oer. !icrosoft is well known for its advanced database solution "

    !icrosoft# $%& $erver# '((). It’s a fully featured database server that

    scales to the largest si*es, supports highly available and fault+tolerant

    congurations, and includes top+specication analysis tools. That’s

    great if you’re building a business critical application for thousands of

    simultaneous users, but may be a little over the top for developing a

    Web site for a small hardware store " right-

    ctually, $%& $erver '(() is available in a number of editions to suit all

    kinds of scenarios. /or developing Web applications, $%& $erver '(()

    01press 0dition is an ideal choice. It provides e1actly the same

    advanced database engine as the other editions of $%& $erver, with a

    few scalability restrictions 2it can only make use of a single processor

    and 3 45 of 6!, and databases are limited to 745 in si*e8. The

    database engine is completely compatible with other editions of $%&

    http://www.msdev.com/Directory/SeriesDescription.aspx?CourseId=105http://www.msdev.com/Directory/SeriesDescription.aspx?CourseId=105

  • 8/19/2019 SQL Server Express for Web Developers

    2/30

    $erver, so even if your customer or hoster requires the scalability of

    $%& $erver Web, $tandard, or 0nterprise 0dition, any database

    application you build on $%& $erver 01press 0dition will work

    unchanged when deployed to the production environment. 29ote, in

    particular, that $%& $erver '(() 01press matches the features of $%&

    $erver '(() Web 0dition. This is the edition likely to be in use at your

    hosting provider, so you are unlikely to nd you’ve used a feature that

    your hoster doesn’t support.8

    $%& $erver 01press 0dition is closely integrated with !icrosoft

    developer tools such as isual $tudio and isual Web ;eveloper

    01press, and it even supports advanced data types such as native

    spatial data, /I&0$T60! data and so on. In fact it’s surprising how

    many features of $%& $erver are included in $%& $erver 01press when

    you consider its most important feature= it’s free " for both

    development and deployment>

     There are three versions of $%& $erver 01press 0dition you can choose

    from:

    • $%& $erver '(() 01press 0dition with Tools: In this version you

    get the database engine with the $%& $erver !anagement $tudio

    5asic " this tool is all you need to create, edit, and manage

    databases.

    • $%& $erver '(() 01press 0dition with dvanced $ervices: In this

    version you get the engine, the !anagement $tudio, plus /ull+

    te1t $earch for searching te1t+intensive data, and 6eporting

    $ervices for creating powerful reports on your data.

    • $%& $erver '(() 01press 0dition 26untime ?nly8: In this version

    you only get the database engine.

     @ou can also get the $%& $erver !anagement $tudio '(() 01press as a

    separate download. This might be useful to add where you have the

    6untime ?nly edition, or to install on a separate computer for remote

    administration.

     @ou can download any of these for free from

    http:AAwww.microsoft.comAe1pressAsqlAdownload

    http://www.microsoft.com/express/sql/downloadhttp://www.microsoft.com/express/sql/download

  • 8/19/2019 SQL Server Express for Web Developers

    3/30

    Installing SQL Server Express Edition

    If you are new to $%& $erver, the easiest method of installation is viathe Web Blatform Installer " also known as the Web BI. This tool, which

    launches automatically from the Website above, streamlines the

    installation of $%& $erver. /or instance, if you need to install any pre+

    requisites, Web BI takes care of this for you.

    Web BI also can be used to install !icrosoft’s entire Web platform, if

    you wish, including $%& $erver 01press, the II$ Web server, the .90T

    /ramework, and isual Web ;eveloper '(() 01press. When you use

    this tool there is Cust one simple wi*ard to complete for all the products

    in the platform. dditionally, the tool downloads the latest version ofeach product, and the latest critical patches, and installs them for you,

    so you can be sure your platform is right up to date. In this e1ample,

    we’ll Cust stick with $%& $erver 01press.

  • 8/19/2019 SQL Server Express for Web Developers

    4/30

    When you click on DInstallE from your selected 0dition at the Web page

    above, you’ll be asked to install the Web BI " go ahead and install this

    tool.

    Fpon installation, you’ll be brought to the $%& $erver 01press

    installation page.

  • 8/19/2019 SQL Server Express for Web Developers

    5/30

    Glick Install. @ou have to accept the relevant licensing terms:

  • 8/19/2019 SQL Server Express for Web Developers

    6/30

     @ou also have to specify the authentication method to use:

  • 8/19/2019 SQL Server Express for Web Developers

    7/30

     Then the Web BI downloads and installs all the prerequisites and

    components for you:

  • 8/19/2019 SQL Server Express for Web Developers

    8/30

  • 8/19/2019 SQL Server Express for Web Developers

    9/30

    the development environment as a user who has permission to

    create database in the local instance of $%& $erver 01press. If

    you develop software using an account with minimal

    permissions, you can use $%& $erver !anagement $tudio or the

    $%&G!; command+line tool to create a $%& $erver login and

    grant the necessary permissions.

    ?n the tools menu, click $o##ect to a Dataase and then

    choose the Microsoft SQL Server ;ata$ource. The dd

    Gonnection bo1 appears:

  • 8/19/2019 SQL Server Express for Web Developers

    10/30

    In the Server %a"e bo1 you need to enter

    ServerNameInstanceName and you can use JlocalK for

    databases on the development computer. To create a new

    database enter a new name under Select or e#ter a &ataase

    #a"e. When you click '(  you’ll see this dialog:

  • 8/19/2019 SQL Server Express for Web Developers

    11/30

     The new database is created and the ;atabase 01plorer pane

    appears in the development environment:

    • Greate a database that is saved as a le in the application folder

    itself.

    With this approach, the database is created as a le and

    attached to a user+instance of $%& $erver 01press dynamically at

    run time. This removes the requirement to have administrative

    rights in the local $%& $erver 01press instance and makes it

    possible to distribute the database along with the rest of the Web

  • 8/19/2019 SQL Server Express for Web Developers

    12/30

    application proCect les, so you can work on the proCect on any

    computer with $%& $erver 01press installed. However, you

    should be cautious when using this approach as it does not

    reLect the way that database are typically congured on hosted

    Web sites, so you might need to perform additional testing

    before deploying the application to production.

     To use this approach, add a new item to the site, and choose the

    SQL Server Dataase template:

    When the process is complete you can see the new database in

    ;atabase 01plorer like this:

  • 8/19/2019 SQL Server Express for Web Developers

    13/30

    fter you’ve created your database, you can start adding tables, views,

    stored procedures and other features, all in your development

    environment. /or e1ample, to add a new table, right+click )ales and

    then click A&& %ew )ale. @ou can add columns, set data types, and

    set primary keys in a simple 4FI:

  • 8/19/2019 SQL Server Express for Web Developers

    14/30

    Creating a Data-Bound User Interface

    ?f course you must present the data stored in $%& $erver 01press onyour Web pages and frequently you must write data from those pages

    to the database. There are several approaches to accomplishing these

    tasks.

    /irst, you can write your own code to read and write data= this gives

    you a large degree of control over the process but it takes a lot of work

    and debugging time. ?f course, you could place this code in a .90T

    library, so you could reuse it in other Web sites. @ou’ll probably use

    ;?.90T obCects in this code to interact with the database and its

    stored procedures.

    If time is short, however, it’s really quick to create data+bound control

    on Web pages in !icrosoft development environments. /or e1ample,

    you can create a grid control on the Web page simply by dragging a

    table or view onto it. When you do this, the 4rid iew Tasks menu

  • 8/19/2019 SQL Server Express for Web Developers

    15/30

    appears, which you can use to congure the columns to display and

    other feature of the table:

    If that’s too simple, you can gain e1tra Le1ibility by creating a data

    source from a stored procedure or $%& statement. Then you can bindmultiple controls to it. This is a good way to display similar data on

    several pages.

     @ou can drag a $%&;ata$ource control from the ;ata section of the

    toolbo1 onto your Web page. Then, in the $%&;ata$ource Tasks list

    that appears, click $o#*+ure Data Source.

  • 8/19/2019 SQL Server Express for Web Developers

    16/30

     This starts a wi*ard in which you specify the table, view, or stored

    procedure you want to use, and other information. /or e1ample, if you

    specify a stored procedure, you may be asked for parameters:

  • 8/19/2019 SQL Server Express for Web Developers

    17/30

     The wi*ard also lets you test the query to make sure you get the

    results you e1pect.

    fter you’ve created a data source control you can bind many dierent

    types of control to it. /or e1ample, here’s a 4ridiew control being

    bound to a data source called D$ql;ata$ource3E:

  • 8/19/2019 SQL Server Express for Web Developers

    18/30

    $everal controls in the .90T /ramework allow you to edit data, and you

    can create paged and sorted displays rapidly.

    Debugging Data Access Code

    !icrosoft isual $tudio and isual Web ;eveloper both include

    powerful debugging tools that step through code line by line and

    enable you to e1amine variables, errors, and other runtime aspects.

    However, with most databases, when the script in a stored procedure

    is e1ecuted, the debugger loses control " you only get full debugging

    functionality when the procedure is complete and control returns to the

    .90T /ramework code.

     This is not true when you use $%& $erver '(() 01press: In isual

    $tudio Brofessional 0dition the debugger can step through Transact$%& code for a stored procedure Cust as if it was managed code in the

    Web site itself. This enables you to spot bugs in stored procedures that

    would otherwise take much longer to diagnose.

    When you open a stored procedure you can set break points in it, Cust

    as you would for GM or isual 5asic code:

  • 8/19/2019 SQL Server Express for Web Developers

    19/30

    9ow, start debugging the stored procedure by right+clicking it and click

    Step #to Store& Proce&ure:

  • 8/19/2019 SQL Server Express for Web Developers

    20/30

  • 8/19/2019 SQL Server Express for Web Developers

    21/30

     Accessing Data wit !"!

    $o far in this article, we’ve discussed development in isual $tudio or

    isual Web ;eveloper with .90T /ramework languages such as isual

    5asic and isual GM. However it’s also true that you can use other

    server+side technologies to access data stored in $%& $erver 01press

    and other development environments. /or e1ample, BHB developers

    can access $%& $erver databases by using the $%& $erver ;river for

    BHB 2available here: http:AAwww.codeple1.comA$%&$6BHB8.

    /urthermore, you can use whatever development environment you

    prefer, from 9otepad up, to write code for these technologies.

    /or e1ample, in the following screenshots a developer is writing BHB

    code in the !icrosoft 01pression Web development tool to access an

    $%& $erver 01press database. @ou could use a third+party development

    tool in e1actly the same way.

    http://www.codeplex.com/SQLSRVPHPhttp://www.codeplex.com/SQLSRVPHP

  • 8/19/2019 SQL Server Express for Web Developers

    22/30

     @ou can even write BHB code within isual $tudio " NG< $oftware

    provides the $.Bhp add+in with advanced functionality such asdebugging and Intelli$ense:

  • 8/19/2019 SQL Server Express for Web Developers

    23/30

    I haven’t space in this article to dive deeply into BHB and $%& $erver

    01press, but you should know that, once you’ve set up the driver,

    writing BHB against $%& $erver is very similar to writing BHB against

    any other database and the obCects are very well documented.

     The driver is available at this link:

    http:AAwww.codeple1.comA$%&$6BHB. /urther, you can nd a training

    kit on BHB and $%& $erver here:

    http:AAgo.microsoft.comA-linkidOPQRQ()3

    Deplo#ing a $eb Site

    When you have completed, tested, and debugged your Web site you

    must then deploy it to the production server " usually one run by your

    hosting provider " and you must deploy the database. In practice the

    database deployment method depends on the provider you use.

    Gommon methods for database deployment include:

    http://www.codeplex.com/SQLSRVPHPhttp://go.microsoft.com/?linkid=9656081http://www.codeplex.com/SQLSRVPHPhttp://go.microsoft.com/?linkid=9656081

  • 8/19/2019 SQL Server Express for Web Developers

    24/30

    • Fse the ;ata Bublishing Wi*ard to generate a Transact $%& script

     The wi*ard can generate a script that creates and populates the

    database when you run it on the hosted database server.

     To run the script some hosting providers have a Web+based

     Transact $%& script editor. @ou copy and paste your script into

    this page and e1ecute it.

  • 8/19/2019 SQL Server Express for Web Developers

    25/30

    $ome providers give you other ways to run such a script. /or

    e1ample, they may enable you to connect $%& $erver

    !anagement $tudio 5asic to the hosted database server.

    • Fpload the database as a .mdf le

    In this method you copy the .mdf le to a location on the hosted

    server. Then you must attach it to the $%& $erver instance. @ou

    might do this by using a custom Web user interface.

    • 5ackup and restore the database

    In this method you use $%& $erver !anagement $tudio to

    backup the database:

  • 8/19/2019 SQL Server Express for Web Developers

    26/30

    !ake sure you do a /ull backup and select $op- '#l- .ac/up

    to avoid disrupting your normal backup routine:

  • 8/19/2019 SQL Server Express for Web Developers

    27/30

     The backup should proceed smoothly and show a conrmation

    message.

    9e1t, upload the .bak le to the hosted server. @ou must then

    restore it to the hosted $%& $ervice instance. The provider may

    have a custom Web user interface for this purpose.

    • Fse the ;atabase Bublishing Wi*ard with the ;ata Bublishing

    $ervice.

     This wi*ard is including with isual $tudio and isual Web

    ;eveloper. @our provider must run the ;ata Bublishing $ervice on

    the server for this method to work. $tart the ;ata Bublishing

    Wi*ard by opening the ;atabase 01plorer pane, then right+click

    the database and choose Pulis to Provi&er. /irst you must

    choose the database to publish:

  • 8/19/2019 SQL Server Express for Web Developers

    28/30

     Then enter the provider details and the name of the remote

    database. @our hosting provider will provide these details,

    including credentials with which to authenticate against theservice:

  • 8/19/2019 SQL Server Express for Web Developers

    29/30

    Whichever method you have used, you should now have the databaseon the production Web server at the hosting provider. However, the

    connection string that you used to connect the Web site to the

    database during development will not work at the hoster: you’ll need

    to edit that string before the Web site can run. To edit the connection

    string open web.cong on the production server and nd the

    Sconnection$trings tag. Here’s an e1ample:

    Sconnection$trings

    Sadd nameOUdventureWorksGonnection$tring3U

    connection$tringOU;ata

    $ourceOservernameinstancename=Initial

    GatalogOdatabaseName=Integrated $ecurityOTrueU

    provider9ameOU$ystem.;ata.$qlGlientU A

    SAconnection$trings

  • 8/19/2019 SQL Server Express for Web Developers

    30/30

     @ou must ensure that the servername, instancename, and

    databaseName strings match the details supplied by the hosting

    provider.

    Su""ar-

     @ou’ve seen how capable $%& $erver '(() 01press can be and how it

    can help you write Web sites faster. In particular:

    • It’s easy to install with the Web Blatform Installer.

    •  @ou can set up and populate databases without leaving your

    development tool.

    •  @ou can bind your user interface to data very rapidly and Le1ibly.

    • With isual $tudio '(() Brofessional, you get a debugger for

     Transact $%& stored procedures.

     @ou don’t need to learn isual 5asic or isual GM= BHB can access$%& $erver 01press databases very easily.

    •  @ou get a range of tools for deploying the database to a hosting

    provider.

     This is pretty good functionality for free software.

    broader series of R+3( minute DHow ;o IE trainings 2&evel 3(38 is

    available here:

    http:AAwww.msdev.comA;irectoryA$eries;escription.asp1-GourseIdO3'7

     To nd out more and keep up+to+date with the latest information,

    bookmark the $%& $erver 01press Weblog:

    http:AAblogs.msdn.comAsqle1pressA

     There’s also more information and lots of resources on the $%& $erver

    '(() 01press product information pages:

    http:AAwww.microsoft.comAsqlserverA'(()AenAusAe1press.asp1

     @ou can nd out more about !icrosoft’s Web Blatform here:

    http:AAwww.microsoft.comAweb

    /inally, you can download the Web Blatform Installer here:

    http:AAwww.microsoft.comAwebAdownloadsAplatform.asp1

    http://www.msdev.com/Directory/SeriesDescription.aspx?CourseId=124http://blogs.msdn.com/sqlexpress/http://www.microsoft.com/sqlserver/2008/en/us/express.aspxhttp://www.microsoft.com/webhttp://www.microsoft.com/web/downloads/platform.aspxhttp://www.msdev.com/Directory/SeriesDescription.aspx?CourseId=124http://blogs.msdn.com/sqlexpress/http://www.microsoft.com/sqlserver/2008/en/us/express.aspxhttp://www.microsoft.com/webhttp://www.microsoft.com/web/downloads/platform.aspx