不向DB插入值,返回0

时间:2017-11-06 18:36:17

标签: c# sql-server insert sql-insert

我已经尝试过我能想到的一切。我正在创建和对象,然后将其插入SQL Server数据库。但是对于这种特殊的情绪,我没有工作(我从其他有效的模块中回收)。

public int Insert(int userId, int refNum, string name, string position, string lastPlace, string duration, int currentEmp,
                  int rehire, string reason, string areaStr, string areaWeak, string satisfac, string reasonSep, int form1, int form2,
                  int form3, int form4, int form5, int form6, int form7, int form8, int form9, int form10, int form11, int form12, int form13, int form14)
{
    int sumForm = form1 + form2 + form3 + form4 + form5 + form6 + form7 + form8 + form9 + form10 + form10 + form11 + form12 + form13 + form14;

    // SQL Command para llamar el stored procedure
    SqlCommand comando = new SqlCommand("dbo.[Referencia_Insert]", base.Db);

    // Title
    SqlParameter spUser = new SqlParameter("@userId", System.Data.SqlDbType.Int);
    spUser.Value = userId;
    comando.Parameters.Add(spUser);

    SqlParameter spRef = new SqlParameter("@refNum", System.Data.SqlDbType.Int);
    spRef.Value = refNum;
    comando.Parameters.Add(spRef);

    ....

    SqlParameter spSumForm = new SqlParameter("@sumForm", System.Data.SqlDbType.Int);
    spSumForm.Value = sumForm;
    comando.Parameters.Add(spSumForm);

    HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('checa esto: "+base.ExecuteScalar(comando)+"')</SCRIPT>");

    // Ejecuta la consulta
    return base.ExecuteScalar(comando);
}

警报刚刚返回0.没有错误,没有任何错误。

这是存储过程:

ALTER PROCEDURE [dbo].[Referencia_Insert]
    @userID int, 
    @refNum int, 
    @name varchar(MAX), 
    @position varchar(MAX), 
    @lastPlace varchar(MAX), 
    @duration varchar(MAX), 
    @currentEmp int, 
    @rehire int, 
    @reason varchar(MAX), 
    @areaStr varchar(MAX), 
    @areaWeak varchar(MAX), 
    @satis varchar(MAX), 
    @reasonSep varchar(MAX), 
    @f1 int, @f2 int, @f3 int, @f4 int, @f5 int, @f6 int, 
    @f7 int, @f8 int, @f9 int, @f10 int, @f11 int, @f12 int, 
    @f13 int, @f14 int, 
    @sumForm int
WITH EXEC AS CALLER
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO [JobApplication].[dbo].[tbl_references] 
           ([userId], [refNum],[name], [position], [lastPlace], [duration],
            [currentEmp], [reHire], [reason], [areaStr], [areaWeak],
            [satisfac], [reasonSep],  
            [form1], [form2], [form3], [form4], [form5], [form6], [form7],
            [form8], [form9], [form10], [form11], [form12], [form13], [form14], [totalForm])
    VALUES (@userId, @refNum, @name, @position, @lastPlace, @duration,
            @currentEmp, @rehire, @reason, @areaStr, @areaWeak,
            @satis, @reasonSep,
            @f1, @f2, @f3, @f4, @f5, @f6, @f7,
            @f8, @f9, @f10, @f11, @f12, @f13, @f14, @sumForm)

    SELECT @@IDENTITY              
END

出于某种原因,它只会在ExecuteScalar中返回0。为什么?我不明白,因为每次我尝试显示值时,都会显示出来。我在本节中称它为一些任意值。

它应该在插入任何内容时返回1,但我只得到0。

protected void btnSend_Click(object sender, EventArgs e)
{
    ASF.HC.JobApplication.BO.Referencia refo = new ASF.HC.JobApplication.BO.Referencia();
    refo.Insert(370,1,"pruebini","tQM","la vida","la vida",1,1,"se fue","fuerza","debil","eu","holini",1,2,3,4,5,6,7,8,9,10,11,12,13,14);
}

EB。

1 个答案:

答案 0 :(得分:2)

您缺少命令类型

SqlCommand comando = new SqlCommand("dbo.[Referencia_Insert]", base.Db);
comando.CommandType = CommandType.StoredProcedure

这样,ADO.NET将执行此查询,然后您可以从SELECT @@IDENTITY

获取值
exec [dbo].[Referencia_Insert] ......

另一方面,当未指定命令类型时,ADO.NET将执行动态查询,如

exec sp_executesql N'[dbo].[Referencia_Insert]', N'.............'