简体   繁体   中英

Problems creating a one-to-one relationship with an access database in c#

I am trying to create a one to one relationship in a Microsoft Access database using c#. So far, I have the following code which (from what I understand) should create a one-to-one relationship:

OleDbConnection dbCon = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = d:\\Test.mdb; Jet OLEDB:System Database=c:\\secure.mdw; User Id=321; Password=123;");
dbCon.Open();
String szTable1 = "CREATE TABLE Table1 (Field1 int UNIQUE NOT NULL PRIMARY KEY)";
OleDbCommand table1Cmd = new OleDbCommand(szTable1, dbCon);
table1Cmd.ExecuteNonQuery();    
String szTable2 = "CREATE TABLE Tablea (Fielda int UNIQUE NOT NULL PRIMARY KEY)";
OleDbCommand table2Cmd = new OleDbCommand(szTable2, dbCon);
table2Cmd.ExecuteNonQuery();
String szRelationship1 = "ALTER TABLE Table1 ADD CONSTRAINT TableLink FOREIGN KEY (Field1) REFERENCES Tablea(Fielda) ON DELETE CASCADE";
OleDbCommand relationship1Cmd = new OleDbCommand(szRelationship1, dbCon);
relationship1Cmd.ExecuteNonQuery();
dbCon.Close();

This creates the 2 tables and creates a relationship between them, but when I look at the MSysRelationship table in Access, it is giving me a grbit of 4096 (which would be one-to-many) instead of 4097 which is one-to-one:

该图显示了MSysRelationships表,其grbit为4096
(source: richardn.co.uk )

When I look at the relationship in the relationships window, it is showing me on the relationship diagram that it is a one-to-many relationship, but in the edit relationship window it is saying it is a one-to-one relationship:

该图显示了来自Microsoft Access的冲突信息
(source: richardn.co.uk )

I'm really stuck here because in Access if I edit the relationship and change one of the settings and click on ok, it instantly becomes a one-to-one relationship in the window and the grbit changes to 4097. What am I missing in my code to set the relationship up as a one-to-one with a grbit of 4097 originally?

As suggested by my answer to a similar question here , this appears to be a glitch in the way relationships are displayed in the Relationships tab when the relationship is created using DDL. In that previous answer I didn't go so far as to change something in the "Edit Relationships" dialog and see if the little line in the Relationships tab actually changed, but your findings indicate that it does.

I don't believe that you are doing anything wrong , and the "fix" would be to tweak the MSysRelationships.grbit value, but you can't get at the [MSys...] tables from an OleDb connection anyway. So, this might simply be one of those little quirks we have to accept.

And really, if the relationship actually behaves as a One-to-One then it doesn't really matter what the little line in the Relationships tab looks like. Run a couple of tests to ensure that the relationship and the Referential Integrity rules behave as they should, and if they do, then just move on to other things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM