Beginning VB 2008 Databases From Novice to Professional phần 9 pdf

44 268 0
Beginning VB 2008 Databases From Novice to Professional phần 9 pdf

Đ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

Try It Out: Handling a Database Exception (Part 1): RAISERROR Here, you’ll see how to raise a database error and handle the exception. 1. Add a button to the Database tab page and change its Text property to Database Exception-1. Add a label to the right of this button, and for its Text property type Calls a stored procedure that uses RAISERROR. 2. Add a second button to the tab page, and change its Text property to Database Exception-2. Add a label to the right of this button, and for its Text property type Calls a stored procedure that encounters an error. 3. Add a third button to the tab page, and change its Text property to Database Exception-3. Add a label to the right of this button, and for its Text property type Creates multiple SqlError objects. The layout should look like Figure 16-7. Figure 16-7. Database tab page 4. Using SSMSE, create a stored procedure in Northwind named sp_DbException_1, as follows: create procedure sp_DbException_1 as set nocount on declare @ordercount int Select @ordercount = count(*) From Orders if @ordercount > 10 raiserror ( 'Orders Count is greater than 10 - Notify the Business ➥ Manager', 16, 1 ) CHAPTER 16 ■ HANDLING EXCEPTIONS 323 9470ch16final.qxd 3/15/08 2:43 PM Page 323 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 5. Add the code in Listing 16-4 to the button3_Click method. Listing 16-4. button3_Click() Dim conn As SqlConnection = New SqlConnection _ ("Data Source=.\sqlexpress;" & _ "Integrated Security=True;" & _ "database=northwind") 'create command Dim cmd As SqlCommand = conn.CreateCommand 'Specify that a stored procedure is to be executed cmd.CommandType = CommandType.StoredProcedure cmd.CommandText = "sp_DbException_1" Try 'Open connection conn.Open() 'Execute stored procedure cmd.ExecuteNonQuery() Catch ex As System.Data.SqlClient.SqlException Dim str As String str = "Source: " + ex.Source.ToString str += ControlChars.NewLine + "Number: " + ex.Number.ToString str += ControlChars.NewLine + "Message: " + ex.Message str += ControlChars.NewLine + "Class: " + ex.Class.ToString str += ControlChars.NewLine + "Procedure: " + ex.Procedure str += ControlChars.NewLine + "Line Number: " + ex.LineNumber. ➥ ToString str += ControlChars.NewLine + "Server: " + ex.Server MessageBox.Show(str, "Database Exception") Catch ex As System.Exception Dim str As String str = "Source: " + ex.Source.ToString str += ControlChars.NewLine + "Exception Message: " + ex.Message MessageBox.Show(str, "General Exception") Finally If conn.State = ConnectionState.Open Then MessageBox.Show("Finally block Closing the connection", ➥ "Finally") conn.Close() End If End Try CHAPTER 16 ■ HANDLING EXCEPTIONS324 9470ch16final.qxd 3/15/08 2:43 PM Page 324 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 6. Run the program by pressing Ctrl+F5, and then click the Database Exception-1 button. Y ou’ll see the message box in Figure 16-8. Click OK to close the message box, click OK to close the next one, and then close the window. Figure 16-8. RAISERROR database exception message Observe the caption and contents of the message box. The source, message, name of the stored procedure, exact line number where the error was found, and name of the server are all displayed. You obtain this detailed information about the exception from the SqlException object. How It Works In the sp_DBException_1 stored procedure, you first find the number of orders in the Orders table and store the number in a variable called @ordercount: select @ordercount = count(*) from orders If @ordercount is greater than ten, you raise an error using the RAISERROR statement: if @ordercount > 10 raiserror ( 'Orders Count is greater than 10 - Notify the Business Manager', 16, 1 ) Then, in the button3_Click method, you execute the stored procedure using the ExecuteNonQuery method within a Try block: Try 'Open connection conn.Open() 'Execute stored procedure cmd.ExecuteNonQuery() CHAPTER 16 ■ HANDLING EXCEPTIONS 325 9470ch16final.qxd 3/15/08 2:43 PM Page 325 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com When the stored procedure executes, the RAISERROR statement raises an error, which is converted to an exception by ADO.NET. The exception is handled by Catch ex As System.Data.SqlClient.SqlException Dim str As String s tr = "Source: " + ex.Source.ToString str += ControlChars.NewLine + "Number: " + ex.Number.ToString str += ControlChars.NewLine + "Message: " + ex.Message str += ControlChars.NewLine + "Class: " + ex.Class.ToString str += ControlChars.NewLine + "Procedure: " + ex.Procedure str += ControlChars.NewLine + "Line Number: " + ex.LineNumber.ToString str += ControlChars.NewLine + "Server: " + ex.Server MessageBox.Show(str, "Database Exception") Try It Out: Handling a Database Exception (Part 2): Stored P rocedure Error Now you’ll see what happens when a statement in a stored procedure encounters an error. You’ll create a stored procedure that attempts an illegal INSERT, and then you’ll extract infor- mation from the SqlException object. 1. Using SSMSE, create a stored procedure in Northwind named sp_DbException_2, as follows: create procedure sp_DBException_2 as set nocount on insert into employees ( employeeid, Firstname ) values (50, 'Cinderella') 2. I nsert the code in Listing 16-5 into the button4_Click method. Listing 16-5. button4_Click() Dim conn As SqlConnection = New SqlConnection _ ("Data Source=.\sqlexpress;" & _ "Integrated Security=True;" & _ "database=northwind") 'create command Dim cmd As SqlCommand = conn.CreateCommand 'Specify that a stored procedure is to be executed cmd.CommandType = CommandType.StoredProcedure cmd.CommandText = "sp_DbException_2" CHAPTER 16 ■ HANDLING EXCEPTIONS326 9470ch16final.qxd 3/15/08 2:43 PM Page 326 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Try 'Open connection conn.Open() 'Execute stored procedure cmd.ExecuteNonQuery() Catch ex As System.Data.SqlClient.SqlException Dim str As String str = "Source: " + ex.Source.ToString str += ControlChars.NewLine + "Number: " + ex.Number.ToString str += ControlChars.NewLine + "Message: " + ex.Message str += ControlChars.NewLine + "Class: " + ex.Class.ToString str += ControlChars.NewLine + "Procedure: " + ex.Procedure str += ControlChars.NewLine + "Line Number: " + ex.LineNumber.ToString str += ControlChars.NewLine + "Server: " + ex.Server MessageBox.Show(str, "Database Exception") Catch ex As System.Exception Dim str As String str = "Source: " + ex.Source.ToString str += ControlChars.NewLine + "Exception Message: " + ex.Message MessageBox.Show(str, "General Exception") Finally If conn.State = ConnectionState.Open Then MessageBox.Show("Finally block Closing the connection", "Finally") conn.Close() End If End Try 3. Run the program by pressing Ctrl+F5, and then click the Database Exception-2 button. Y ou’ll see the message box in Figure 16-9. Click OK to close the message box, click OK to close the next one, and then close the window. Figure 16-9. S tor ed procedure database exception message CHAPTER 16 ■ HANDLING EXCEPTIONS 327 9470ch16final.qxd 3/15/08 2:43 PM Page 327 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com How It Works The stored procedure tries to insert a new employee into the Employees table: insert into employees ( employeeid, firstname ) values (50, 'Cinderella') However, since the EmployeeID column in the Employees table is an IDENTITY column, you can’t explicitly assign a value to it. ■Tip Actually, you can—as the message indicates—if you use SET IDENTITY_INSERT employees OFF in the stored procedure before you attempt the INSERT. This would allow you to insert explicit EmployeeID values, but this seldom is, or should be, done. When this SQL error occurs, the specific SqlException Catch clause traps it and displays the information. The Finally block then closes the connection. It’s possible for stored procedures to encounter several errors. You can trap and debug these using the SqlException object, as you’ll see next. Try It Out: Handling a Database Exception (Part 3): Errors Collection The SqlException class SqlException class has an Errors collection property. Each item in the Errors collection is an object of type SqlError. When a database exception occurs, the Errors collection is populated. For the example, you’ll try to establish a connection to a nonexistent database and investigate the SqlException’s Errors collection. 1. I nser t the code in Listing 16-6 into the button5_Click method. N ote that y ou’re inten- tionally misspelling the database name. Listing 16-6. button5_Click() Dim conn As SqlConnection = New SqlConnection _ ("Data Source=.\sqlexpress;" & _ "Integrated Security=True;" & _ "database=northwnd") 'create command Dim cmd As SqlCommand = conn.CreateCommand CHAPTER 16 ■ HANDLING EXCEPTIONS328 9470ch16final.qxd 3/15/08 2:43 PM Page 328 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 'Specify that a stored procedure is to be executed cmd.CommandType = CommandType.StoredProcedure cmd.CommandText = "sp_DbException_2" Try 'Open connection conn.Open() 'Execute stored procedure cmd.ExecuteNonQuery() Catch ex As System.Data.SqlClient.SqlException Dim str As String Dim i As Integer For i = 0 To ex.Errors.Count - 1 Step i + 1 str += ControlChars.NewLine & "Index #".ToString & _ i & ControlChars.NewLine & _ "Exception: " & ex.Errors(i).ToString() & ControlChars.New ➥ Line & _ "Number: " & ex.Errors(i).Number.ToString() & ControlChars. ➥ NewLine Next MessageBox.Show(str, "Database Exception") Catch ex As System.Exception Dim str As String str = "Source: " + ex.Source.ToString str += ControlChars.NewLine + "Exception Message: " + ex.Message MessageBox.Show(str, "ADO.NET Exception") Finally If conn.State = ConnectionState.Open Then MessageBox.Show("Finally block Closing the connection", ➥ "Finally") End If conn.Close() End Try End Sub 2. Run the program by pressing Ctrl+F5, and then click the Database Exception-2 button. You’ll see the message box in Figure 16-10. CHAPTER 16 ■ HANDLING EXCEPTIONS 329 9470ch16final.qxd 3/15/08 2:43 PM Page 329 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Figure 16-10. Handling multiple database errors Observe that two items are found in the Errors collection, and their error numbers are different. How It Works In the connection string, you specify a database that doesn’t exist on the server; here you misspell Northwind as Northwnd: Dim conn As SqlConnection = New SqlConnection _ ("Data Source=.\sqlexpress;" & _ "Integrated Security=True;" & _ "database=northwnd") When you try to open the connection, an exception of type SqlException is thrown and you loop through the items of the Errors collection and get each Error object using its indexer: Catch ex As System.Data.SqlClient.SqlException Dim str As String Dim i As Integer For i = 0 To ex.Errors.Count - 1 Step i + 1 str += ControlChars.NewLine & "Index #".ToString & i & _ ControlChars.NewLine & _ "Exception: " & ex.Errors(i).ToString() & ControlChars.NewLine & _ "Number: " & ex.Errors(i).Number.ToString() & ControlChars.NewLine Next MessageBox.Show(str, "Database Exception") This example shows that the SqlException object carries detailed information about every SQL error in its Errors collection. Summary In this chapter, you saw how to handle exceptions thrown by ADO.NET and by the SQL Server database . In particular, you learned how to handle both single and multiple database errors with the System.Data.SqlClient.SqlException class. In the next chapter, you’ll look at transactions and how to work with events. CHAPTER 16 ■ HANDLING EXCEPTIONS330 9470ch16final.qxd 3/15/08 2:43 PM Page 330 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Working with Events Any type of application, either window based or web based, is designed and developed to help users achieve functionality and run their businesses. Users interact with applications by using input devices such as the keyboard or the mouse to provide input to these applications. Whatever users do using input devices gets translated into events that are recognized and thus cause certain actions to occur. Clicking by using a mouse is the most common task we com- puter users all do, and whenever we click, what should happen is recorded in the form of an event or an action. In this chapter, we’ll cover the following: • Understanding events • Properties of events • Design of events • Common events raised by controls • Event generator and consumer Understanding Events An event can be defined as an action that a user can respond to or that can be handled in the form of code. Usually events are generated by a user action, such as clicking the mouse or pressing a key. Events are associated with the controls you put in Windows Forms or web forms, and whenev er you code any functionality behind a control’s behavior, for example, a click of a mouse, then that associated event will be raised and the application will respond to that event. No application can be written without events. Event-driven applications execute code in r esponse to events. Each form and control exposes a predefined set of events that you can program against. If one of these events occurs and there is code in the associated event han- dler, that code is invoked. E vents enable a class or object to notify other classes or objects when something of inter- est occurs. The entire event system works in the form of the publisher and subscriber model. The class that sends or raises the event is known as the publisher, and the class that receives (or handles) that event is known as the subscriber. 331 CHAPTER 17 9470ch17final.qxd 3/3/08 5:19 PM Page 331 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com In a typical Visual Basic Windows Forms Application or web application, you subscribe t o events raised by controls such as Buttons, ListBoxes, LinkLabels, and so forth. The Visual Studio 2008 integrated development environment (IDE) allows you to browse the events that a control publishes and select the ones that you want it to handle. The IDE automatically adds an empty event handler method and the code to subscribe to the event. Properties of Events The events associated with any class or object work in some predefined manner. Here, we describe the properties of events and the way the publisher and subscriber works to achieve functionality: • The publisher determines when an event is raised; the subscriber determines what action is taken in response to the event. • An event can have multiple subscribers. A subscriber can handle multiple events from multiple publishers. • Events that have no subscribers are never called. • Events are typically used to signal user actions such as button clicks or menu selections in graphical user interfaces. • When an event has multiple subscribers, the event handlers are invoked synchronously when an event is raised. • Events can be used to synchronize threads. • In the .NET Framework class library, events are based on the EventHandler delegate and the EventArgs base class. Design of Events E vents happen either before their associated action occurs ( pr e-events ) or after that action occurs ( post-events). For example, when a user clicks a button in a window, a post-event is raised, allowing application-specific methods to execute. An event handler delegate is bound to the method to be executed when the system r aises an event. The event handler is added to the event so that it is able to invoke its method when the event is raised. Events can have event-specific data (for example, a mouse-down event can include data about the screen cursor ’ s location). The event handler signature observes the following conventions: • The return type is Void. • The first par ameter is named sender and is of type Object. This r epresents the object that r aised the ev ent. • The second par ameter is named e and is of type EventArgs or a der iv ed class of EventArgs. This represents the event-specific data. • The event takes only these two parameters. CHAPTER 17 ■ WORKING WITH EVENTS332 9470ch17final.qxd 3/3/08 5:19 PM Page 332 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... DisplayImages to your solution Rename Form1 .vb to DisplayImages .vb 2 Add a text box, a button, and a picture box to the form and set the button’s Text property to Show Image and the form’s Text property to Display Images, as in Figure 18-3 Figure 18-3 Design view of Display Images form 3 Add a new class named Images to this Windows Form project Replace the code in Images .vb with the code in Listing 18-2 351 94 70ch18final.qxd... database column 355 94 70ch18final.qxd 3/15/08 2: 49 PM Page 356 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 356 CHAPTER 18 s WORKING WITH TEXT AND BINARY DATA Try It Out: Loading Text Data from a File To load text data from a file, follow these steps: 1 Add a Visual Basic Console Application project named LoadText to the solution 2 Rename Module1 .vb to LoadText .vb, and replace... cycle (stop and restart) SSE before rerunning the program, to remove the table by re-creating an empty tempdb database You’ll see how to avoid this problem in “Working with Text Data” later in this chapter Retrieving Images from a Database Now that you’ve stored some images, you’ll see how to retrieve and display them with a Windows application Try It Out: Displaying Stored Images To display your stored... in Code view Switch to Code view of the Windows Form, named Events.cs, to view the event handler and to prepare to write the functionality for the Click event (see Figure 17-3) 94 70ch17final.qxd 3/3/08 5: 19 PM Page 335 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com CHAPTER 17 s WORKING WITH EVENTS Figure 17-3 Event handler in Code view 5 Inside the button1_Click() event handler,... also be set without using the Smart Tag feature You can Tip directly set the MultiLine property to True, which is set to False by default 6 Drag a Label control from the Toolbox to below the TextBox and set its AutoSize property to False Also, set the Label’s Font Size property to 12 and TextAlign property to MiddleCenter Now your Events form will look like the one shown in Figure 17-6 Figure 17-6 The... movements of the mouse To try them, follow these steps: 1 Navigate to Solution Explorer and open the Events form in Design view 2 Drag a TextBox control onto the Windows Form just under the button1 control Select the TextBox control, and you will see an arrow on the top-right border of the control; this arrow is called a Smart Tag 335 94 70ch17final.qxd 3/3/08 5: 19 PM Page 336 Simpo PDF Merge and Split... Button control over to the form Select the Button control, navigate to the Properties window, and for the control’s Text property type Click Me Then click the lightning bolt button located on the toolbar shown in the Properties window, and you will see the entire list of events that the Button control supports; event handlers could be written for all these events (see Figure 17-1) Also notice the tooltip... 18-2 Viewing image data 94 70ch18final.qxd 3/15/08 2: 49 PM Page 3 49 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com CHAPTER 18 s WORKING WITH TEXT AND BINARY DATA How It Works In the Main method, you do three major things You call an instance method to create a table to hold images: ' Create table loader.CreateImageTable() You call an instance method to prepare a command (yes,... C:\Documents and Settings\Toshiba User\My Documents\Visual Studio 2008\ Projects\Chapter18\Image; you can use the path of the location where you have some images in your PC 94 70ch18final.qxd 3/15/08 2: 49 PM Page 345 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com CHAPTER 18 s WORKING WITH TEXT AND BINARY DATA Try It Out: Loading Image Binary Data from Files In this example,... i As Integer For i = 1 To loader.numberImageFiles loader.ExecuteInsertImages(i) Next i 345 94 70ch18final.qxd 3/15/08 2: 49 PM Page 346 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 346 CHAPTER 18 s WORKING WITH TEXT AND BINARY DATA Catch ex As Exception Console.WriteLine(ex.ToString()) Finally loader.CloseConnection() Console.WriteLine("Press any key to continue ") Console.ReadLine() . Form1 .vb to Events .vb, and also modify the Text property of the form to Events. 2. Open the Toolbox and drag a Button control over to the form. Select the Button con- trol, navigate to the Properties. property to True, which is set to False by default. 6. Drag a Label control from the Toolbox to below the TextBox and set its AutoSize prop- erty to False. Also, set the Label’s Font Size property to. type Calls a stored procedure that encounters an error. 3. Add a third button to the tab page, and change its Text property to Database Exception-3. Add a label to the right of this button, and for

Ngày đăng: 12/08/2014, 10:21

Mục lục

  • Beginning VB 2008 Databases: From Novice to Professional

  • Contents at a Glance

  • About the Technical Reviewer

  • Introduction

    • Who This Book Is For

    • What This Book Covers

    • How This Book Is Organized

    • How to Download the Sample Code

    • Getting Your Tools

      • Obtaining Visual Studio 2008

      • Installing SQL Server Management Studio Express

      • Installing the Northwind Sample Database

        • Installing the Northwind Creation Script

        • Creating the Northwind Sample Database

        • Installing the AdventureWorks Sample Database

          • Installing the AdventureWorks Creation Script

          • Creating the AdventureWorks Sample Database

          • Getting to Know Your Tools

            • Microsoft .NET Framework Versions and the Green Bit and Red Bit Assembly Model

            • Using Microsoft Visual Studio 2008

              • Try It Out: Creating a Simple Console Application Project Using Visual Studio 2008

              • Using SQL Server Management Studio Express

              • Getting to Know Relational Databases

                • What Is a Database?

                • Choosing Between a Spreadsheet and a Database

                • Why Use a Database?

                • Benefits of Using a Relational Database Management System

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

  • Đang cập nhật ...

Tài liệu liên quan