Danh mục

Creating a Connection Object Using Visual Studio .NET

Số trang: 7      Loại file: pdf      Dung lượng: 19.08 KB      Lượt xem: 12      Lượt tải: 0    
Jamona

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

Thông tin tài liệu:

Creating a Connection Object Using Visual Studio .NET To create a SqlConnection object using Visual Studio .NET
Nội dung trích xuất từ tài liệu:
Creating a Connection Object Using Visual Studio .NETCreating a Connection Object Using Visual Studio .NETTo create a SqlConnection object using Visual Studio .NET, you drag a SqlConnectionobject from the Data tab of the Toolbox to your form. Youll recall that a SqlConnectionobject allows you to connect to a SQL Server database. You can also drag anOleDbConnection object from the Toolbox to your form to connect to a database thatsupports OLE DB.Figure 7.1 shows a form with a SqlConnection object. This object is assigned the defaultname of sqlConnection1.Figure 7.1: Creating a SqlConnection object with Visual Studio .NETOnce youve created a SqlConnection object, that object appears in the tray below theform. The tray is used to store nonvisual components like SqlConnection objects. Otherobjects that appear in the tray are SqlCommand objects. These objects are considerednonvisual because you dont see them when you run your form. You can of course stillwork with them visually when designing your form.To the right of the form, youll notice the Properties window, which you use to set theproperties for your SqlConnection object. To set the ConnectionString property thatspecifies the details of the database connection, you can either type in the string directlyor click the drop-down list and build the ConnectionString visually. To do this, you selectNew Connection from the drop-down list, which displays the Data Link Properties dialogbox. This dialog box contains four tabs, the first of which is the Provider tab, whichallows you to select the type of provider you want to connect to, as shown in Figure 7.2.Figure 7.2: Selecting the providerClick the Next button to continue to the Connection tab (you can also click theConnection tab directly), where you enter the details for your database connection, asshown in Figure 7.3.Figure 7.3: Entering the connection detailsWarning For security reasons, do not enable the Allow Saving Password check box. If you did, your password would be stored in the actual code, and anyone could get your password from the code. Leave Allow Saving Password in its default non-enabled state; that way, the user will be prompted to enter the password. For testing purposes, however, leaving your password in is sometimes acceptable-just remember not to release your password in production code.Once youve entered your connection details, you can press the Test Connection button toensure your details are correct.At this point, youve entered all the mandatory details, and you can choose to save yourdetails by clicking OK, or you can click Advanced to enter additional details such as theconnection timeout, as shown in Figure 7.4.Figure 7.4: Entering the advanced connection detailsYou can also click the All tab to view and edit all the values for the connection, as shownin Figure 7.5. To edit a value, you click Edit Value.Figure 7.5: Viewing all the connection detailsClick the OK button to save your connection details.On my computer, the ConnectionString property for my SqlConnection object thatconnects to the SQL Server Northwind database is set todata source=localhost;initial catalog=Northwind;persist security info=False;user id=sa;pwd=sa;workstation id=JMPRICE-DT1;packet size=4096Note The persist security info Boolean value controls whether security-sensitive information such as your password is returned in a connection that has been previously opened. Youll typically want to leave this in the default setting of False.Coding an Event in VS .NETYou can add code for an event in VS .NET. For example, lets say you wanted to addcode for the State-Change event of the sqlConnection1 object created earlier. To do this,you first select sqlConnection1 in the tray, then you click the Events (lighting) button inthe Properties window. Figure 7.6 shows the sqlConnection1 objects events.Figure 7.6: sqlConnection1 objects eventsYou then double-click the name of the event in the Properties window you want to code.In this example, you double-click the StateChange event. VS .NET then displays the codeand creates a skeleton of the event handler method for you, as shown in Figure 7.7. Thecursor shows where you add your code.Figure 7.7: The beginning StateChange event handler methodAll you have to do is add your code to the event handler method. For example, you canset your method toprivate void sqlConnection1_StateChange( object sender, System.Data.StateChangeEventArgs e){ Console.WriteLine( State has changed from + e.OriginalState + to + e.CurrentState );}Figure 7.8 shows the completed event handler method.Figure 7.8: The completed StateChange event handler methodOnce youve created a SqlConnection object, you can then use it with other ADO.NETobjects, such as a SqlCommand object. Youll see how to do that ...

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