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

时间:2010-11-24 23:04:57

标签: c# sql database visual-studio

我正在尝试编写一个程序,它将表中的特定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();
 }

基本上,如何编辑5数据库中的GUID字段而不手动打开数据库检查Cascade Update,运行我的程序,然后再次打开数据库并取消选中Cascade Update?

2 个答案:

答案 0 :(得分:0)

不是删除约束,只需向表中添加一行并复制数据。

  1. 使用用户想要的GUID插入新行。
  2. 然后使用旧行中的数据更新行。
  3. 将所有外键引用更改为新行
  4. 删除旧行

答案 1 :(得分:0)

添加一个名为“UserEnteredGuid”的列,只要值不为null,就会向用户显示该列而不是 real GUID。这真是唯一理智的方法。

通常,主键是永不修改的值。