【问题标题】:Stored procedure and VB.net code not working : Too many arguments存储过程和 VB.net 代码不起作用:参数太多
【发布时间】:2014-06-30 12:35:07
【问题描述】:

我有以下存储过程:

 ALTER PROCEDURE p_InsertNewBatch
(
@customer_id VARCHAR(50),
@batch_number INT,
@batch_reference VARCHAR(50),
@output_location VARCHAR(150),
@create_date VARCHAR(50),
@batchid INT OUTPUT
)
AS
BEGIN
SET NOCOUNT ON;
declare @date datetime
set @create_date= convert(VARCHAR, @create_date, 1) -- as dd/mm/yyyy

INSERT INTO tbl_batches
(
[customer_id],
[batch_number],
[batch_reference],
[batch_output_location],
[create_date]
)
VALUES
(
@customer_id,
@batch_number,
@batch_reference,
@output_location,
@create_date
)
SELECT @batchid = SCOPE_IDENTITY();
END

我从表单 OnCLick 事件中运行这个存储过程:

Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim intRowsAffected As Integer

'Try
Dim returnbatch As Integer = 0
Dim customerID As String
Dim batchref As String
Dim todaysdate As String = String.Format("{0:dd/MM/yyyy}", DateTime.Now)
Dim batchid As Integer
Dim myClientFolder As String
Dim myBatchFolder As String

con.ConnectionString = My.Settings.TestValue()
con.Open()
cmd.Connection = con
cmd.CommandText = "p_GetNextBatchNumber"
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@CustomerID", SqlDbType.Char)
cmd.Parameters("@CustomerID").Value = txtCustomerNumber.Text
cmd.Parameters.Add("@NextBatch", SqlDbType.Int).Direction = ParameterDirection.Output
cmd.ExecuteNonQuery()
con.Close()

returnbatch = Convert.ToInt32(cmd.Parameters("@NextBatch").Value)
batchref = txtCustomerNumber.Text & "-" & returnbatch
txtBatchNumber.Text = returnbatch
txtBatchReference.Text = batchref
txtCreateDate.Text = todaysdate
customerID = txtCustomerNumber.Text

myClientFolder = "c:\Scanned Batches1\" & txtCustomerNumber.Text
myBatchFolder = "c:\Scanned Batches1\" & txtCustomerNumber.Text & "\" & txtBatchReference.Text

If Directory.Exists(myClientFolder) Then
Else
    MsgBox("A new folder for this client will be created")
    Directory.CreateDirectory(myClientFolder)
End If
' Now check that the Batch ID folder exists
If Directory.Exists(myBatchFolder) Then
    MsgBox("A batch folder already exists with this name.  Are you sure you want to continue", vbInformation)
Else
    MsgBox("A new folder for this batch will be created", vbInformation)
    Directory.CreateDirectory(myBatchFolder)
End If

txtOutputLoc.Text = "c:\Scanned Batches1\" & txtCustomerNumber.Text & "\" & txtBatchReference.Text
txtOutputLoc.Enabled = False


'INSERT into tbl_Batches the Batch Details 

' @customer_id VARCHAR(50),
' @batch_number INT,
' @batch_reference VARCHAR(50),
' @output_location VARCHAR(150),
' @create_date datetime 


con.Open()
cmd.CommandText = "p_InsertNewBatch"
cmd.CommandType = CommandType.StoredProcedure

cmd.Parameters.Add("@customer_id", SqlDbType.Char).Value = customerID
cmd.Parameters.Add("@batch_number", SqlDbType.Int).Value = returnbatch
cmd.Parameters.Add("@batch_reference", SqlDbType.Char).Value = batchref
cmd.Parameters.Add("@output_location", SqlDbType.Char).Value = txtOutputLoc.Text
cmd.Parameters.Add("@create_date", SqlDbType.Char).Value = todaysdate
cmd.Parameters.Add("@batchid", SqlDbType.Int).Direction = ParameterDirection.Output
cmd.ExecuteNonQuery()
intRowsAffected = cmd.ExecuteNonQuery()


con.Close()
batchid = Convert.ToInt32(cmd.Parameters("@batchid").Value)
txtID.Text = batchid

'Catch ex As Exception
'MessageBox.Show("Error while trying to add new batch..." & ex.Message, "Create Batch")
' Finally
con.Close()
'End Try

End Sub

但是我不断收到错误:

System.Data.SqlClient.SqlException 未处理
过程或函数 p_InsertNewBatch 指定的参数过多。

这里发生了什么?多年来我一直在查看代码,但似乎看不出是什么原因造成的。

谢谢

【问题讨论】:

  • 检查你的参数值。
  • 旁注:到底为什么要将今天的日期转换为 string 以将其传递给数据库,从而引入格式问题 - 因为您正在使用参数,您可以将其保存为 VB 中的 DateTime 变量和数据库中的 datetime,并依靠 ADO.NET 在两者之间安全地进行转换。

标签: sql sql-server vb.net stored-procedures


【解决方案1】:

您正在使用单个命令对象来执行多个 SQL 语句,但在为第二个添加新参数之前,您没有清除第一个中的参数。我倾向于为每个 SQL 语句创建一个新的命令对象,但是,如果您要使用相同的命令对象并更改 CommandText,那么请确保您也清除参数。

【讨论】:

  • 你的代码中有多少东西有CommandText属性和参数?您的代码中有多少东西的名称中有“命令”?只有一个,所以这显然是命令对象。我的意思是,与其创建其中一个对象并多次使用它,不如为每次使用创建一个新对象。
猜你喜欢
  • 2016-07-28
  • 1970-01-01
  • 2019-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-06
  • 1970-01-01
  • 2019-07-29
相关资源
最近更新 更多