Beginning Microsoft Visual Basic 2008 phần 8 potx

92 361 0
Beginning Microsoft Visual Basic 2008 phần 8 potx

Đ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

611 Chapter 17: Database Programming with SQL Server and ADO.NET 4. Change the Data Source to Microsoft SQL Server (SqlClient). Put in your server name. Choose the type of authentication and user information to login to your database server. For database name, enter pubs . Click the Test Connection button to verify your choices. When you get a valid connection, click OK. 5. Now, expand the new Data Connection in Server Explorer and view the tables as shown in Figure 17 - 15 . Drag the authors table and the titleauthor table on to the designer. You will not use the titleauthor table in this Try It Out, but notice the relationship created for you by the Designer. Figure 17-14 Figure 17-15 c17.indd 611c17.indd 611 4/1/08 6:40:15 PM4/1/08 6:40:15 PM 612 Chapter 17: Database Programming with SQL Server and ADO.NET 6. Save the project. 7. On the form add a DataGridView with the default properties. Increase the width of the form and DataGridView so you can display more fields on the form. 8. Go to the code behind next. In the form1 load sub, add the following code. Dim PubsDB As New PubsDataContext Dim authors = From author In PubsDB.authors _ Where author.state = “CA” DataGridView1.DataSource = authors 9. Run the application to see all authors in California. You should see a form like Figure 17 - 16 . Figure 17-16 Figure 17-17 10. Add the following code to form load. Here you are selecting a single row from the database. Run the application and notice the new form title as shown in Figure 17 - 17 . Dim author1 = PubsDB.authors.SingleOrDefault(Function(au) au.au_id = “172-32-1176”) Me.Text = author1.au_fname + “ “ + author1.au_lname c17.indd 612c17.indd 612 4/1/08 6:40:16 PM4/1/08 6:40:16 PM 613 Chapter 17: Database Programming with SQL Server and ADO.NET 11. Add the following code to form load between the last two lines you added. Here you are selecting a single row from the database and then changing it. Run the application and note the new form title and the updated grid last name as shown in Figure 17 - 18 . Dim author1 = PubsDB.authors.SingleOrDefault(Function(au) au.au_id = “172-32-1176”) author1.au_lname = “Test” PubsDB.SubmitChanges() Me.Text = author1.au_fname + “ “ + author1.au_lname Figure 17-18 How It Works This application shows just how amazing LINQ is to developers. By writing one simple VB query and binding to a DataGridView, you were able to filter records from an object. In this case, it was an Entity class representing a database table. First, you declare a new instance of the PubsDataContext as PubsDB . PubsDB is now considered an entity. Dim PubsDB As New PubsDataContext Next, you declare an object named authors to hold the results of the query against the authors table. The query is a simple where clause for all authors in the state “ CA ” . Dim authors = From author In PubsDB.authors _ Where author.state = “CA” The third line simply binds the result of the LINQ query to the DataGridView. DataGridView1.DataSource = authors c17.indd 613c17.indd 613 4/1/08 6:40:16 PM4/1/08 6:40:16 PM 614 Chapter 17: Database Programming with SQL Server and ADO.NET The last four lines were added to allow data updates. First, you selected a single row using the SingleOrDefault method. If the database might return nothing, you need to use this method otherwise you can use the method Single . Once you get an instance of a single author, you can update any column. You updated the last name to “ Test ” . To push the changes to the database, just call SubmitChanges() . Finally, you put the new value in the title of the form: Dim author1 = PubsDB.authors.SingleOrDefault(Function(au) au.au_id = “172-32-1176”) author1.au_lname = “Test” PubsDB.SubmitChanges() Me.Text = author1.au_fname + “ “ + author1. Now, that was what you call easy. You should use the IntelliSense to view the objects, properties, and methods available to you with LINQ. It is a simple way to work with your data. In the chapter exercises, you get to research LINQ to Objects and write a query against a dictionary object. Summary This chapter covers a few very important ADO.NET classes, particularly the SqlConnection , SqlDataAdapter , SqlCommand , and SqlParameter classes. You saw firsthand how valuable these classes can be when selecting, inserting, updating, and deleting data. These particular classes are specifically for accessing SQL Server, but similar principles apply to the OLE DB counterparts. You also saw the DataSet and DataView classes from the System.Data namespace put to use, and you used both of these classes to create objects that were bound to the controls on your forms. Of particular interest to this discussion is the DataView object, as it provides the functionality to perform sorting and searching of data. The DataView class provides the most flexibility between the two classes, because you can also present a subset of data from the DataSet in the DataView . You saw how easy it is to bind the controls on your form to the data contained in either the DataSet or the DataView . You also saw how to manage the navigation of the data in these objects with the CurrencyManager class. This class provides quick and easy control over the navigation. This chapter has demonstrated using manual control over the navigation of data on the form and manual control over the insertion, update, and deletion of data in a data store. You should use the techniques that you learned in this chapter when you need finer control of the data, especially when dealing with complex table relationships such as you have dealt with here. You also got a taste of LINQ. LINQ is a topic that deserves an entire book, but you saw how powerful it is. In a few lines of code (no SQL), you were able to write a query to filter a group of objects like you can in SQL. c17.indd 614c17.indd 614 4/1/08 6:40:17 PM4/1/08 6:40:17 PM 615 Chapter 17: Database Programming with SQL Server and ADO.NET To summarize, after reading this chapter you should: Feel comfortable using the ADO.NET classes discussed in this chapter Know when to use the DataSet class and when to use the DataView class Know how to bind controls on your form manually to either a DataSet or a DataView object Know how to use the CurrencyManager class to navigate the data in a DataSet or DataView object Know how to sort and search for data in a DataView object Be familiar with the Object Relational Desiner in Visual Studio Know how to use LINQ to query an Entity Class Exercises 1. Create a Windows Forms application that will display data to the user from the Authors table in the Pubs database. Use a DataGridView object to display the data. Use the simple select state- ment here to get the data: Select * From Authors 2. Looking at the DataGridView, it is not very user - friendly. Update the column headings to make more sense. If you know SQL, you can give each column an alias. The current column header names are au_id , au_lname , au_fname , phone , address , city , state , zip , and contract . The solution to this exercise will give each column an alias in SQL. 3. Create a Windows Forms application. On form1, add a ListBox named ListBox1. On form load, create a dictionary object with key/value pairs of names and states of your friends. Now, write a query to return all of your friends in a certain state. Take your result and bind it to the ListBox using a for each loop. You may need to add a reference to System.Data.Linq . ❑ ❑ ❑ ❑ ❑ ❑ ❑ c17.indd 615c17.indd 615 4/1/08 6:40:17 PM4/1/08 6:40:17 PM c17.indd 616c17.indd 616 4/1/08 6:40:17 PM4/1/08 6:40:17 PM 18 ASP.NET As we look to the future, the Internet is sure to increase its presence in business. Developers need to gain knowledge of building robust, dynamic web sites. In this chapter, you will learn about building Web Forms applications. You will focus on the basics for web site development and move to database - driven applications. With Visual Studio 2008, you will be building data - driven sites in no time. Visual Studio 2008 is the best tool for creating ASP.NET sites on the market today. It provides you with the best Intellisense, debugging, and control library to create web sites written in Visual Basic. You can build ASP.NET web sites (sometimes referred to as Web Forms applications), web services and even sites targeted for mobile devices in VS 2008. Also, you do not need IIS or any web server to host your site with VS 2008; ASP.NET Development Server is a built - in web server you can use to host your sites while developing them. In this chapter, you will: Look at a basic overview of web applications (thin - client applications) See the advantages of Web Forms versus Windows Forms Understand the control toolbox Explore client and server processing Assess the possible locations for web sites in VS 2008 (IIS and ASP.NET Development Server) Error handling has been omitted from all of the Try It Outs in this chapter to save space. You should always add the appropriate error handling to your code. Review Chapter 10 for error - handling techniques. Before you get your first look at the code, you will have a short lesson on the building blocks developers use to create web applications. ❑ ❑ ❑ ❑ ❑ c18.indd 617c18.indd 617 4/1/08 6:40:45 PM4/1/08 6:40:45 PM 618 Chapter 18: ASP .NET Thin - Client Architecture In previous chapters, you have seen thick - client applications in the type of Windows Forms applications. Most of the processing is completed by the client application you built earlier, and many of the applications stood on their own and needed no other applications or servers. In web development, on the other hand, most of the processing is completed on the server and then the result is sent to the browser. When you develop Web Forms applications, you do not have to distribute anything to the user. Any user who can access your web server and has a web browser can be a user. You must be careful with the amount of processing you place on the client. When you design a thin - client system, you must be aware that your users or customers will use different clients to access your application. If you try to use too much processing on the client, it may cause problems for some users. This is one of the major differences between Windows and Web Forms applications. You will learn about the major difference between these two types of Visual Studio 2008 applications later in this chapter. When dealing with a Windows Forms application, you have a compiled program that must be distributed to the user ’ s desktop before they can use it. Depending upon the application, there may also be one or more supporting DLLs or other executables that also need to be distributed along with the application. In thin - client architecture, there is typically no program or DLL to be distributed. Users merely need to start their browsers and enter the URL of the application web site. The server hosting the web site is responsible for allocating all resources the web application requires. The client is a navigation tool that displays the results the server returns. All code required in a thin - client application stays in one central location: the server hosting the web site. Any updates to the code are immediately available the next time a user requests a web page. Thin - client architecture provides several key benefits. First and foremost is the cost of initial distribution of the application — there is none. In traditional client/server architecture, the program would have to be distributed to every client who wanted to use it, which could be quite a time - consuming task if the application is used in offices throughout the world. Another major benefit is the cost of distributing updates to the application — again, there is none. All updates to the web site and its components are distributed to the web server. Once an update is made, it is immediately available to all users the next time they access the updated web page. In traditional client/server architecture, the updated program would have to be distributed to every client, and the updates could take days or weeks to roll out. Thin-client architecture allows a new version of an application to be distributed instantly to all the users without having to touch a single desktop. Another major benefit is that you can make changes to the back - end architecture and not have to worry about the client. Suppose, for example, that you want to change the location of the database from a low - end server to a new high - end server. The new server would typically have a new machine name. In a traditional client/server application, the machine name of the database server is stored in the code or Registry setting. You would need to modify either the code or the Registry setting for every person who uses the application. In thin - client architecture, you simply need to update the setting of the web server to point to the new database server and you are in business, and so are all of the clients. c18.indd 618c18.indd 618 4/1/08 6:40:46 PM4/1/08 6:40:46 PM 619 Chapter 18: ASP .NET You can see that in a thin - client architecture model, any client with a browser can access your web site and immediately have access to updates. In fact, if your changes were transparent to the user, the client wouldn ’ t even know that changes had been made. Now that you have a basic understanding of thin - client architecture, look at how Web Forms work. Web Forms versus Windows Forms In this section, you will get an overview of the advantages of both Windows Forms and Web Forms. This will give you an idea of when you build each type of application to solve a customer ’ s problem. You will almost always have to choose between these two types of architecture when building solutions. It is important to understand some of the advantages of both. Windows Forms Advantages Windows Forms applications have advantages in some types of systems. Typically, applications that require a responsive interface, such as a point - of - sale system at a retail store, are Windows Forms applications. Also, in most cases, processor - intensive applications such as games or graphics programs are better suited to a Windows Forms program. A major advantage for Windows Forms is trust. When a user installs the application, it is given trust in the current zone. With this high-enough level of trust, you can store data and state about the current session on the local computer. The user can run the application and it can interact with the local file system or Registry seamlessly. Trust is very limited, however, for an Internet application. Another advantage is having control over the client application. This allows you to build a very powerful, rich, user interface. You will see that there are numerous controls not available to a Web Form (although this is becoming less of a difference) that permit the developer to create user - friendly applications. Windows Forms allow for a more ample user interface. Also, application responsiveness is an advantage with Windows Forms. With most or all of the processing being done on the client, the need to send data over the wire can be reduced. Any amount of data sent to servers can cause latency. For an application running locally on a computer, the normal events are handled more quickly. Also, the speed of data transmission over a local network is much faster than the typical Internet connection. This speed will allow data to move across the wire faster and create less of a bottleneck for the user. Web Forms Advantages The advantages of Web Forms may seem to be greater than the advantages of Windows Forms. Do not permit this to transform you into a full - time web developer for every project. There will always be times when Windows Forms are a better solution. The greatest advantage for a web application is distribution. To distribute a Web Forms application, just install it on the web server. That is it. No need to create an installation for every version of Windows and ship CDs. When you make a change, just publish the change to the web server, and the next time a customer comes to the site, he or she will use the latest application. c18.indd 619c18.indd 619 4/1/08 6:40:46 PM4/1/08 6:40:46 PM 620 Chapter 18: ASP .NET Version control, or change control, is another advantage. With all of your application code at the same location, making changes is a breeze. You never have to worry about one user on version 8 and another on version 10; all users access the same application. As soon as you publish the change, all users see the update with no user intervention necessary. Have you heard the term platform independence ? Web applications have it. It doesn ’ t matter what type of computer the user has — as long as there is a browser and a connection to your web server, the user can access your application. There is no need to build application versions for different operating systems. These advantages can add up to millions of dollars of savings over a Windows application. Being able to make quick changes and maintain one code base are great advantages. Still, there are times when a web application will not provide an adequate user experience. Make sure you evaluate both options for every project. Now, let ’ s look more closely at Web Forms development. Web Applications: The Basic Pieces In its simplest form, a web application is just a number of web pages. For the user to access the web pages, there must be a web server and browser. A request is made by the browser for the page on the server. The server then processes the web page and returns the output to the browser. The user sees the page inside the browser window. The pages that the users see may contain HyperText Markup Language (HTML), cascading style sheets (CSS), and client - side script. Finally, the page displays in the browser for the user. In this section, you will receive a basic overview of each piece of the system. Web Servers There are many web servers on the market today. The most well known web servers in use today are Microsoft Internet Information Services (IIS) and Apache. For this book, you will focus exclusively on IIS. Browsers Every user of a Web Forms application must have a browser. The four most popular browsers are Microsoft Internet Explorer (IE), Mozilla Firefox, Netscape, and Opera. When you develop public web sites, you must be aware that the site may render differently in each browser. You will find that IE is the most lenient when it comes to valid HTML. We will focus on IE 7 for this book. HyperText Markup Language Also known as HTML, this is the presentation or design layout of the web page. HTML is a tag - based language that allows you to change the presentation of information. For example, to make text bold in HTML, just place the < b > tag around the text. The following text is an example of HTML. This is < b > bold < /b > in HTML. c18.indd 620c18.indd 620 4/1/08 6:40:47 PM4/1/08 6:40:47 PM [...]... setup screen for an FTP site in Figure 18- 7 629 c 18. indd 629 4/1/ 08 6:40:50 PM Chapter 18: ASP NET Figure 18- 7 The final option is a Remote Site Again, this also may be used when you use a hosting company If your hosting company supports FrontPage Extensions, you can use this option as shown in Figure 18- 8 Figure 18- 8 Performing Data Entry and Validation One of the basic functions of almost every web site... may see different default attributes set by Visual Studio 20 08 If you work with code in the aspx page, only the Language attribute is set by Visual Studio 20 08 The Page directive has over 30 attributes that can be set I will discuss only the default attributes If you want to explore the rest, search for @Page in the help files for VS 20 08 or on http://msdn2 microsoft. com/ Take a look at the default attributes... 642 c 18. indd 642 4/1/ 08 6:40:55 PM Chapter 18: ASP NET . is the code - behind page. Visual Studio 20 08 makes creating web applications an easy task. c 18. indd 622c 18. indd 622 4/1/ 08 6:40:47 PM4/1/ 08 6:40:47 PM 623 Chapter 18: ASP .NET Controls: The. System.EventArgs) lblServer.Text = “Changed” End Sub Figure 18- 4 c 18. indd 625c 18. indd 625 4/1/ 08 6:40: 48 PM4/1/ 08 6:40: 48 PM 626 Chapter 18: ASP .NET Run the program again by pressing Ctrl+F5 and. running on the production server before going live. c 18. indd 628c 18. indd 6 28 4/1/ 08 6:40:50 PM4/1/ 08 6:40:50 PM 629 Chapter 18: ASP .NET Figure 18- 5 There are three other ways to work with web site

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

Từ khóa liên quan

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

Tài liệu liên quan