首页 > 解决方案 > 为什么.NET 两次添加 SP 参数?

问题描述

我有一个 winforms 应用程序,当我插入一行时我正在调用一个 SP。我正在定义参数,但它在最后添加了额外的参数。

da.InsertCommand = new SqlCommand("dbo.GridInsert", new SqlConnection(MC.ConnectionString));
da.InsertCommand.CommandType = CommandType.StoredProcedure;
da.InsertCommand.Parameters.Add("@ApplyTo", SqlDbType.VarChar, 25).Value = applyTo;
da.InsertCommand.Parameters.Add("@ApplyType", SqlDbType.VarChar, 50).Value = applyType;
da.InsertCommand.Parameters.Add("@ApplyValue", SqlDbType.VarChar, 50).Value = applyValue;
da.InsertCommand.Parameters.Add("@ID", SqlDbType.BigInt, 0, "ID");
da.InsertCommand.Parameters.Add("@InstanceID", SqlDbType.BigInt, 0, "InstanceID");

上面,第一个 3 参数从变量中获取它们的值。接下来的 2 个与网格列相关联。但是,这是发送到数据库的内容。

exec dbo.GridInsert @ApplyTo='xxx',@ApplyType='xxx',@ApplyValue='xxx',@ID=NULL,@InstanceID=38,@ApplyTo='xxx',@ApplyType='xxx',@ApplyValue='xxx' 

请注意,第一个 3 参数放在前面和末尾。另外,如果我不将这 3 个参数添加到集合中,它们无论如何都会放在最后。有人有任何线索吗?

标签: c#stored-proceduresparameter-passing

解决方案


推荐阅读