简体   繁体   English

插入已插入ID的表中

[英]Insert in a table which has already id inserted

I have a table 'Agent' its ID is already inserted before and displayed in a textbox for using it in my insertion , now I try just to add the rest of records but in couldn't do that an error is displayed ...(I'm working in asp with c#) and SQL SERVER : 我有一个表“ Agent”,其ID之前已经插入并显示在文本框中,供在插入时使用它,现在我尝试仅添加其余记录,但无法显示错误...(我正在使用C#和SQL SERVER在ASP中工作:

Violation of PRIMARY KEY constraint 'PK__Agent. 违反主键约束'PK__Agent。 "Can not insert duplicate key in object 'dbo.Agent. "The duplicate key value is (1). “无法在对象'dbo.Agent中插入重复键。”重复键值为(1)。 The statement has been terminated. 该语句已终止。

this my code behind : 这是我的代码背后:

protected void Button_validerinfo_Click(object sender, EventArgs e)
{
    try
    {
        c.cmd = c.cn.CreateCommand();
        c.cmd.CommandText = "AjouterAgent";
        c.cmd.CommandType = CommandType.StoredProcedure;
        if (c.cn.State == ConnectionState.Closed)
        {
            c.cn.Open();
        }

        c.cmd.Parameters.Add("@ppr", SqlDbType.Int);
        c.cmd.Parameters.Add("@lieu", SqlDbType.VarChar);
        c.cmd.Parameters.Add("@adresspro", SqlDbType.VarChar);
        c.cmd.Parameters.Add("@adressperso", SqlDbType.VarChar);
        c.cmd.Parameters.Add("@telbureau", SqlDbType.VarChar);
        c.cmd.Parameters.Add("@telgsm", SqlDbType.VarChar);
        c.cmd.Parameters.Add("@email", SqlDbType.VarChar);

        c.cmd.Parameters.Add("@np", SqlDbType.VarChar);
        c.cmd.Parameters.Add("@proff", SqlDbType.VarChar);
        c.cmd.Parameters.Add("@empl", SqlDbType.VarChar);
        c.cmd.Parameters.Add("@retraite", SqlDbType.VarChar);
        c.cmd.Parameters.Add("@TypeOperation", SqlDbType.Int);

        c.cmd.Parameters["@ppr"].Value = TextBox_PPR.Text;
        c.cmd.Parameters["@lieu"].Value = TextBox_ln.Text;
        c.cmd.Parameters["@adresspro"].Value = TextBox_adrspro.Text;
        c.cmd.Parameters["@adressperso"].Value = TextBox_adrssperso.Text;
        c.cmd.Parameters["@telbureau"].Value = TextBox_bureau.Text;
        c.cmd.Parameters["@telgsm"].Value = TextBox_gsm.Text;
        c.cmd.Parameters["@email"].Value = TextBox_email.Text;
        c.cmd.Parameters["@np"].Value = TextBox_npconj.Text;
        c.cmd.Parameters["@proff"].Value = TextBox_prof.Text;
        c.cmd.Parameters["@empl"].Value = TextBox_empl.Text;
        c.cmd.Parameters["@retraite"].Value = DropDownList_retraite.SelectedValue;
        c.cmd.Parameters["@TypeOperation"].Value = 0;
        c.cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
    finally
    {
        if (c.cn.State == ConnectionState.Open)
        {
            c.cn.Close();
        }
    }
}

and my stroredprocedure : 和我的程序:

USE [CVtech]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[AjouterAgent]
 @ppr int,
@lieu varchar(100),
@adresspro varchar(100),
@adressperso varchar(100),
@telbureau varchar(100),
@telgsm varchar(100),
@email varchar(100),

@np varchar(100),
@proff varchar(100),
@empl varchar(100),
@retraite varchar(3),
@TypeOperation nvarchar(1)
as
if(@TypeOperation = '0')
begin tran
 if exists ( select ppr from Agent where PPR = @ppr)
 begin
    insert into Agent (LieuNaissance,AdressePro, AdressePerso,TelBureau,TelPerso,Email)
    values (@lieu,@adresspro, @adressperso,@telbureau,@telgsm,@email)
end

insert into Conjoint (PPR,NomPrenom , Profession, Employeur, Retraite) values (@ppr ,@np ,@proff,@empl,@retraite)
commit

Does your table have Identity Specification switched to off? 表格中的身份规范是否已关闭? To check go on your table, right click, design, select the primary key, in the options displayed you should see IDENTITY SPECIFICATION. 要检查表,右键单击,设计,选择主键,在显示的选项中,您应该看到IDENTITY SPECIFICATION。 This must be set to yes in your case. 在您的情况下,必须将其设置为yes。 For more information about this, you can view this link from MSDN : http://msdn.microsoft.com/en-us/library/x5s13zy2.aspx 有关此的更多信息,您可以从MSDN查看此链接: http : //msdn.microsoft.com/zh-cn/library/x5s13zy2.aspx

Obviously, if you want to do an update, you must execute an update statement, and not insert the same record again. 显然,如果要进行更新,则必须执行一条update语句,并且不要再次插入相同的记录。

Yes, first my question is, are you send the column value (which one has primary key constrain) from user? 是的,首先我的问题是,您是从用户发送列值(哪个具有主键约束)吗? if no mean check that column has the identity or not(if no mean change it) if yes then you have to pass the unique value for that textbox 如果没有意思,请检查该列是否具有标识(如果没有意思,请更改它),如果是,则必须传递该文本框的唯一值

It doesn't seem to make sense that you are trying to do an insert where some value in Agent exists. 您试图在Agent中存在某些值的位置进行插入似乎没有任何意义。

if exists ( select ppr from Agent where PPR = @ppr)
begin
    insert into Agent (LieuNaissance,AdressePro, AdressePerso,TelBureau,TelPerso,Email)
    values (@lieu,@adresspro, @adressperso,@telbureau,@telgsm,@email)
end

I would expect that if the item exists that you want to do an UPDATE not an INSERT . 我希望,如果该项目存在,则您想执行UPDATE而不是INSERT

USE [CVtech]
GO
/****** Object:  StoredProcedure [dbo].[AjouterAgent]    Script Date: 02/04/2014 12:00:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[AjouterAgent]
 @ppr int,
@lieu varchar(100),
@adresspro varchar(100),
@adressperso varchar(100),
@telbureau varchar(100),
@telgsm varchar(100),
@email varchar(100),

@np varchar(100),
@proff varchar(100),
@empl varchar(100),
@retraite varchar(3),
@TypeOperation nvarchar(1)
as
if(@TypeOperation = '0')
begin tran
 if exists ( select ppr from Agent where PPR = @ppr)
 begin
    update Agent set  LieuNaissance=@lieu,AdressePro=@adresspro, AdressePerso=@adressperso , TelBureau=@telbureau,TelPerso=@telgsm,Email=@email
 where PPR = @ppr
end
 if exists ( select ppr from Agent where PPR = @ppr)
  begin 
  update Conjoint set PPR=@ppr,NomPrenom =@np, Profession=@proff, Employeur=@empl, Retraite=@retraite  where ppr= @ppr
  end 
  else 
     insert into Conjoint (PPR,NomPrenom , Profession, Employeur, Retraite) values (@ppr ,@np ,@proff,@empl,@retraite)

commit 

So the code would be this: 所以代码是这样的:

ALTER proc [dbo].[AjouterAgent]
 @ppr int,
@lieu varchar(100),
@adresspro varchar(100),
@adressperso varchar(100),
@telbureau varchar(100),
@telgsm varchar(100),
@email varchar(100),

@np varchar(100),
@proff varchar(100),
@empl varchar(100),
@retraite varchar(3),
@TypeOperation nvarchar(1)
as
if(@TypeOperation = '0')
begin tran
 if exists ( select ppr from Agent where PPR = @ppr)
 begin
    UPDATE Agent
    SET LieuNaissance = @lieu
        ,AdressePro = @adresspro
        ,AdressePerso = @adressperso
        ,TelBureau = @telbureau
        ,TelPerso = @telgsm
        ,Email = @email
    WHERE ppr = @ppr
end

insert into Conjoint (PPR,NomPrenom , Profession, Employeur, Retraite) values (@ppr ,@np ,@proff,@empl,@retraite)
commit

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

相关问题 获取最后插入的ID并使用该ID填充/插入另一个表 - get the last inserted id and populate / insert into another table with that id 如何为尚未插入的行计算(自定义ID)? - How to calculate the (custom id) for a row which has not been inserted yet? 将多个数据插入两个表,并需要将最后插入的ID插入另一个表 - Insert multiple data into two tables and need to insert the last inserted id into another table 确定表是否已包含具有特定ID的记录的最佳方法是什么? - What is the best way to determine if table already has record with specific ID? 为什么当我尝试插入具有Employee类型的Manager的Employee对象时,为什么要插入两个记录? - Why two records are inserted when i try to insert Employee object which has Manager of type Employee? SQL插入新插入的行的ID - SQL INSERT an ID of freshly inserted row 执行插入命令,返回插入的Id在Sql - Execute Insert command and return inserted Id in Sql 调用具有插入语句并返回表的SP - Calling a SP which has Insert Statement and returns a table 无法在(SEQUENCE_NO)作为列的历史记录表中插入值 - Not able to insert values in history table which has (SEQUENCE_NO) as Column 如何调用插入数据库的数据的ID? - How to call id of data which is inserted into database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM