How to do this in Visual C #. The selection of different records for implementing a dataset in net

Source: Internet
Author: User
Tags definition class definition extend reference valid knowledge base visual studio
How visual in Visual C #. The selection of different records for implementing a dataset in net
From: Microsoft knowledge Base article–326176, address: http://support.microsoft.com/default.aspx?scid=kb; en-us;326176

Summary:

This is a basic, simple article that illustrates how to implement and how to use a DataSetHelper class that uses concise code to create a new DataTable object that is unique to the value of a column specified by a DataTable object.

????? To do this, you can use SELECTDISTINCT to expose this method, or you can use a private helper method that compares fields that might contain null values (DBNull.Value).

?????? The DataSetHelper class contains a DataSet member variable, and you can optionally specify an existing DataSet object as a variable for this dataset member. If this member variable is a valid dataset, any DataTable object created by any one by one selectdistinct methods can be populated into the dataset. In that way, this method requests that the DataTable object involved be returned.

Requirements:

The following list lists the recommended hardware, software, network infrastructure, and required patch packs:

(1), Microsoft Windows XP, Windows Watts, or Windows NT 4.0 Service Pack 6a

(2), Microsoft Visual Studio. NET

This article assumes that you are familiar with the following topics:

(1), Visual C #. NET's syntax

(2), ADO. NET rationale and syntax

DataSetHelper Construction Class:

(1), start Microsoft Visual Studio. NET.

(2), on the File menu, click New (New), and then click Item (Project).

(3) In the New Project dialog box, click the project type under Visual C # project (Visual C # Projects), and then click on the Class library (Types) Library) under the template (templates).

(4), fill in the Name column 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 create an instance class with or without a valid dataset with a 2-time overloaded constructor. Because the class is involved in a valid dataset, the DataTable object returned by this method is automatically populated into the dataset.

Note: Add the following code to the top of the code window:

Using System.Data;

?

SelectDistinct Method:

This section contains the code for the exposed SelectDistinct method and the private Columnequal auxiliary method.

(1) Add the following private method to the class definition, which is also used in other datasethelper articles and is used to compare the values of fields (including null values).

private bool Columnequal (object A, Object B)

{

???????? ?? Compares two values to the if they are equal. Also compares DBNULL. Value.

??????? Note:if your DataTable contains object fields, then you must extend

??????? 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 duplicates the values of the fields you have selected into a new DataTable. If the field contains a null value, a record in the target column will also contain a null value.

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;

}

Test application

(1), save and compile the DataSetHelper class of this section you created earlier, and then close the solution.

(2), in the following steps in Visual Studio. NET to create a new Visual C # Windows form program:

A, start Visual Studio. NET;

B, on the File menu, click New (New), and then click Item (Project).

C, in the New Project dialog box, click the project type under Visual C # project (Visual C # Projects) (Project Types), and then tap the Windows application (Windows Template (templates) under the application).

(3), right-click the solution in Solution Explorer, and then click Add Existing Project to join DataSetHelper this project.

(4), in the Item menu, click Item Dependencies (ADD Reference).

(5), in the Project Dependencies dialog box, select the dependency (Projects) tab, and then increase the relationship between this DataSetHelper project and the Windows Form program.

(6), in the window design interface, drag a button control from the Toolbox and a DataGrid control onto the form, specify the button name Btnselectdistinct, and continue to maintain the default name (DATAGRID1) for the DataGrid control.

(7) In the form code interface, add the following reference declaration at the top of the code window:

Using System.Data;

(8), add the following variable declaration in the definition of the form:

DataSet ds;

Datasethelper.datasethelper Dshelper;

(9), add the following construction code (under the InitializeComponent (); below):

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 in the Btnselectdistinct.click event:

Dshelper.selectdistinct ("Distinctemployees", ds. tables["Orders"], "EmployeeID");

DataGrid1.SetDataBinding (ds, "Distinctemployees");

(11), run this program, click the button once, observe the table and data generated from the code on the DataGrid binding.

Note: You can only click Btnselectdistinct once. If you click this button more than once, you will receive an error message-you are trying to add the same table 2 times.

Further ideas:

You can only filter one field to be different with this function, but you may extend it to multiple fields in a similar nature. As an alternative, you can invoke the Creategroupbytable, Insertgroupbyinto, and Selectgroupbyinto methods to get the same result by using the functions of group By-type.

?




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.