Danh mục

Create a Detach/Attach SQL Server Database Dialog Box

Số trang: 8      Loại file: pdf      Dung lượng: 26.00 KB      Lượt xem: 1      Lượt tải: 0    
Hoai.2512

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

7,5 Tạo một Tháo / Đính kèm SQL Server cơ sở dữ liệu Dialog Box trạng đôi khi phát sinh đòi hỏi một cơ sở dữ liệu được đính kèm, tách ra, hoặc cả hai.
Nội dung trích xuất từ tài liệu:
Create a Detach/Attach SQL Server Database Dialog Box7.5 Create a Detach/Attach SQL Server Database Dialog BoxSituations sometimes arise that require a database to be attached, detached, or both.Perhaps your client needs to move the database from one SQL Server to another. ThisHow-To shows you the methods you can use to perform these tasks.Before diving into solving some of the tasks that can be accomplished in the How-Tosjust listed, its important to discuss the SQL-DMO object model, as well as create areference to it.Many times, youve used the Enterprise Manager to move databases for users byattaching and detaching them. It would be nice to be able to perform this task withoutusers calling you. At he very least, youd like to be able to walk users through the detailsover the phone, without having to use the Enterprise Manager. How do you create adialog box to perform this task?TechniqueThis is probably the easiest of the How-Tos to create for this chapter, other than How-To7.1. For this task, you will be using two methods of the SQLServer object: DetachDB andAttachDBWithSingleFile.Along with the two methods just mentioned, you will also be using a Tab control tochoose which task to perform, as well as an OpenFile Dialog control to allow the user tochoose the file to attach.StepsOpen and run the VB.NET -Chapter 7 solution. From the main Windows form, click onthe command button with the caption How-To 7.5. You can select a database, such as theone displayed in Figure 7.11, and click the Detach button. You will see the databasedisappear from the list of databases to choose from. Figure 7.11. Ready to detach the chosen database.After you have chosen the database, you can reattach the database by clicking on the tablabeled Attach Database. You can then type in the name you want to attach the databaseas, and click on the Locate File button to locate the database file to attach (see Figure7.12.) Figure 7.12. Choosing the database file to attach.Select the file and click Open. To attach the file, click the Attach Database button. Thedatabase file will then be attached, and you can see it in the list of databases. To checkthis, you can look at the list back on the Detach Database tab; that list was refreshedwhen you clicked the Attach Database button. 1. Create a Windows Form.2. Add a Tab control from the Windows Form Controls list. Click the builder button in the Tab Pages property, and add two pages. Set the Text property for Page1 to Detach Database, and the Text property for Page2 to Attach Database.3. Add an OpenFileDialog control from the Windows Form Controls list. Go ahead and leave the default name given to the control, but make a note of it.4. Place the other controls shown in Figure 7.10 and 7.11, with the following properties set as in Table 7.9.Table 7.9. Label, ListBox, and Command Button Controls Property SettingsObject Location Property SettingLabel Main Form Name Label1 Text SQL ServersListBox Main Form Name lstSQLServersLabel Main Form Name Label2 Text DatabasesListBox Tab Page1 Name lstDatabasesButton Tab Page1 Name btnDetach Text &Detach DatabaseLabel Tab Page2 Name Label3 Text File to AttachTextBox Tab Page2 Name txtFileToAttachLabel Tab Page2 Name Label4 Text Name of Attached DatabaseTextBox Tab Page2 Name txtNameOfAttachButton Tab Page2 Name btnLocate Text &Locate FileButton Tab Page2 Name btnAttach Text &Attach Database5. On the form, add the code in Listing 7.24 to the Load event. This will look familiar from How-To 7.1. For an examination of the LoadSQLServers routine, check out step 4 in that How-To. Listing 7.24 frmHowTo7_5.vb: Calling the Routine That Loads Available SQL Servers into a List Box Private Sub frmHowTo7_5_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load LoadSQLServers(Me.lstSQLServers) End Sub6. On the lstSQLServers list box, add the code in Listing 7.25 to the SelectedIndexChanged event. This routine toggles the btnDetach button, depending on whether a SQL Server and database has been selected. It then calls GetSQLDatabases, described in step 6 of How-To 7.1. Listing 7.25 frmHowTo7_5.vb: Populating the lstDatabases List Boxes Private Sub lstSQLServers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles lstSQLServers.SelectedIndexChanged If lstSQLServers.SelectedItems.Count > 0 And _ lstDatabases.SelectedItems.Count > 0 Then Me.btnDetach.Enabled = True Else Me.btnDetach.Enabled = False End If GetSQLDatabases(Me.lstSQLServers.SelectedItem, Me.lstDatabases) End Sub7. On the lstDatabases list box, add the code in Listing 7.26 to the SelectedIndexChanged event. This routine toggles the btnDetach button, depending on whether a SQL Server and database have been selected. Listing 7.26 frmHowTo7_5.vb: Toggling the btnDetach Button Based on Whether a SQL Server and Database Have Been Chosen Private Sub lstDatabases_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles lstDatabases.SelectedIndexChanged If lstSQLServers.SelectedItems.Count > 0 And _ ...

Tài liệu được xem nhiều: