Constraints are not enabled. One or more lines contain a value that violates non-empty, unique or foreign key constraints.

xiaoxiao2021-03-06  39

The problem is not resolved

Strongly Typed DataSet Advantages

Strong type DataSet

Mycustomers mycustomers = new mycustomers ();

Mycustomers.customersDataTaBLE TBLCUSTOMERS = MyCustomers.customers;

Mycustomers.customersrow Rowcustomers = TBLCUSTOMERS.NEWCUSTOMERSROW ();

RowCustomers.xxx = "xxx";

// ....

TBLCUSTOMERS.ADDCUSTOMERSROW (RowCustomers);

Auto-completion feature combined with VS.NET can greatly improve development efficiency.

Strong type DataSet advantages and disadvantages

advantage:

1) The advantage of designing, writing code is easy, able to make full use of VS.NET automatic completion

2) Bind controls more easily during design

3) When running, it is primarily that the performance of the application is mainly because when accessing a field of accessing a record, using a DataColumn object itself is higher efficient than using the column name, but has added maintenance difficulties. Strong type DataSet solves this problem for us.

Disadvantages:

not flexible.

1) Because the architecture information has been hardcoded into mycustomers.cs, if change, it must be regenerated.

2) For example, select 3 fields A, B, C from a table with 30 fields, generate a strong type of DataSet1 specifically for these three fields, but if another method needs to select the field C, D , E, also need to generate a strong type of DataSet2 for these three fields. If the SELECT field is customized by the user, a combination of the field is no.

3) In order to avoid using multiple strong type Dataset above, we can only use a strong type of Dataset, so each of our Fill how many fields are filled into a strong type DataSet. But this has brought new questions: i) space waste (especially for multi-table connection). You can write code to test 30 String fields only populate 1, 2 occupied spaces (using strong type Dataset is more than weak types). Ii) Constraint violations. Strong types have added constraints (such as non-empty, fk, etc.) in the database, so that there may be a violation of the constraint when only partial data is filld. For example, COL1, COL2, COL3, and COL4 are required to be empty, but I only select COL1, COL2 enters strong Type Dataset, which corresponds to the recorded col3, and the col4 field is empty, violating non-empty constraints, will throw an abnormality Tip: Unprocessed "System.Data.constraintexception" type of exception appears in System.Data.dll.

Additional information: The constraint cannot be enabled. One or more lines contain a value that violates non-empty, unique or foreign key constraints.

Although the DataSet's EnforceConstraints property can be set, it is still not cool.

Therefore, the strength of weakness DataSet is probably binding, or abandoning the use of strong type DataSet.

23:41 | Comments (0)

Strongly Typed DataSet (2) - Generate Code Analysis

Generate code analysis

Generate sample DataSet

Table Customers with Microsoft's Northwind as an example of a strong type of DataSet:

1) Newly built a DataAdapter, the SQL statement used is SELECT * FROM CUSTOMERS

2) Use this DataAdapter to create a data set MyCustomers

2.2.2

View code

We can find that vs.net adds a MyCustomers.xsd file if you choose to view all files, you can also see there is a MyCustomers.cs file.

//Mycustomers.cs

1) Define a class MyCustomers inherited from Dataset

In addition to define 2), 3), 4), mainly to maintain a reference to TableCustomers

2) MyCustomers define Customs CustomersDataTable from DataTable

Public Class CustomersDataTable: DataTable, System.collections.ienumerable {

/ / Define the corresponding DataColumn object for each field and access it through attribute. Use Datacolumn to improve access efficiency

Private Datacolumn ColumnCustom;

Private Datacolumn ColumnCompanyName;

Private Datacolumn ColumnContactName;

Private Datacolumn ColumnContactTitle;

// other fields of Datacolumn objects

INTERNAL CUSTOMERSDATABLE ():

Base ("Customers") {

THIS.INITCLASS ();

}

INTERNAL CUSTOMERSDATABLE (DataTable Table):

Base (Table.TableName) {

// Set the corresponding attribute of this class according to Table

IF ((Table.casensensitive! = Table.Dataset.casensensitive)) {

this.casensensitive = Table.casensITIVE;

}

IF ((Table.locale.Tostring ()! = Table.DataSet.locale.tostring ())) {

THISLOCALE = Table.locale;

}

IF ((Table.Namespace! = Table.DataSet.namespace)) {

THIS.NAMESPACE = Table.Namespace;

}

THIS.PREFIX = Table.Prefix;

THIS.MINIMUMCAPACITY = TABLE.MINIMUMCAPACITY;

This.displayExpression = Table.disPlayExpression;

}

[System.componentmodel.browsable (false)]

// count attribute

Public int count {

Get {

Return this.rows.count;

}

}

// Various Datacolumn corresponding to the above

INTERNAL DATACOLUMN CUSTOMERIDCOLUMN {

Get {

Return this.columncustomerid;

}

}

INTERNAL DATACOLUMN CompanyNAMECOLUMN {

Get {

Return this.columncompanyname;

}

}

INTERNAL DATACOLUMN ContactNameColumn {

Get {

Return this.columncontactname;

}

}

INTERNAL DATACOLUMN Contacttitlecolumn {Get {

Return this.columncontacttitle;

}

}

// Index, return to the corresponding Customersrow

Public Customersrow this [int index] {

Get {

(Customersrow) (this.rows [index]));

}

}

