明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
  博客园  :: 首页  :: 管理

http://support.microsoft.com/default.aspx?scid=kb;EN-US;326176

HOW TO: Implement a DataSet SELECT DISTINCT Helper Class in Visual C# .NET

Article ID : 326176
Last Review : February 8, 2007
Revision : 3.1
This article was previously published under Q326176

SUMMARY

This step-by-step article illustrates how to implement and how to use a DataSetHelper class that includes sample code to create a DataTable object that contains the unique values of a column of another DataTable object.

To do this, you use the SelectDistinct public method. You can also use a private helper method that compares fields that may contain NULL values (DBNull.Value).

The DataSetHelper class includes a DataSet member variable. Optionally, you can assign an existing DataSet object to the DataSet member variable. If the member variable points to a valid DataSet, any DataTable objects that the SelectDistinct method creates are added to the DataSet. In either case, the method call returns a reference to the DataTable object.

For additional information about DataSet objects, click the article number below to view the article in the Microsoft Knowledge Base:
313485 (http://support.microsoft.com/kb/313485/EN-US/) INFO: Roadmap for ADO.NET DataSet, DataView, and DataViewManager

Back to the top

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
Microsoft Windows XP, Windows 2000, or Windows NT 4.0 Service Pack 6a
Microsoft Visual Studio .NET
This article assumes that you are familiar with the following topics:
Visual C# .NET syntax
ADO.NET fundamentals and syntax

Back to the top

DataSetHelper Shell Class

The code in this section declares the shell class to which all DataSetHelper articles add methods and member variables.
1. Start Visual Studio .NET.
2. On the File menu, point to New, and then click Project.
3. In the New Project dialog box, click Visual C# Projects under Project Types, and then click Class Library under Templates.
4. In the Name box, type DataSetHelper.
5. Replace the class code with the following code:
public class DataSetHelper
{
	public DataSet ds;
	public DataSetHelper(ref DataSet DataSet)
	{
	        ds = DataSet;
	}
	public DataSetHelper()
	{
	        ds = null;
	}
}
					
You can use the two overloads for the constructor to create an instance of the class with or without a reference to a valid DataSet. For a class that contains a reference to a valid DataSet, the DataTable objects that the methods return are also added automatically to the DataSet.
NOTE: Add the following to the top of the code window:
using System.Data;
				

Back to the top

SelectDistinct Method

This section contains the code for the SelectDistinct method and the private ColumnEqual helper method.
1. Add the following Private method to the class definition. This method is the same as the method that is used in other DataSetHelper articles. It is used to compare field values (including NULL).
private bool ColumnEqual(object A, object B)
{
	
        // Compares two values to see if they are equal. Also compares DBNULL.Value.
        // Note: If your DataTable contains object fields, then you must extend this
        // function to handle them in a meaningful way if you intend to group on them.
			
        if ( A == DBNull.Value && B == DBNull.Value ) //  both are DBNull.Value
            return true; 
        if ( A == DBNull.Value || B == DBNull.Value ) //  only one is DBNull.Value
            return false; 
        return ( A.Equals(B) );  // value type standard comparison
}
					
2. Add the following Public method to the class definition. This method copies unique values of the field that you select into a new DataTable. If the field contains NULL values, a record in the destination table will also contain NULL values.
public DataTable SelectDistinct(string TableName, DataTable SourceTable, string FieldName)
{	
        DataTable dt = new DataTable(TableName);
        dt.Columns.Add(FieldName, SourceTable.Columns[FieldName].DataType);
			
        object LastValue = null; 
        foreach (DataRow dr in SourceTable.Select("", FieldName))
        {
            if (  LastValue == null || !(ColumnEqual(LastValue, dr[FieldName])) ) 
            {
                LastValue = dr[FieldName]; 
                dt.Rows.Add(new object[]{LastValue});
            }
        }
        if (ds != null) 
            ds.Tables.Add(dt);
        return dt;
}
					

Back to the top

Test Application

1. Save and compile the DataSetHelper class that you created in the previous sections, and then close the solution.
2. Follow these steps to create a new Visual C# Windows Form application in Visual Studio .NET.
a. Start Visual Studio .NET.
b. On the File menu, point to New, and then click Project.
c. In the New Project dialog box, click Visual C# Projects under Project Types, and then click Windows Application under Templates.
3. In Solution Explorer, right-click the solution and then click Add Existing Project. Add the DataSetHelper project.
4. On the Project menu, click Add Reference.
5. In the Add Reference dialog box, click the Projects tab, and then add a reference to the DataSetHelper project to the Windows Form application.
6. In the form designer, drag a Button control and a DataGrid control from the toolbox to the form. Name the button btnSelectDistinct, and then keep the default name for the DataGrid control (DataGrid1).
7. In the form code, add the following Using statement to the top of the code window:
using System.Data;
					
8. Add the following variable declarations to the form definition:
DataSet ds;
DataSetHelper.DataSetHelper dsHelper;
					
9. Add the following constructor code (below the InitializeComponent();):
ds = new DataSet();
dsHelper = new DataSetHelper.DataSetHelper(ref ds);

// Create source table
DataTable dt = new DataTable("Orders");
dt.Columns.Add("EmployeeID", Type.GetType("System.String"));
dt.Columns.Add("OrderID", Type.GetType("System.Int32"));
dt.Columns.Add("Amount", Type.GetType("System.Decimal"));
			
dt.Rows.Add(new object[] {"Sam", 5, 25.00});
dt.Rows.Add(new object[] {"Tom", 7, 50.00});
dt.Rows.Add(new object[] {"Sue", 9, 11.00});
dt.Rows.Add(new Object[] {"Tom", 12, 7.00});
dt.Rows.Add(new Object[] {"Sam", 14, 512.00});
dt.Rows.Add(new Object[] {"Sue", 15, 17.00});
dt.Rows.Add(new Object[] {"Sue", 22, 2.50});
dt.Rows.Add(new object[] {"Tom", 24, 3.00});
dt.Rows.Add(new object[] {"Tom", 33, 78.75});

ds.Tables.Add(dt);
					
10. Add the following code to the btnSelectDistinct.Click event:
dsHelper.SelectDistinct("DistinctEmployees", ds.Tables["Orders"], "EmployeeID");
dataGrid1.SetDataBinding(ds, "DistinctEmployees");
					
11. Run the application, and then click the button one time. Notice that the DataGrid is populated with the tables and the data from the code.

NOTE: You can only click the btnSelectDistinct button one time. If you click the button more than one time, you receive an error message that you are trying to add the same table two times.

Back to the top

Enhancement Ideas

You can only use the function to select a single, distinct field. However, you can extend the functionality to include multiple fields. Alternatively, you can call the CreateGroupByTable, the InsertGroupByInto, and the SelectGroupByInto methods, which use GROUP BY-type functionality, to get the same kind of results.

Back to the top

Troubleshooting

The fieldname and alias parts of the field list must comply with DataColumn naming conventions. The parser also restricts the names, in that the name must not contain a period (.), a comma (,), or a space ( ).
If you click the button more than one time, the same table is added two times to the DataSet, which results in an exception. To workaround this problem, you can add code to the test application to check whether a DataTable of the same name already exists. Alternatively, you can create the DataSetHelper class without a reference to a DataSet, and then bind the DataGrid.DataSource property directly to the dt variable instead of by using the SetDataBinding method call.
If the source table uses custom data types (that is, a class), you must add code to the SelectDistinct method to perform a deep copy of the data. Otherwise, only a reference is copied.