I'm trying to write a program that will change a particular GUID in a table to a GUID that the user specifies. The issue is when it tries to overwrite the old GUID with the new one it will give me the following error:
You cannot add or change a record because a related record is required in table tblEF
The database has 5 tables. The main table, tblA, has a Primary Key set to pkAccounts. The other tables all have a Foreign Key, called fkAccounts.
All the relationships are set to Enforce Referential Integrity and Cascade Delete Related Records. If I manually open the database and edit the relationships to have Cascade Update Related Fields, my program will update the GUIDs BUT the program that the database is used in will no longer work.
Trying to overcome this I added in to variables that would drop the primary key on the main table then add the primary key back after the program finished replacing all the GUIDs. In this case I will get Syntax error in Alter Table
Here is my code. Sorry if it is messy but this is one of my first programs. Plus my first time messing around with SQL stuff.
try
{
string GetRI = "SELECT fkAccountGUID FROM tblR";
string GetTWI = "SELECT pkAccountGUID FROM tblTW";
string GetAI = "SELECT pkAccountGUID FROM tblA";
string GetEAI = "SELECT fkAccountGUID FROM tblEAI";
string GetEF = "SELECT fkAccountGUID FROM tblEF";
string NoPK = "ALTER TABLE tblA DROP CONSTRAINT pkAID";
string PK = "ALTER TABLE tblA ADD PRIMARY KEY (pkAID)";
DataSet ds = new DataSet();
//create a connection to the database
OleDbConnection ConnectDatabase = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data Source=C:\db.mdb;" +
"Persist Security Info=True;" +
"Jet OLEDB:Database Password=123;");
//open the connection to the database
ConnectDatabase.Open();
//creates an adapter and runs the string command from the database connection
//it will then fill the information in the dataset in its own table
OleDbDataAdapter DatabaseAdapter = new OleDbDataAdapter(GetRI, ConnectDatabase);
DatabaseAdapter.Fill(ds, "tblR");
OleDbDataAdapter DatabaseAdapter1 = new OleDbDataAdapter(GetAI, ConnectDatabase);
DatabaseAdapter.Fill(ds, "tblA");
OleDbDataAdapter DatabaseAdapter2 = new OleDbDataAdapter(GetTWI, ConnectDatabase);
DatabaseAdapter.Fill(ds, "tblTW");
OleDbDataAdapter DatabaseAdapter3 = new OleDbDataAdapter(GetEAI, ConnectDatabase);
DatabaseAdapter.Fill(ds, "tblEAI");
OleDbDataAdapter DatabaseAdapter4 = new OleDbDataAdapter(GetEF, ConnectDatabase);
DatabaseAdapter.Fill(ds, "tblEF");
//get old GUID
Console.WriteLine("What is the current GUID?");
string OldGUID = Console.ReadLine();
char ap = '\x0027';
OldGUID = ap + OldGUID + ap;
//get new GUID
Console.WriteLine("What is the new GUID name?");
string NewGUID = Console.ReadLine();
NewGUID = ap + NewGUID + ap;
//test lines
//Console.WriteLine(NewGUID);
//Console.WriteLine(OldGUID);
//UPDATE string to rename the old GUID to the New GUID
string UpdateR = "UPDATE tblR SET fkAccountGUID=" + NewGUID + "WHERE fkAccountGUID=" + OldGUID;
string UpdateA = "UPDATE tblA SET pkAccountGUID=" + NewGUID + "WHERE pkAccountGUID=" + OldGUID;
string UpdateTW = "UPDATE tblTW SET pkfkAccountGUID=" + NewGUID + "WHERE pkfkAccountGUID=" + OldGUID;
string UpdateEA = "UPDATE tblTW SET fkAccountGUID=" + NewGUID + "WHERE fkAccountGUID=" + OldGUID;
string UpdateEF = "UPDATE tblEF SET fkAccountGUID=" + NewGUID + "WHERE fkAccountGUID=" + OldGUID;
//create the variables to run the string commands
OleDbCommand updatecmd0 = new OleDbCommand(UpdateF);
OleDbCommand updatecmd1 = new OleDbCommand(UpdateR);
OleDbCommand updatecmd2 = new OleDbCommand(UpdateEA);
OleDbCommand updatecmd3 = new OleDbCommand(UpdateTW);
OleDbCommand updatecmd4 = new OleDbCommand(UpdateA);
OleDbCommand nocheckcmd = new OleDbCommand(NoPK);
OleDbCommand checkcmd = new OleDbCommand(PK);
//have the commands connect to the database
nocheckcmd.Connection = ConnectDatabase;
updatecmd0.Connection = ConnectDatabase;
updatecmd1.Connection = ConnectDatabase;
updatecmd2.Connection = ConnectDatabase;
updatecmd3.Connection = ConnectDatabase;
updatecmd4.Connection = ConnectDatabase;
checkcmd.Connection = ConnectDatabase;
//Run the commands
nocheckcmd.ExecuteNonQuery();
updatecmd0.ExecuteNonQuery();
updatecmd1.ExecuteNonQuery();
updatecmd2.ExecuteNonQuery();
updatecmd3.ExecuteNonQuery();
updatecmd4.ExecuteNonQuery();
checkcmd.ExecuteNonQuery();
//Dispose the adapter and close the connection to the database.
DatabaseAdapter.Dispose();
ConnectDatabase.Close();
//console will display the string if everything completed
Console.WriteLine("Success. Press any key to exit.");
Console.Read();
}
catch (OleDbException Error)
{
//when an error occurs display the error in the console
Console.WriteLine(Error.Message);
Console.Read();
}
Basically, how do I edit the GUID开发者_JAVA技巧 fields among the 5 database without manually opening the database checking Cascade Update, running my program, then opening the database again and unchecking Cascade Update?
Instead of dropping constraints, just add a new row to the table and copy the data over.
- Insert a new row with the GUID the user wants.
- Then update the row with the data from the old row.
- Change all foreign key references to the new row
- Delete the old row
Add a column called "UserEnteredGuid", and display that to the user instead of the real GUID whenever the value is not null. This is really the only sane approach.
Normally, primary keys would be values that are never modified.
精彩评论