Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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 about attaching a database and granting the necessary access to ASP.NET.
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
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 Halloween on primary (filename = 'C:\Murach\ASP2CS\Database\Halloween.mdf') log on (filename = 'C:\Murach\ASP2CS\Database\Halloween_log.ldf') for attach go
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 to make 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, the commands in the file will look like this:
sp_grantlogin 'MikePC\ASPNET' go use Halloween go 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.
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