插入已插入id的表中

时间:2014-02-04 11:42:09

标签: c# asp.net sql sql-server sql-insert

我有一个表'Agent'它的ID已经插入之前并显示在文本框中以便在我的插入中使用它,现在我只是尝试添加其余的记录,但是无法做到这一点,显示错误。 ..(我在使用c#的asp工作)和SQL SERVER:

  

违反PRIMARY KEY约束'PK__Agent。 “无法在对象'dbo.Agent中插入重复键。”重复键值为(1)。   声明已经终止。

我的代码背后:

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();
        }
    }
}

和我的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

5 个答案:

答案 0 :(得分:1)

您的桌子是否已将身份规格切换为关闭?要查看桌面上的内容,请右键单击,设计,选择主键,在显示的选项中,您应该看到IDENTITY SPECIFICATION。在您的情况下,必须将其设置为yes。有关此问题的详细信息,您可以从MSDN查看此链接:http://msdn.microsoft.com/en-us/library/x5s13zy2.aspx

显然,如果要进行更新,则必须执行更新语句,而不是再次插入相同的记录。

答案 1 :(得分:0)

是的,首先我的问题是,您是否从用户发送了列值(哪个具有主键约束)?如果没有意味着检查该列是否具有该标识(如果没有意味着更改它),如果是,那么您必须传递该文本框的唯一值

答案 2 :(得分:0)

尝试进行插入时,代理中存在某些值,这似乎没有意义。

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

如果项目存在,您希望UPDATE而不是INSERT

答案 3 :(得分:0)

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 

答案 4 :(得分:0)

所以代码就是这样:

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