Public Event CustomersRowchangeEventHandler CustomersRowChanged;

Public Event CustomersRowchangeEventhandler CustomersRowchanging;

Public Event CustomersRowchangeEventhandler CustomersrowDeled;

Public Event CustomersRowchangeEventhandler CustomersrowDeleding;

// Add a line using the Customersrow object

Public void addcustomersrow (Customersrow Row) {

this.Rows.Add (row);

}

// fill a new line using the value of each field

public CustomersRow AddCustomersRow (string CustomerID, string CompanyName, string ContactName, string ContactTitle, string Address, string City, string Region, string PostalCode, string Country, string Phone, string Fax) {

Customersrow Rowcustomersrow = ((Customersrow) ()));

RowCustomersrow.ItemRay = new object [] {/ Use itemArray property Sets the value of CustomersRow

Customerid,

CompanyName,

ContactName,

ContactTitle,

Address,

CITY,

Region,

Postalcode,

COUNTRY,

Phone,

FAX};

This.Rows.Add (RowCustomersrow);

Return RowCustomersRow;

}

/ / Find Customersrow through Customerid

Public Customersrow FindbyCustomerid (String Customerid) {

Return (CustomersRow) (this.rows.find (new object [] {

Customerid}))))))))))))))))

}

/ / Return to ENUMERATOR

Public system.collections.ienumerator geteNumerator () {

Return this.Rows.GeteNumerator ();

}

// clone method

Public override datatable clone () {

CustomersDataTable Cln = ((CustomersDataTable) (BASE.CLONE ()))

Cln.initvars ();

Return CLN;

}

// Create a new instance

Protected Override DataTable CreateInstance () {

Return New CustomersDataTable ();

}

// Datacolumn variable initialization function, CLONE () call, another call mode is called in MyCustomers' initvars ()

INTERNAL VOID INITVARS () {// Initialization of Various Datacolumn Variables

this.columncuStomerid = this.columns ["customerid"];

THIS.COLUMNCOMPANYNAME = this.columns ["CompanyName"];

THIS.COLUMNCONTACTAME = this.columns ["contactname"];

THIS.COLUMNCONTACTTITLE = this.columns ["ContactTitle"];

this.columnaddress = this.columns ["address"];

THIS.COLUMNCITY = this.columns ["city"];

THIS.COLUMNREGION = this.columns ["region"];

THIS.COLUMNPOSTALCODE = this.columns ["postalcode"];

THIS.COLUMNCOUNTRY = this.columns ["country"];

this.columnphone = this.columns ["phone"];

THIS.COLUMNFAX = this.columns ["fax"];

}

// Initialization function, initialize columns, and add various constraints

