Creating a Client

Now that you have created a serviced component, you can use it in much the same way as you would use any other managed class. The following code shows how you would use the Book class.

1.  private void cmdGetData_Click(object sender,
2.  System.EventArgs e)
3.  {
4.      Book obj;
5.      IDisposable disp;
6.      DataSet ds;
7.      obj=new Book();
8.      ds=obj.GetBooksByTitle(txtTitle.Text);
9.      grid1.DataSource=ds.Tables["Titles"].DefaultView;
10.     disp=obj as IDisposable;
11.       disp.Dispose();
12.  }

In this code, you instantiate an instance of the Book class on line 7. Line 8 calls the GetBooksByTitle, passing in the title of a book (or a portion of the title). This method will return an ADO.NET dataset (we'll talk more about ADO.NET and datasets shortly). Line 9 binds the default view of this dataset to a datagrid, which is a WinForms table control. Finally, lines 10 and 11 call the Dispose method on the object to free its resources immediately. It's a good idea to do this with serviced components. Calling Dispose will immediately free the unmanaged context that is associated with each serviced component.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.74.54