簡體   English   中英

使用C#更新數據庫中的字段時遇到問題

[英]Trouble updating fields in a database with C#

我正在嘗試編寫將表中的特定GUID更改為用戶指定的GUID的程序。 問題是,當嘗試用新的GUID覆蓋舊的GUID時,會出現以下錯誤:

您無法添加或更改記錄,因為表tblEF中需要相關記錄

該數據庫有5個表。 主表tblA的主鍵設置為pkAccounts。 其他表都具有外鍵,稱為fkAccounts。

所有關系都設置為“強制引用完整性”和“級聯刪除相關記錄”。 如果我手動打開數據庫並編輯關系以具有“級聯更新相關字段”,則我的程序將更新GUID,但使用該數據庫的程序將不再起作用。

為了解決這個問題,我添加了一些變量,這些變量會在主表上刪除主鍵,然后在程序完成替換所有GUID之后再添加主鍵。 在這種情況下,我會在Alter Table中得到語法錯誤

這是我的代碼。 很抱歉,如果它很亂,但這是我的第一個程序。 再加上我第一次弄亂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();
 }

基本上,如何在不手動打開數據庫的情況下編輯Cascade Update,運行程序,然后再次打開數據庫並取消選中Cascade Update的情況下,如何在5個數據庫中編輯GUID字段?

無需刪除約束,只需在表中添加新行並復制數據即可。

  1. 用用戶想要的GUID插入新行。
  2. 然后使用舊行中的數據更新行。
  3. 將所有外鍵引用更改為新行
  4. 刪除舊行

添加一個名為“ UserEnteredGuid”的列,並在該值不為null時向用戶而不是真實的 GUID顯示該列。 這確實是唯一理智的方法。

通常,主鍵是永遠不會修改的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM