Beginning C# 2005 Databases From Novice to Professional phần 2 pot

52 295 0
Beginning C# 2005 Databases From Novice to Professional phần 2 pot

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

16. Now try to execute the query in the SQL edit window (or try anything to access the instance, if you’ve already shut the window). There will be no Results window, and the Messages window will report an error, as in Figure 2-16. CHAPTER 2 ■ GETTING TO KNOW OUR TOOLS 27 Figure 2-14. A database diagram Figure 2-15. S topping an SSE instance 777Xch02final.qxd 11/18/06 3:43 PM Page 27 17. To start SQLEXPRESS, right-click the instance node in Object Explorer, click Start, and click Yes in the confirmation message box. Click the Northwind node and retry the query. It should work fine. 18. To restore the Northwind database, right-click the Northwind node in Object Explor er, than click Tasks ➤ Restor e ➤ Database… . You’ll see the window in Figure 2-17. 19. Click OK and you’ll get the message box in Figure 2-18. You can’t restore the Northwind database, because the kind of restore you’re doing requires exclusive access, and SSMSE is currently connected to Northwind through the query. Click OK to close the message bo x, then close the SQL edit window (click No when prompted to save it). 20. Click OK again in the Restore Database window. You should get the message box sho wn in F igure 2-19. To see that the database has been restored, try to expand the Database Diagrams node under Northwind. You saved a diagram earlier, but once again the plus sign disappears, since the database diagram was stored in Northwind in step 14, but you backed up the database in steps 7 and 8, so you’ve restored Northwind to a state before the diagram was saved. The same holds true for any data in any SSE database. CHAPTER 2 ■ GETTING TO KNOW OUR TOOLS28 Figure 2-16. Error on trying to query a stopped SSE instance 777Xch02final.qxd 11/18/06 3:43 PM Page 28 CHAPTER 2 ■ GETTING TO KNOW OUR TOOLS 29 Figure 2-17. Restoring the Northwind database Figure 2-18. Restore-failure message box Figure 2-19. M essage bo x stating the N or thwind database is restored 777Xch02final.qxd 11/18/06 3:43 PM Page 29 Configuring VCSE VCSE offers a significant subset of Visual Studio 2005 functionality, limited to C# pro- gramming and not including templates for building ASP.NET applications (for which another Express IDE, Visual Web Developer 2005 Express, is available). We assume since you know C# that you have some experience with either Visual Studio or VCSE, so we’ll only cover the specific things you should do with VCSE to make the examples in this book easiest to work with. Let’s do these few simple things with VCSE: 1. To open VCSE, click Start ➤ All Programs ➤ Microsoft Visual C# 2005 Express Edi- tion. You should see the screen shown in Figure 2-20. CHAPTER 2 ■ GETTING TO KNOW OUR TOOLS30 Figure 2-20. Opening VCSE 777Xch02final.qxd 11/18/06 3:43 PM Page 30 2. VCSE is a highly configurable IDE that each user typically adapts to personal pref- erences. We use it in a rather vanilla way and don’t at all try to exploit its many features that can significantly enhance developer productivity, because what seems the best way to one user may seem the worst way to another. But, we do customize the configuration a bit, because we want to control how we indent code and where we store our VCSE solutions. You don’t have to do this, but here’s what we did, if you’d like to be consistent. Click Tools ➤ Options… to open the window, as in Figure 2-21. 3. Check the “Show all settings” check box, then expand the Projects and Solutions node. Click the General node and change the “Visual Studio projects location” to C:\bcs2005db\solutions, as in Figure 2-22. 4. Expand the Text Editor node. Expand the All Languages node and click the Tabs node . Make the information consistent with Figure 2-23. This changes tabs and indentations to thr ee spaces. Click OK. CHAPTER 2 ■ GETTING TO KNOW OUR TOOLS 31 Figure 2-21. Opening VCSE Options 777Xch02final.qxd 11/18/06 3:43 PM Page 31 5. You’ll need a connection to the SSE Northwind database, so let’s create one. Click View ➤ Other Windows ➤ Database Explorer to open Database Explorer (which is v er y similar to SSMSE’s Object Explorer). Notice in Figure 2-24 that there are no D ata C onnections nodes. CHAPTER 2 ■ GETTING TO KNOW OUR TOOLS32 Figure 2-22. Changing the default folder for VCSE projects Figure 2-23. S etting VCSE tabs and indentations 777Xch02final.qxd 11/18/06 3:43 PM Page 32 6. Right-click the Data Connections node and click Add Connection… . The Add Connection window appears, as in Figure 2-25. Note that the data source is a SQL Server database file and will be accessed through SqlClient (the .NET data provider for SQL Server, to be covered in Chapter 4). The connection will use W indo ws A uthentication, meaning any user who can log in to the server machine can connect to the N or thwind database . CHAPTER 2 ■ GETTING TO KNOW OUR TOOLS 33 Figure 2-24. Database Explorer with no connections 777Xch02final.qxd 11/18/06 3:43 PM Page 33 7. You need to specify a database to connect to. Browse to the Northwind database in C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\northwnd.mdf, and select it. Then click Test Connection. A message box should appear telling you “Test connection succeeded.” Click OK to close it. Click OK to save the connec- tion. A northwnd.mdf node will appear in Database Explorer. Expand the node, and you’ll see nodes in Figure 2-26 that are very similar to the ones in Figure 2-14 for SSMSE. CHAPTER 2 ■ GETTING TO KNOW OUR TOOLS34 Figure 2-25. Add Connection Figure 2-26. An expanded connection in Database Explorer 777Xch02final.qxd 11/18/06 3:43 PM Page 34 8. Expand the Tables node and right-click the Employees node. You’ll see the menu in Figure 2-27. Database Explorer basically offers the same features for database access and management as SSMSE’s Object Explorer, but it doesn’t offer adminis- tration functions for managing instances. VCSE’s New Query has a slightly differ- ent format from SSMSE’s New Query, but it’s as powerful. VCSE’s Show Table Data has the same format and functionality as SSMSE’s Open Table. Using BOL BOL is the official source of SQL Server documentation. The documentation for SQL S erver Express is part of BOL. BOL is a hypertext application that is both a blessing and a bane for SQL S erver users. It’s an enormously rich source of tutorials as well as refer- ence infor mation, but despite the fact that it’s extensively indexed, finding what you need to kno w can sometimes be quite frustrating. CHAPTER 2 ■ GETTING TO KNOW OUR TOOLS 35 Figure 2-27. An expanded table menu in Database Explorer 777Xch02final.qxd 11/18/06 3:43 PM Page 35 We’ll only point out a few things to help you get started: 1. To open BOL, click Start ➤ All Programs ➤ Microsoft SQL Server 2005 ➤ Docu- mentation and Tutorials ➤ SQL Server Books Online. You should see the screen in Figure 2-28. 2. There are five nodes in the Contents tree. The third one leads to SSE-specific doc- umentation, but unless you plan to use SSE exclusively as a production server, you probably won’t find this very useful. What you’ll find most useful is the first node, SQL Server 2005 Books Online, so expand it. Then expand the SQL Server Lan- guage Reference node and the Transact-SQL Reference node within it. Scroll up to the top, and you’ll see a screen as in Figure 2-29. All T-SQL language compo- nents are listed alphabetically. CHAPTER 2 ■ GETTING TO KNOW OUR TOOLS36 Figure 2-28. Opening BOL 777Xch02final.qxd 11/18/06 3:43 PM Page 36 [...]... also numeric data types) map to a single C# type, decimal (see Table 3-8) Table 3-8 T-SQL Money Data Types SQL Data Type C# Type Description money decimal Values from - 922 ,337 ,20 3,685,477.5808 through 922 ,337 ,20 3,685,477.5807 smallmoney decimal Values from -21 4,748.3648 through 21 4,748.3647 Character String Data Types All six T-SQL character string data types map to the C# string type (see Table 3-9)... support most of the other topics in the list) 37 777Xch02final.qxd 38 11/18/06 3:43 PM Page 38 CHAPTER 2 s GETTING TO KNOW OUR TOOLS Figure 2- 30 BOL Index 4 The index redisplays entries as you type, so if you enter “sel” (it’s not case sensitive), you’ll see the screen in Figure 2- 31 777Xch02final.qxd 11/18/06 3:43 PM Page 39 CHAPTER 2 s GETTING TO KNOW OUR TOOLS Figure 2- 31 BOL Index entries for... expressions don’t have to be If you work with another version of SQL, please refer to its documentation for specifics In addition to these operators, the LIKE operator (see Table 3 -2) allows you to match patterns in character data As with all SQL character data, strings must be enclosed in single quotes (') Table 3 -2 The LIKE Operator Operator Description Example LIKE Allows you to specify a pattern... Types SQL Data Type C# Type Description char string Fixed-length string of 1 to 8,000 bytes nchar string Fixed-length Unicode string of 1 to 4,000 bytes text string Variable-length string of 1 to 23 1-1 characters ntext string Variable-length Unicode string of 1 to 23 0-1 bytes varchar string Variable-length string of 1 to 23 1-1 bytes nvarchar string Variable-length Unicode string of 1 to 23 1-1 bytes 61 777Xch03final.qxd... data from Jan 1, 1900, through June 6, 20 79, accurate to the minute Binary Data Types All three T-SQL binary data types map to C# byte arrays (see Table 3-11) Table 3-11 T-SQL Binary Data Types SQL Data Type C# Type Description binary byte[] Fixed-length binary data of 1 to 8,000 bytes image byte[] Variable-length binary data of 0 to 23 1-1 bytes varbinary byte[] Variable-length binary data of 0 to 23 1-1...777Xch02final.qxd 11/18/06 3:43 PM Page 37 CHAPTER 2 s GETTING TO KNOW OUR TOOLS Figure 2- 29 T-SQL Language Reference Table of Contents 3 Click the Index tab below the Contents tree You’ll see the screen in Figure 2- 30 The “Look for:” text box is where you specify what to search for Above it is a “Filtered by:” drop-down Click the down arrow and click SQL Server Express, to keep search results to a minimum... you’re able to search BOL as well as the rest of us Good luck! 39 777Xch02final.qxd 40 11/18/06 3:43 PM Page 40 CHAPTER 2 s GETTING TO KNOW OUR TOOLS Figure 2- 32 Alternative BOL Index results Summary In this chapter, we covered just enough about SSMSE to get you familiar with the kinds of things you’ll do with it later in this book Among other things, we showed how to execute T-SQL, back up and restore a... showed you how to configure a few VCSE options to be consistent with the ones we use in writing our example programs in VCSE We also showed you the basics of using BOL Now that your tools are installed and configured, you can start learning how to do database programming by learning the basics of T-SQL 777Xch02final.qxd 11/18/06 3:43 PM Page 41 CHAPTER 2 s GETTING TO KNOW OUR TOOLS HOW TO RECOVER NORTHWIND,... you to specify criteria for selecting rows This clause can be complex, but we’ll stick to a simple example for now The syntax for our example is WHERE where is a comparison operator (for example, =, , >, or . database. CHAPTER 2 ■ GETTING TO KNOW OUR TOOLS28 Figure 2- 16. Error on trying to query a stopped SSE instance 777Xch02final.qxd 11/18/06 3:43 PM Page 28 CHAPTER 2 ■ GETTING TO KNOW OUR TOOLS 29 Figure 2- 17 as in Figure 2- 16. CHAPTER 2 ■ GETTING TO KNOW OUR TOOLS 27 Figure 2- 14. A database diagram Figure 2- 15. S topping an SSE instance 777Xch02final.qxd 11/18/06 3:43 PM Page 27 17. To start SQLEXPRESS,. VCSE: 1. To open VCSE, click Start ➤ All Programs ➤ Microsoft Visual C# 20 05 Express Edi- tion. You should see the screen shown in Figure 2- 20. CHAPTER 2 ■ GETTING TO KNOW OUR TOOLS30 Figure 2- 20.

Ngày đăng: 09/08/2014, 14:20

Từ khóa liên quan

Mục lục

  • Beginning C# 2005 Databases: From Novice to Professional

    • Chapter 3 Introducing SQL

    • Chapter 4 Introducing ADO.NET

Tài liệu cùng người dùng

Tài liệu liên quan