【发布时间】: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