简体   繁体   English

存储过程不通过C#插入

[英]Stored Procedure not inserting via C#

My stored procedure works fine in my SQL EXEC query. 我的存储过程在我的SQL EXEC查询中正常工作。 It inserts into my DB but does not when I call it from C# though it does not throw an error. 它插入到我的数据库中,但是当我从C#调用它时它不会抛出错误。 What is happening. 怎么了。

My C# Code: 我的C#代码:

        con.Open();
        transaction = con.BeginTransaction();
        cmd = new SqlCommand("InsertIndent", con, transaction);
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add("@IP_No", SqlDbType.VarChar, 15);
        cmd.Parameters["@IP_No"].Value = indent.IP_No;

        cmd.Parameters.Add("@Stat", SqlDbType.VarChar, 1);
        cmd.Parameters["@Stat"].Value = indent.Status;

        cmd.Parameters.Add("@Reference_No", SqlDbType.VarChar, 15);
        cmd.Parameters["@Reference_No"].Value = indent.Reference_No;

        cmd.Parameters.Add("@Remarks", SqlDbType.VarChar, 30);
        cmd.Parameters["@Remarks"].Value = indent.Remarks;

        cmd.Parameters.Add("@Update_Time", SqlDbType.DateTime);
        cmd.Parameters["@Update_Time"].Value = indent.Update_Time;

        cmd.Parameters.Add("@User_Id", SqlDbType.VarChar, 15);
        cmd.Parameters["@User_Id"].Value = indent.User_Id;

        cmd.Parameters.Add("@Log_Id", SqlDbType.BigInt);
        cmd.Parameters["@Log_Id"].Value = indent.Log_Id;

        cmd.Parameters.Add("@Shift_Id", SqlDbType.Int);
        cmd.Parameters["@Shift_Id"].Value = indent.Shift_Id;

        result = cmd.ExecuteNonQuery().ToString();
        log.Debug("connection.cs : inside addNewIndent,for IpNo " + indent.IP_No);
        con.Close();

My values while Tracing 追踪时我的价值观 价值

My Stored Procedure: 我的存储过程:

[InsertIndent]
(
@IP_No varchar(15),        
@Stat varchar(1),      
@Reference_No varchar(15),
@Remarks varchar(30),   
@Update_Time DATETIME,
@User_Id varchar(15),
@Log_Id bigint,
@Shift_Id int
)
AS
BEGIN
DECLARE @Indent_No varchar(20)
set @Indent_No= (SELECT TOP 1 In_Id from Id_Generation WITH(NOLOCK))

INSERT INTO IP_Indent(IP_No,Indent_No,Status,Reference_No,Remarks,User_Id,Log_Id,Shift_Id,Update_Time)
VALUES(@IP_No,@Indent_No,@Stat,@Reference_No,@Remarks,@User_Id,@Log_Id,@Shift_Id,@Update_Time)

Update Id_Generation set In_Id=In_Id+1;

END

Since you've started a transaction, you'll also need to commit it before you close the connection, otherwise the change will be rolled back. 由于您已启动事务,因此您还需要在关闭连接之前提交它 ,否则将回滚更改。

On another note, why not use an IDENTITY for that Id_Generation counter column? 另请注意,为什么不为该Id_Generation计数器列使用IDENTITY? As it stands, if 2 concurrent callers call your proc, they could both wind up with the same value of Id_Generation . 就目前而言,如果两个并发呼叫者呼叫你的proc,他们都可以使用相同的Id_Generation值。

Also, note that SqlConnection and SqlCommand are both Disposable - eg wrapping both in a using statement will ensure that resources are released ASAP. 另请注意, SqlConnectionSqlCommand都是Disposable - 例如,将两者都包装在using语句中将确保资源尽快释放。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM