标识列错误

时间:2014-08-25 11:59:49

标签: sql asp.net

我已经制作了一个存储过程,用于通过前端在表中插入值。我在存储过程中将IDENTITY列作为ID。但是每当我添加它时,它都会给出无法在表格中为标识列插入显式值的信息' BusinessUnit'当IDENTITY_INSERT设置为OFF时。请参阅代码供您参考。

ALTER PROCEDURE [dbo].[Add_BusinessUnit]
-- Add the parameters for the stored procedure here
@Id int,
@Name nvarchar(100)
AS

BEGIN

     INSERT INTO Career.BusinessUnit(Id,Name) values (@Id,@Name)
END

同时看到按钮点击,因为它是从前端添加的。

protected void btnAddBusiness_Click(object sender, EventArgs e)
{
    using (SqlConnection con = new SqlConnection(constring))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "Add_BusinessUnit";
        cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = 0;
        cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtBusinessUnitOther.Text.Trim();
        cmd.Connection = con;
        try
        {
            cmd.ExecuteNonQuery();
            // BindContrydropdown();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);//You Can Haave Messagebox here
        }
        finally
        {
            con.Close();
        }
    }
}

请帮忙。

2 个答案:

答案 0 :(得分:1)

因为您手动设置标识列的值。 你应该是这样的

ALTER PROCEDURE [dbo].[Add_BusinessUnit]
-- Add the parameters for the stored procedure here
@Name nvarchar(100)
AS

BEGIN

     INSERT INTO Career.BusinessUnit(Name) values (@Name)
END

你的c#代码应该是

protected void btnAddBusiness_Click(object sender, EventArgs e)
{
    using (SqlConnection con = new SqlConnection(constring))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "Add_BusinessUnit";

        cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtBusinessUnitOther.Text.Trim();
        cmd.Connection = con;
        try
        {
            cmd.ExecuteNonQuery();
            BindContrydropdown();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);//You Can Haave Messagebox here
        }
        finally
        {
            con.Close();
        }
    }
}

答案 1 :(得分:0)

SQL Server过程

ALTER PROCEDURE [dbo].[Add_BusinessUnit]
@Id int  OUTPUT,
@Name nvarchar(100)
AS
BEGIN
  SET NOCOUNT ON;
     INSERT INTO Career.BusinessUnit(Name) 
     values (@Name)

     SET @Id = SCOPE_IDENTITY();
END

// Call procedure


DECLARE @ID INT;

EXECUTE [dbo].[Add_BusinessUnit] 'Mark', @ID OUTPUT

SELECT @ID; --<-- Variable will be populated with newly inserted ID value

C#代码

// define connection and command, in using blocks to ensure disposal
using(SqlConnection conn = new SqlConnection(pvConnectionString ))
using(SqlCommand cmd = new SqlCommand("dbo.Add_BusinessUnit", conn))
{
    cmd.CommandType = CommandType.StoredProcedure;

    // set up the parameters
    cmd.Parameters.Add("@Name", SqlDbType.VarChar, 100);
    cmd.Parameters.Add("@ID", SqlDbType.Int).Direction = ParameterDirection.Output;

    // set parameter values
    cmd.Parameters["@Name"].Value = Name;

    // open connection and execute stored procedure
    conn.Open();
    cmd.ExecuteNonQuery();

    // read output value from @Id
    int NewID = Convert.ToInt32(cmd.Parameters["@ID"].Value);
    conn.Close();
}