How to Use Data Sources with Databases - How to choose database objects for a data source
(Page 5 of 5 )
Figure 14-4 shows how you can use the last step of the Data Source Configuration Wizard to choose the database objects for a data source. This step lets you choose any tables, views, stored procedures, or functions that are available from the database. In some cases, you can just select the table you need from the list of tables that are available from the database. Then, all of the columns in the table are included in the dataset.
If you want to include selected columns from a table, you can expand the node for the table and select just the columns you want. In this figure, for example, the node for the Products table has been expanded and the three columns that will be used by the Product Maintenance applications in this chapter are selected. Note that although these applications will allow data to be added to the Products table, the OnHandQuantity column can be omitted because it’s defined with a default value in the database. So when a new row is added to the database, the database will set this column to its default value.
If you include a column with a default value in a dataset, you need to realize that this value isn’t assigned to the column in the dataset, even though the dataset enforces the constraints for that column. For instance, the OnHandQuantity column in the MMABooks database has a default value of zero and doesn’t allow nulls. But if you include this column in the dataset, its definition will have a default value of null and won’t allow nulls. As a result, an exception will be thrown whenever a new row is added to the dataset with a null value for the OnHandQuantity column.
This means that either the user or the application must provide an acceptable value for the OnHandQuantity column. One way to do that is to provide a way for the user to enter a value for the column. Another way is to use the Dataset Designer to set the DefaultValue property for this column as described in this figure. You’ll learn more about working with the Dataset Designer later in this chapter.
In a larger project, you might want to include several tables in the dataset. Then, the dataset will maintain the relationships between those tables whenever that’s appropriate. Or, you might want to use views, stored procedures, or functions to work with the data in the database. If you have experience working with views, stored procedures, and functions, you shouldn’t have any trouble understanding how this works. Otherwise, you can get another book such as Murach’s SQL for SQL Server to learn more about working with these types of objects.
The last step of the Data Source Configuration Wizard

Description
- In the last step of the Data Source Configuration Wizard, you can choose the database objects that you want to include in the dataset for your project.
- In this step, you can choose from any tables, views, stored procedures, or functions that are available from the database. In addition, you can expand the node for any table, view, stored procedure, or function and choose just the columns you want to include in the data source.
- You can also enter the name you want to use for the dataset in this dialog box. By default, the name is the name of the database appended with “DataSet”.
How to work with columns that have default values
- If a column in a database has a default value, that value isn’t included in the column definition in the dataset. Because of that, you may want to omit columns with default values from the dataset unless they’re needed by the application. Then, when a row is added to the table, the default value is taken from the database.
- If you include a column that’s defined with a default value, you must provide a value for that column whenever a row is added to the dataset. One way to do that is to let the user enter a value. Another way is to display the Dataset Designer as described in figure 14-21, click on the column, and use the Properties window to set the DefaultValue property.
Figure 14-4. How to choose database objects for a data source
Please check back next week for the conclusion of this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter 14 of the book Murach's Visual Basic 2005, written by Anne Boehm (Murach, 2006; ISBN: 1890774383). Check it out today at your favorite bookstore. Buy this book now.
|
|