Processing of most tables in. NET multiple languages and datasets (3)

Source: Internet
Author: User
Tags foreach
Data 3 Consider the combination of the above two situations
When the above two situations are together, the situation will be more complicated, because in our solution, the main body of multi-language and information is loosely coupled, and if loose coupling is not guaranteed to be universal and scalable, the loose coupling can cause problems in multiple table operations in data sets.
Because of loose coupling, so in the dataset automatically update the time is not automatically updated, the modification does not matter, we just have to ensure that with the multilingual table associated with the same GUID is OK, but delete it? We can not delete the subject of the information and then keep the multilingual data, even more troublesome because the data has been deleted, it is difficult to know how much of the GUID was associated with the multilingual datasheet before the deletion. And we need to put the deletion of the subject of information and the deletion of multilingual data into a transaction, I believe that no one will want the program accidentally error information body has not been deleted when the multilingual data has been deleted. Also, we want to implement one of the previous goals: each user only needs to maintain the record information of the language they use, regardless of other language problems, but also very convenient even if the system has been running for a while and then add the supported language, You don't need to add all the language versions after you add a record, but add the version of the language when you need it, making the database record as few as possible.
Well, we know the need, and then what? Look at the following code:
When fetching data from a database:
Public Addressdata getaddress (string languagecode)
{
Addressdata ds = new Addressdata ();

SqlDataAdapter daregion = new SqlDataAdapter (addresssql.strgetregion,sqlconfig.databaseconnection);
Daregion.fill (ds. Region);

SqlDataAdapter dacountry = new SqlDataAdapter (addresssql.strgetcountry,sqlconfig.databaseconnection);
Dacountry.fill (ds. Country);

SqlDataAdapter daprovince = new SqlDataAdapter (addresssql.strgetprovince,sqlconfig.databaseconnection);
Daprovince.fill (ds. province);

SqlDataAdapter dacity = new SqlDataAdapter (addresssql.strgetcity,sqlconfig.databaseconnection);
Dacity.fill (ds. City);

SqlDataAdapter daport = new SqlDataAdapter (addresssql.strgetport,sqlconfig.databaseconnection);
Daport.fill (ds. Port);

Regiontypedata regiontypeds = Getregiontype (languagecode);

foreach (Addressdata.regionrow region in DS. Region)
{
if (!region. Isnameguidnull ())
{
Region. Name = Dbdisplaystring.getdisplay (region. nameguid,languagecode,vdisplaytable);
}
if (RegionTypeDS.RegionType.FindByRegionTypeID (region). Regiontypeid)!= null)
{
Region. Regiontypename = RegionTypeDS.RegionType.FindByRegionTypeID (region. Regiontypeid). ABBR;
}
}

foreach (Addressdata.countryrow country in DS. Country)
{
if (!country. Isnameguidnull ())
{
Country. Name = Dbdisplaystring.getdisplay (country. nameguid,languagecode,vdisplaytable);
}
}

foreach (Addressdata.provincerow Province in DS. Province)
{
if (!province. Isnameguidnull ())
{
Province. Name = Dbdisplaystring.getdisplay (province. nameguid,languagecode,vdisplaytable);
}
}

foreach (Addressdata.cityrow city) ds. City)
{
if (!city. Isnameguidnull ())
{
City. Name = Dbdisplaystring.getdisplay (city. Nameguid, languagecode,vdisplaytable);
}
}

foreach (Addressdata.portrow port in DS. Port)
{
if (!port. Isnameguidnull ())
{
Port. Name = Dbdisplaystring.getdisplay (port. nameguid,languagecode,vdisplaytable);
}
}

