This is a multi-table connection and how to return null values ​​to foreign key fields in the ASPNET.

xiaoxiao2021-03-06  57

This is a multi-table connection and how to return null values ​​to foreign key fields in the ASPNET.

The background of the database is like this:

Equipment Table A, Foreign Committe: Supply_ID (Supplier Serial Number) Data Type: INT, Allowed

Supplier table B, primary key: Suppirl_id (vendor number) Data type: int, as a seed

1. Since it is not a vendor record, there is a number of record_ids with a plurality of records in A.

2. Since the Support_ID in B is an identification seed starting from 1, in order to correspond to the corresponding relationship, the content of the data is maintained, the content of the a.Supply_ID is an integer equal to 1.

I used A to the left-way connection to B.

This is the result I discussed in the morning, and it seems to have passed the test data. But in the afternoon, I used ASPNET to access the device table, and there was a problem with it.

I use the DropDownList control to bind the supplier,

/ * Binding supplier DPDOWNLIST * /

String sup_sql = "SELECT Supply_ID, Supply_Name from Supply";

mysql.runload (SUP_SQL); // MySQL is a public class, a collection of database connections and access

DropDownList2.Items.add (New ListItem ("," 0 "))); // The first item of the drop-down menu is empty, in order to show the devices that have no supplier. TEXT is empty, Value is O

While (mysql.myDataReader.Read ())

DropdownList2.items.add (NEW ListItem (MySQL.MYDATAREADER [1] .tostring (), mysql.mydatareader [0] .tostring ()))); // The binding of the supplier is just the serial number (Supply_ID) And supplier name (Supply_Name) a SelectIndex, and a value value, a text value.

Then I connect to the database and start accessing the data, no problem.

IF (MySql.myDataReader [Suppy_ID] == "")

DropDownList1.selectIndIndex = 0;

Else

{

DropdownList1.selectIndIndex = Convert.Toint (mysql.mydatareader [suppy_id] .tostring ())

}

The problem is in the modification of the data and returns back to the database!

I modified the vendor to replace the value of DropDownList1.selectIndex, so the repossed SQL statement is

Update machine set supply_id = dropdownloadList1.selectIndex

When I selected null, (no supplier), DropDownList1.selectIndex = 0,0 was returned to the original A.Supply_ID, when we stipulated that the content of this field started from 1, this The contradiction between the number of times during reading data is caused.

Solution:

Changed all the foreign keys in the database to 0, the DropDownList binds the supplend table, it has already been vacated with SlectIndex = 0, Value = 0, has nothing to do, but when the database is accessed, Put

IF (MySql.myDataReader [Suppy_ID] == "") DropDownList1.selectIndIndex = 0;

Else

{

DropdownList1.selectIndIndex = Convert.Toint (mysql.mydatareader [suppy_id] .tostring ())

}

This big section is changed

DropDownList1.selectIndex = Convert.TOINT (MySql.myDataReader [Suppy_ID] .tostring ());

The previous external connection does not change, and the data consistency and integrity can be solved.

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

New Post(0)