book applications readme.pdf
TRANSCRIPT
-
8/14/2019 Book applications readme.pdf
1/4
How to use the downloadable
applications for Murachs
ASP.NET 2.0 Web Prog ramm ingw ith C# 2005
Thank you for downloading the sample applications forMurachs ASP.NET 2.0 Web
Programming with C# 2005. If you have installed Visual Studio 2005 on your system,
and you have successfully unzipped the downloadable files, you are ready to begin
working with many of these applications. However, before you begin working with the
sample applications that use the Halloween database, you must attach this database toSQL Server Express. In addition, you must grant ASP.NET the proper access to this
database. Youll learn how to perform these tasks in this document.
How to attach the database and grant ASP.NET access to it from Windows Explorer 2How to attach the Halloween database from the command prompt 2How to grant ASP.NET access to the database from the command prompt 3How to use the sqlcmd tool to execute commands 4How to restore the original Halloween database 4How to detach the Halloween database 4
Mike Murach & Associates(559) 440-9071 (800) 221-5528
-
8/14/2019 Book applications readme.pdf
2/4
Readme for Murachs ASP.NET 2.0 Web Programmingbook applications 2
How to attach the database and grant ASP.NET
access to it from Windows Explorer
The easiest way to attach the Halloween database to SQL Server Express and grant
ASP.NET access to it is to run the batch files we provide from Windows Explorer. To do
that, just follow these steps:
1. Use Windows Explorer to navigate to the C:\Murach\ASP2CS\Database directory.
2. Double-click the db_attach.bat file to run it. This should attach a database named
Halloween to the SQL Server Express database server on the local machine.
3. Right-click the db_grant_access.sql file and select Edit to open it in a text editor.
Then, replace all occurrences of [machineName] with the name of your computer
(MikePC, for example). When youre done, save the file.
4. Double-click the db_grant_access.bat file to run it. This should grant a user named
ASPNET owner access to the Halloween database.
If you encounter problems, you can read the topics that follow to learn more aboutattaching a database and granting the necessary access to ASP.NET.
How to attach the Halloween database from the
command prompt
If you have trouble attaching the database by running the db_attach.bat file from
Windows Explorer, you can run this file from the command prompt instead. Then, you
can see any error messages that are displayed, which will help you troubleshoot any
problems that you may encounter. You can run this batch file from the command prompt
as follows:
1. Start the command prompt (StartAll ProgramsAccessoriesCommand Prompt).
2. Use the cd (change directory) command to navigate to the Database subdirectory of
the directory that holds the applications file (C:\Murach\ASP2CS) like this:
C:\Documents and Settings\Mike>cd \murach\asp2cs\database
3. Run the db_attach.bat file like this:
C:\Murach\ASP2CS\Database>db_attach.bat
The db_attach.bat file uses the sqlcmd tool to run the commands stored in the
db_attach.sql file from SQL Server Express on the local machine. To do that, this bat file
issues a single command:
sqlcmd -S localhost\SQLExpress -E /i db_attach.sql
-
8/14/2019 Book applications readme.pdf
3/4
Readme for Murachs ASP.NET 2.0 Web Programmingbook applications 3
The sql file creates a database named Halloween and attaches the primary data file
(Halloween.mdf) and the log file (Halloween_log.ldf) to that database. To do that, this
file issues these commands:
create database Halloweenon primary (filename = 'C:\Murach\ASP2CS\Database\Halloween.mdf')log on (filename = 'C:\Murach\ASP2CS\Database\Halloween_log.ldf')
for attachgo
If you decide to store the sample applications in a different directory, you can edit the
db_attach.sql file to specify the correct paths for the Halloween.mdf and
Halloween_log.ldf files.
How to grant ASP.NET access to the database from
the command prompt
If you have trouble granting ASP.NET access to the Halloween database by running
the db_grant_access.bat file from Windows Explorer, you can follow the steps below tomake sure that youve edited this batch file properly. Then, you can run this file from the
command prompt so you can see any error messages that are displayed.
1. Open the db_grant_access.sql file in a text editor.
2. Replace all occurrences of [machineName] with the name of your computer and then
save the file. For example, if you replace [machineName] with MikePC, thecommands in the file will look like this:
sp_grantlogin 'MikePC\ASPNET'go
use Halloweengo
sp_grantlogin 'MikePC\ASPNET'go
sp_grantdbaccess 'MikePC\ASPNET'go
sp_addrolemember 'db_owner', 'MikePC\ASPNET'go
exit
This grants a user named ASPNET on a computer named MikePC access to the
Halloween database.
3. Run the db_grant_access.bat file from the command prompt like this:
C:\Murach\ASP2CS\Database>db_grant_access.bat
This batch file executes the commands stored in the db_grant_access.sql file shown
above.
-
8/14/2019 Book applications readme.pdf
4/4
Readme for Murachs ASP.NET 2.0 Web Programmingbook applications 4
How to use the sqlcmd tool to execute commands
If necessary, you can use the sqlcmd tool that comes with SQL Server Express to
interactively run the commands that are stored in the sql files. That way, you can run
these commands one at a time to see if they are successful. To do that, start a command
prompt. Then, start the sqlcmd tool like this:C:\Documents and Settings\Mike>sqlcmd -S localhost\SQLExpress
Once the sqlcmd tool is started, you can enter a command like the ones shown on the
previous page. Then, you can type go to execute the command like this:
1> sp_grantlogin 'MikePC\ASPNET'2> go
To end the sqlcmd tool, just type exit.
How to restore the original Halloween database
If you need to restore the original Halloween database, you can do that by runningthe db_restore.bat file, which runs the script stored in the db_restore.sql file. This script
starts by detaching the Halloween database. Then, it copies the original database files,
which are stored in the Original Database directory, over the current files in the Database
directory. Finally, it reattaches the database.
How to detach the Halloween database
If you need to detach the Halloween database from the server, you can do that by
running the db_detach.bat file. For example, if you want to move the mdf and ldf files for
the Halloween database after youve already attached the database, youll need to detach
the database. Then, you can move the files and use an edited version of the db_attach.sqlfile to attach them later.