return DS;
}
This allows you to get the data in the language of the user using a single name field, regardless of which language the user uses. The reason why the join is not directly in the database is that the method of acquiring the multilingual data is not so simple as to take out the database data directly, if the user wants to get a piece of the language of that kind of data is not exactly? We took a system default language data, so which one is the system default language? Visible if all of this is done in SQL, there will be a lot of arguments to pass in, and the SQL statements will become very complex and not conducive to encapsulation, and given the complexity of SQL statement debugging, I have no choice.
So what about updating the data back to the database? Look at the following code:
private void Deletedisplay (Addressdata ds, Guid refguid)
{
foreach (Addressdata.addressdisstrrow dis in DS. Addressdisstr.select ("Refguid = '" +refguid+ ""))
{
Dis. Delete ();
}
}
private void Deleteport (addressdata ds, int portid)
{
if (!ds. Port.findbyportid (Portid). Isnameguidnull ())
{
Deletedisplay (Ds,ds. Port.findbyportid (Portid). NAMEGUID);
}
Ds. Port.findbyportid (Portid). Delete ();
}

private void Deletecity (addressdata ds, int Cityid)
{
if (!ds. City.findbycityid (Cityid). Isnameguidnull ())
{
Deletedisplay (Ds,ds. City.findbycityid (Cityid). NAMEGUID);
}
foreach (Addressdata.portrow port in DS. Port)
{
if ((int) port["Cityid", datarowversion.original] = = Cityid)
{
Port. RejectChanges ();
Deleteport (Ds,port. Portid);
}
}
Ds. City.findbycityid (Cityid). Delete ();
}

private void Deleteprovince (addressdata ds, int provinceid)
{
if (!ds. Province.findbyprovinceid (Provinceid). Isnameguidnull ())
{
Deletedisplay (Ds,ds. Province.findbyprovinceid (Provinceid). NAMEGUID);
}
foreach (Addressdata.cityrow city) ds. City)
{
if ((int) city["Provinceid", datarowversion.original] = = Provinceid)
{
City. RejectChanges ();
Deletecity (ds,city. Cityid);
}
}
Ds. Province.findbyprovinceid (Provinceid). Delete ();
}
public void Updateprovince (Addressdata ds, String languagecode)
{
SqlDataAdapter daaddressdisstr = new SqlDataAdapter (addresssql.strgetaddressdisstr,sqlconfig.databaseconnection);
Daaddressdisstr.fill (ds. ADDRESSDISSTR);

foreach (Addressdata.provincerow Province in DS. Province)
{
if (province. RowState = = datarowstate.deleted)
{
Province. RejectChanges ();
Deleteprovince (ds,province. Provinceid);
}
else if (province. RowState = = DataRowState.Added | | Province. RowState = = datarowstate.modified)
{
if (province. Isnamenull () &&!province. Isnameguidnull ())
{
Dbdisplaystring.savedisplay (languagecode,province. nameguid,null,vdisplaytable);
}
else if (!province. Isnamenull ())
{
if (province. Isnameguidnull ())
{
Province. Nameguid = Guid.NewGuid ();
}
Dbdisplaystring.savedisplay (languagecode,province. Nameguid,province. name,vdisplaytable);
}
}
}
datatableextend[] DTS = new DATATABLEEXTEND[4];
Dts[0] = new Datatableextend (ds. Port, "Port");
DTS[1] = new Datatableextend (ds. City, "City");
DTS[2] = new Datatableextend (ds. Province, "province");
DTS[3] = new Datatableextend (ds. addressdisstr,vdisplaytable);
Sqlmodify.modifydatabase (dts);
}
It can be seen that this operation will be very troublesome, for a delete operation, we need to erase all the multilingual data for that record, and then unfortunately, for records that have been deleted in the dataset, although it still exists but is inaccessible, and, unfortunately, because most tables are associated with the reason, We don't even know how much data has been deleted by the Cascade: (No Way, RejectChanges (), and then through the association to find all the data deleted by the Cascade, delete the multilingual data, and then delete themselves, notice that I use the Select method here, this is a very convenient approach, Reduces the round-trip to the database, thereby saving time with the database at the expense of some memory. For new and modified operations, we are burdened with another task, that is, the data in the name field according to the user's current language, update the corresponding records in the database, there will be many situations, such as the field data is new, the default language data is not, the current language of the data is not, Or the data in the default language has no data in the current language, or if the user clears the data from the field, we have to find the corresponding record in the multilingual table and delete it ... Wait, helpless, we had to let Dbdisplaystring.savedisplay () to dispose of all this.

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.