简体   繁体   English

使用C#更新数据库中的字段时遇到问题

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

I'm trying to write a program that will change a particular GUID in a table to a GUID that the user specifies. 我正在尝试编写将表中的特定GUID更改为用户指定的GUID的程序。 The issue is when it tries to overwrite the old GUID with the new one it will give me the following error: 问题是,当尝试用新的GUID覆盖旧的GUID时,会出现以下错误:

You cannot add or change a record because a related record is required in table tblEF 您无法添加或更改记录,因为表tblEF中需要相关记录

The database has 5 tables. 该数据库有5个表。 The main table, tblA, has a Primary Key set to pkAccounts. 主表tblA的主键设置为pkAccounts。 The other tables all have a Foreign Key, called fkAccounts. 其他表都具有外键,称为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. 如果我手动打开数据库并编辑关系以具有“级联更新相关字段”,则我的程序将更新GUID,但使用该数据库的程序将不再起作用。

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. 为了解决这个问题,我添加了一些变量,这些变量会在主表上删除主键,然后在程序完成替换所有GUID之后再添加主键。 In this case I will get Syntax error in Alter Table 在这种情况下,我会在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. 再加上我第一次弄乱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();
 }

Basically, how do I edit the GUID 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? 基本上,如何在不手动打开数据库的情况下编辑Cascade Update,运行程序,然后再次打开数据库并取消选中Cascade Update的情况下,如何在5个数据库中编辑GUID字段?

Instead of dropping constraints, just add a new row to the table and copy the data over. 无需删除约束,只需在表中添加新行并复制数据即可。

  1. Insert a new row with the GUID the user wants. 用用户想要的GUID插入新行。
  2. Then update the row with the data from the old row. 然后使用旧行中的数据更新行。
  3. Change all foreign key references to the new row 将所有外键引用更改为新行
  4. 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. 添加一个名为“ UserEnteredGuid”的列,并在该值不为null时向用户而不是真实的 GUID显示该列。 This is really the only sane approach. 这确实是唯一理智的方法。

Normally, primary keys would be values that are never modified. 通常,主键是永远不会修改的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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