我正在嘗試編寫一個程序,該程序將表中的某個GUID更改爲用戶指定的GUID。 問題是當它試圖用新的覆蓋舊的GUID它將給我以下錯誤:使用C更新數據庫中的字段時遇到問題#
You cannot add or change a record because a related record is required in table tblEF
該數據庫有5個表。主表tblA的主鍵設置爲pkAccounts。其他表都有一個外鍵,稱爲fkAccounts。
所有關係都設置爲實施參照完整性和級聯刪除相關記錄。如果我手動打開數據庫並編輯關係以具有Cascade Update Related Fields,我的程序將更新GUID,但數據庫所用的程序將不再起作用。
試圖克服這一點,我添加到將放棄主鍵在主表上的變量,然後在程序完成替換所有GUID後添加主鍵。在這種情況下,我將得到Alter表中的語法錯誤
這是我的代碼。對不起,如果它是凌亂的,但這是我的第一個程序之一。加上我第一次搞亂SQL的東西。
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();
}
基本上,我怎麼編輯5數據庫中的GUID字段,而手動打開數據庫檢查級聯更新,運行我的程序,然後重新打開數據庫,並取消選中級聯更新?
這並沒有解決您的問題,但您應該知道,您正在通過構建您的更新查詢來打開您的應用程序,直至SQL注入攻擊。使用準備好的語句,以避免後來的潛在的麻煩:) – 2010-11-24 23:42:39