-2

错误信息

消息 2601,级别 14,状态 1,第 1 行无法在具有唯一索引“唯一键 RTU”的对象“dbo.RTU_ADDRESS”中插入重复键行。重复键值为 (2, 5, 4, 6, 500, 610, IUY )。该语句已终止。

表格脚本

USE [hts]
GO

/****** Object:  Table [dbo].[RTU_ADDRESS]    Script Date: 03/19/2013 21:07:19 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[RTU_ADDRESS](
    [RowNo] [int] IDENTITY(1,1) NOT NULL,
    [StationId] [int] NULL,
    [SystemId] [int] NULL,
    [CCNumber] [int] NULL,
    [LineNumber] [int] NULL,
    [RTUNumber] [int] NULL,
    [SRTUNumber] [int] NULL,
    [Description] [char](100) NOT NULL,
    [SDescription] [char](100) NOT NULL,
    [Area_ID] [char](10) NOT NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

索引脚本

USE [hts]
GO

/****** Object:  Index [Unique Key RTU]    Script Date: 03/19/2013 21:08:31 ******/
CREATE UNIQUE NONCLUSTERED INDEX [Unique Key RTU] ON [dbo].[RTU_ADDRESS] 
(
    [StationId] ASC,
    [SystemId] ASC,
    [CCNumber] ASC,
    [LineNumber] ASC,
    [RTUNumber] ASC,
    [SRTUNumber] ASC,
    [Area_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO

C#代码:

SqlDataAdapter sqlAdapter = null;
                strSelectCmd = "SELECT RowNo FROM RTU_ADDRESS";
                SqlCommand sqlCmd = new SqlCommand();
                sqlCmd.Connection = m_SqlConn;
                sqlCmd.CommandText = strSelectCmd;
                int i = Convert.ToInt32(sqlCmd.ExecuteScalar());
                sqlAdapter = new SqlDataAdapter();
                sqlAdapter.SelectCommand = sqlCmd;


                strInsCmd = "UPDATE RTU_ADDRESS SET RTUNumber='" + rtunum.Text + "', SRTUNumber='" + srtunum.Text + "', Description='" + desc.Text + "', SDescription='" + sdesc.Text + "' WHERE Area_ID='" + area_ID.Text + "' AND StationId='" + station.Text + "' AND SystemId='" + sys.Text + "' AND CCNumber='" + ccnum.Text + "' AND LineNumber='" + linenum.Text + "'";

                strErrorMsg = Utilities.ExecuteSQLCommand(m_SqlConn, strInsCmd);
                string check = "duplicate";
                bool d = check.Any(strErrorMsg.Contains);
                if (d == true)
                {
                    MessageBox.Show(" Duplicates Entries Found", "Database Insertion");
                }
                else if (string.IsNullOrEmpty(strErrorMsg) == false)
                {
                    MessageBox.Show(this, "Error!Unable to Add New RTU Configurations!", "RTU Configurations error");
                }
                else
                {
                    d = false;
                    this.Close();
                }

我现在不知道该怎么做,我没有放任何重复项。

我只想更新 RTUNumber、SRTUNumber、Description 和 SDescription,同时保持我的 stationID、systemID、CCNumber、LineNumber 和 Area_ID 不变。请帮忙

更新:选择语句

SELECT * FROM RTU_ADDRESS WHERE Area_ID='IUY ' AND StationId='2' AND SystemId='5' AND CCNumber='4' AND LineNumber='6'

给我 2 个结果,但 RTUNumber 和 SRTUNumber 不同。

请帮忙

4

2 回答 2

0

好吧,很明显,您尝试执行的更新将在您的表中生成重复项。您是否尝试过根据您尝试用于更新的值对表进行选择?您很可能会找到现有的记录...

于 2013-03-19T17:16:56.217 回答
0

查看更新语句的 where 子句中的条件,如果有多个记录具有相同的 AreaId、SystemId、StationId、CCNumber 和 LineNumber,您将收到此错误,因为您尝试使用相同的 RTUNumber 和 SRTUNumber 更新它们。您可以通过运行以下 sql 来检查:

SELECT AreaId, SystemId, StationId, CCNumber, LineNumber, COUNT(*)
FROM RTU_ADDRESS
GROUP BY AreaId, SystemId, StationId, CCNumber, LineNumber
HAVING COUNT(*) > 1

如果返回任何行,那么您将收到“重复”错误。

事实上,你的编辑版本说情况就是这样。尽管这两条记录可能具有不同的 SRTUNumber 和 RTUNumber,但您尝试在更新语句中为它们提供相同的 SRTUNumber 和 RTUNUMBER。

于 2013-03-19T18:13:28.150 回答