ataTable 去除重复项 HOW TO: Implement a DataSet SELECT DISTINCT Helper Class in Visual C# .NET
2008-10-05 21:32
579 查看
http://support.microsoft.com/default.aspx?scid=kb;EN-US;326176
HOW TO: Implement a DataSet SELECT DISTINCT Helper Class in Visual C# .NET
View products that this article applies to.function loadTOCNode(){}
Article ID | : | 326176 |
Last Review | : | February 8, 2007 |
Revision | : | 3.1 |
On This Page
SUMMARY
Requirements
DataSetHelper Shell Class
SelectDistinct Method
Test Application
Enhancement Ideas
Troubleshooting
var sectionFilter = "type != 'notice' && type != 'securedata' && type != 'querywords'";
var tocArrow = "/library/images/support/kbgraphics/public/en-us/downarrow.gif";
var depthLimit = 10;
var depth3Limit = 10;
var depth4Limit = 5;
var depth5Limit = 3;
var tocEntryMinimum = 1;
.toc{display: none;}
SUMMARY
loadTOCNode(1, '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
loadTOCNode(2, 'summary');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 |
• | Visual C# .NET syntax |
• | ADO.NET fundamentals and syntax |
Back to the top
DataSetHelper Shell Class
loadTOCNode(2, 'summary');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. |
using System.Data;
Back to the top
SelectDistinct Method
loadTOCNode(2, 'summary');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
loadTOCNode(2, 'summary');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.
| ||||||
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
loadTOCNode(2, 'summary');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
loadTOCNode(2, 'summary');• | 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. |
相关文章推荐
- HOW TO: Implement a DataSet SELECT DISTINCT Helper Class in Visual C# .NET
- Implement a DataSet SELECT DISTINCT Helper Class in Visual C# .NET
- (转)Implement a DataSet SELECT DISTINCT Helper Class in Visual C# .NET
- HOW TO: Implement a DataSet JOIN helper class in Visual C# .NET(DataSetHelper)
- DataSet SELECT DISTINCT Helper Class in Visual C# .NET
- Implement a DataSet SELECT DISTINCT Helper Class
- C#.NET Class扩展 ToJson/Select/IsBetween/In/Compress/Serialize
- HOW TO: Set a Windows Hook in Visual C# .NET
- HOW TO:使用 Visual C# .NET 从数据库填充 DataSet 对象
- 如何使用 Visual C# 2005 或 Visual C# .NET 通过 DataSet 对象更新数据库
- HOW TO:使用 Visual C# .NET 生成 Office COM 外接程序
- Asp.Net : 实现一个 DataSet 或DataTable SELECT DISTINCT (字段唯一性)
- HOW TO: 将使用 Visual C#.NET 序列化为 Web 服务器控件
- [导入]HOW TO:使用 Visual C# .NET ...
- HOW TO:使用 Visual C# .NET 在 ASP.NET 中创建自定义错误报告
- HOW TO:使用 Visual C# .NET 从文件中读取 XML
- 怎样在Visual C# .NET中实现一个DataSet的不同记录的选取(zsxfbj译)
- "HOW TO: Create a Smooth Progress Bar in Visual C# .NET"的学习笔记
- New DataSet Features in Visual Studio 2005 (C# version)
- HOW TO:使用 Visual C# .NET 向 Excel 工作簿传输数据