How to implement a different record of a DataSet in Visual C # .NET (ZSXFBJ translation)

xiaoxiao2021-03-06  119

How to implement a different record of a DataSet in Visual C # .NET

From: Microsoft Knowledge Base Article - 326176, Address: http://support.microsoft.com/default.aspx?scid=kb;n-us; 326176

Summary:

This is a basic article, which is a shallow, which means how to implement and use a DataSetHelper class. This class uses a simple code to create a new value specified by a DataTable object has a unique Sex DataTable object.

????? In order to achieve this, you can use the selectdistinct disclosure method, or use a private assist method that compares the field that may contain a null value (DBNULL.VALUE).

?????? DataSethelper class contains a DataSet member variable, you can specify an existing DataSet object as a variable of this DataSet member. If this member variable is a valid Dataset, any DataTable object created by any SelectInstinct method can be filled in this DataSet. In that case, this method requests to return the DataTable object involved.

Claim:

The following list is listed in the recommended hardware, software, network infrastructure, and must-have patch packages:

(1), Microsoft Windows XP, Windows 2000, 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 syntax

(2), ADO.NET basic principles and grammar

DataSethelper constructory:

(1) Start Microsoft Visual Studio .NET.

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

(3) In the New Project dialog box, click Project Types under Visual C # Projects, then click Templates under class library.

(4) Fill in DataSetHelper in the name bar.

(5), replace 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 2 heavy-duty constructor to create an instance class with or without a valid DataSet. Because classes involve an effective DataSet, then the DataTable object returned by this method This is automatically filled into this DataSet. Note: Add the following code at the top of the code window:

Using system.data;

?

SelectDistINCT method:

This section contains code-free SelectDistINCT methods and private columnequal auxiliary methods.

(1) Increase the following private method in the class definition, which is also used in other DataSetHelper articles, which is used to compare the value of the fields (including Null values).

Private Bool Columnequal (Object A, Object B)

{

???????? ?? // Compares Two Values ​​To See eti the isy. Also Compares DBNULL.VALUE.

??????? // Note: if Your DataTable Contains Object Fields, THEN You Must Extend this

??????? // function to handle Theim 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) Increase the following disclosure method in the class definition. This method copies the mutual value of your selected field to a new DataTable. If this field contains a null value, a NULL value is also included in a record in the target column.

Public DataTable SelectDist (String TableName, String FieldName)

{??????

??????? DataTable DT = New DataTable (TABLENAME);

??????? dt.columns.add (fieldname, sourceable.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 you previously created, then close the solution.

(2) Creating a new Visual C # Windows Form program in Visual Studio .NET in the steps:

A, launch Visual Studio .NET;

B, in the File menu, click New, and then click Project.

C. In the New Project dialog box, click Project Types under Visual C # Projects, then click Templates under Windows Application.

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

(4) In the project menu, click on the Project Dependency.

(5) In the Project Dependency dialog box, select the Projects tab, then add this DataSetHelper project and the relationship between this Windows Form program.

(6) In the window design interface, drag a Button control from the toolbox and a DataGrid control to the form, specify this button name BTNSELECTISTINCT, continue to keep the default name of the DataGrid control (DataGrid1).

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

Using system.data;

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

DataSet DS;

Datasethelper.datasethelper dshelper;

(9) Increase the following constructed code (in 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) Increase the following code in BTNSelectDistINCT.Click event:

DSHELPER.SELECTDISTINCT ("DistINCTemPloyees", DS.Tables ["Orders"], "EMPLOYEEID");

DataGrid1.SetDataBinding (DS, "DistINCTemPloyees);

(11), run this program, click the button once, observe the tables and data generated from the code from the code from the code.

Note: You can only click BtnselectDistin to once. If you click this button for more than once, you will receive an error message - you have tried to add the same table twice.

Further ideas:

You can only filter a field in this feature, but you can scale to multiple fields. As another choice, you can call CreateGroupByTable, InsertGroupByinto, and SelectGroupByinto methods, get the same result by using Group By-Type's function.

?

转载请注明原文地址:https://www.9cbs.com/read-126964.html

New Post(0)