Private vidin () {

This.columncuStomerid = New Datacolumn ("Customerid", TypeOf (String), NULL, System.Data.mappingType.element;

this.columns.add (this.columncustomerid);

This.ColumnCompanyName = New Datacolumn ("CompanyName", TypeOf (String), null, system.data.mappingType.element;

this.columns.add (this.columncompanyname);

THIS.COLUMNCONTACTAME = New Datacolumn ("ContactName", TypeOf (String), NULL, System.Data.mappingType.eMent;

this.columns.add (this.columnContactName);

This.ColumnContactTitle = New Datacolumn ("ContactTitle", TypeOf (String), null, system.data.mappingType.element;

This.columns.add (this.columncontacttitle);

This.ColumnAddress = New Datacolumn ("Address", TypeOf (String), null, system.data.mappingType.element;

This.columns.add (this.columnaddress);

THIS.COLumnCITY = New Datacolumn ("City", TypeOf (String), null, system.data.mappingType.element;

This.Columns.Add (this.column "; this.columnregion = New Datacolumn (" region ", typeof (string), null, system.data.mappingtype.element;

This.columns.add (this.columnregion);

This.ColumnPostalcode = New Datacolumn ("Postalcode", TypeOf (String), NULL, System.Data.mappingType.element;

this.columns.add (this.columnpostalcode);

THIS.COLUMNCOUNTRY = New Datacolumn ("Country", TypeOf (String), null, system.data.mappingType.eMent;

this.columns.add (this.columncountry);

This.ColumnPhone = New Datacolumn ("Phone", TypeOf (String), null, system.data.mappingType.element;

this.columns.add (this.columnphone);

This.columnfax = new Datacolumn ("fax", typeof (string), null, system.data.mappingtype.element;

THIS.COLUMNS.ADD (this.columnfax);

THIS.CONSTRAINTS.ADD (New UniqueConstraint ("constraint1", new datacolumn [] {

this.columncuStomerid}, true);

This.columncuStomerid.Allowdbnull = FALSE;

THIS.COLUMNCUSTOMERID.UNIQUE = True;

This.columncompanyname.allowdbnull = false;

}

Public Customersrow New CustomersRow () {

Return ((CustomersRow) (this.newrow ()));

}

Protected Override DataRow NewrowFromBuilder (DataRowbuilder Builder) {

Return New CustomersRow (Builder);

}

Protected Override System.Type GetRowType () {

Return TypeOf (Customersrow);

}

Protected Override Void Onrowchanged (DataRowchangeEventArgs E) {

Base.onrowChanged (e);

IF ((this.customersRowchanged! = null)) {

This.customersRowchanged (this, New CustomersRowchangeEvent ((Customersrow) (E.ROW)), E.ACTION);

}

}

Protected Override Void Onrowchanging (DataRowChangeEventArgs E) {

Base.onrowchanging (e);

IF ((this.customersrowchanging! = null) {

This.customersrowChanging (this, New CustomersRowchangeEvent ((Customersrow) (E.ROW)), E.ACTION);}

}

Protected Override Void Onrowdeleted (DataRowChangeEventArgs E) {

Base.onrowDeled (e);

IF ((this.customersrowdeleted! = null)) {

This.customersrowdeleted (this, New CustomersRowchangeEvent ((Customersrow) (E.ROW)), E.ACTION));

}

}

Protected Override Void OnrowDeleTing (DataRowChangeEventArgs E) {

Base.onrowDeleting (e);

IF ((this.customersrowdeleting! = null) {

This.customersrowDeleTing (this, New CustomersRowchangeEvent ((Customersrow) (E.ROW)), E.ACTION);

}

}

Public Void RemoveCustomersrow (Customersrow Row) {

this.Rows.Remove (ROW);

}

}

3) MyCustomers define the class Customersrow inherited from DATAROW

Public Class Customersrow: DataRow {

Private customer tabletable TableCustomers; // Reserved a reference to DataTable

Internal Customersrow (DataRowbuilder RB):

Base (rb) {

this.tablecustomers = (THISTOMERSDATABLE) (THISTABLE)); // Initialization Reference

}

Public string customerid {

Get {

Return ((String) (this [this.tablecuStomers.customeridColumn]);

}

SET {

THIS [this.tablecuStomers.customeridColumn] = Value;

}

}

Public string companyName {

Get {

Return ((String) (this [this.tablecustomers.companynamecolumn]));

}

SET {

THIS [this.tablecuStomers.comPanyNameColumn] = Value;

}

}

Public string contactname {

Get {

Try {

Return (String) (this [this.tablecuStomers.contactNameColumn]);

}

Catch (InvalidCastExcection E) {

Throw new strongTyPingexception ("Unable to get values ​​because it is dbnull.", e);

}

}

SET {

THIS [this.tablecuStomers.contactNameColumn] = Value;

}

}

Public string contactTitle {

Get {

Try {

Return ((String) (this [this.tablecuStomers.contactTitlecolumn]));

}

Catch (InvalidCastExcection E) {

Throw new strongTyPingexception ("Unable to get values ​​because it is dbnull.", e);

}

SET {

THIS [this.tablecuStomers.contacttitlecolumn] = Value;

}

}

// ...... ...

// and other corresponding properties that can be empty fields

Public bool iscontactnamenull () {

Return this.isnull (THISTABLECUSTOMERS.CONTACTACTAMECOLUMN);

}

Public void setContactNamenull () {

This [this.tablecuStomers.ContactNameColumn] = system.convert.dbnull;

}

Public bool iscontacttitlenull () {

Return this.isnull (THIS.TABLECUSTOMERS.CONTACTACTTITLCOLUMN);

}

Public void setcontacttitlenull () {

THIS [THIS.TABLECUSTOMERS.CONTACTACTECOLUMN] = system.convert.dbnull;

}

// ...... ...

// and other ISXXNULL (), setXXXNULL () method that can be empty fields

}

It can be seen that the CustomersRow class has added a lot of properties (corresponding to each field), and provides the operation of determining whether DBNULL and setting DBNULL. Also, all the operations for these attributes are actually converted to the operation of the corresponding Column object corresponding to the DataTable.

4) MyCustomers define class CustomersRowChangeEvent inherited from Eventargs:

Public Class CustomersRowchangeEvent: Eventargs {

Private Customersrow EventRow;

PRIVATE DATAROWACTION EVENTACTION;

Public CustomersRowchangeEvent (CustomerSrow Row, DataRowAction Action) {

THIS.EVENTROW = ROW;

THIS.EVENTACTION = Action;

}

Public CustomersRow Row {

Get {

Return this.EventRow;

}

}

Public DataRowAction action {

Get {

Return this.eventAction;

}

}

}

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

New Post(0)