我很难通过传入参数来执行存储过程。我已经用网络上的不同示例更改了代码,但我一直收到以下错误,所以我一定是错了。
*Conversion failed when converting the varchar value 'Error' to data type int*
作业号从txt_JobNumber.Text传递到v_jobNumber,然后传递给存储过程。然后,存储过程执行查找和插入。存储过程在T-SQL“EXEC Z_Add_LotNumber' IU9FU-0000'”
中自行执行。下面是一些针对VB,存储过程和堆栈跟踪的代码。有人可以指出我做错了什么。
我使用的是Visual Studio 2013专业版和SQL Server 2008 R2
Private Sub btn_Run_Click(sender As Object, e As EventArgs) Handles btn_Run.Click
Dim response As MsgBoxResult
v_jobNumber = txt_JobNumber.Text 'IUBG1-0000 >>> Value of txt_JobNumber.Text
lbl_JobNumber.Text = v_jobNumber
'======================================================================================================
Dim conn As SqlConnection
Dim com As SqlCommand
Dim connectionString As String = "Data Source=[hostname];uid=[user];pwd=[password];database=[database]"
conn = New SqlConnection(connectionString)
com = New SqlCommand()
com.Connection = conn
com.CommandType = CommandType.StoredProcedure
com.CommandText = "Z_Add_LotNumber"
com.Parameters.AddWithValue("@fjobno", v_jobNumber) 'IUBG1-0000 >>> Value of v_jobNumber
conn.Open()
com.ExecuteNonQuery() ‘<<<<< Error >>>>> [Conversion failed when converting the varchar value 'Error' to data type int.]
conn.Close()
'======================================================================================================
response = MsgBox("Job Number: " & v_jobNumber)
End Sub
ALTER PROCEDURE [dbo].[Z_Add_LotNumber]
@fjobno varchar(50)
AS
DECLARE @V_Date smalldatetime;
DECLARE @V_fpartno varchar(25);
SET @V_Date = GETDATE();
SET @V_fpartno = (SELECT fpartno FROM joitem WHERE fjobno = @fjobno);
BEGIN
.
.
.
END
INSERT INTO Z_LotNumber(
JobNumber
PartNumber
CreateDate
.
.
.
)Values(
@fjobno
@V_fpartno
@V_Date
.
.
.
)
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteScalar()
at AutoGenLotNumber.Form1.btn_Run_Click(Object sender, EventArgs e) in c:\users\xxxxx\documents\visual studio 2013\Projects\AutoGenLotNumber\AutoGenLotNumber\Form1.vb:line 32
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at AutoGenLotNumber.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
COLUMN_NAME, DATA_TYPE, CHAR_MAX_LENGTH, IS_NULLABLE
1 Id int NULL NO
2 JobNumber char 15 YES
3 PartNumber char 25 YES
4 PartRev char 3 YES
5 PartQuantity numeric NULL YES
6 RecordType char 1 YES
7 LotNumber char 20 YES
8 CreateDate smalldatetime NULL YES
9 PartDescription varchar -1 YES
10 HIBIC_Number varchar 22 YES
11 ExpirationDate varchar 7 YES
12 CIFF_Reference varchar 300 YES
13 Auxiliary_CIFF_Reference varchar 300 YES
答案 0 :(得分:0)
将参数定义更改为:
com.Parameters.AddWithValue("@fjobno", v_jobNumber)
或者我的首选方式:
com.Parameters.Add(new SqlParameter("@fjobno", v_jobNumber))
编辑: 您不必将单引号连接到参数值。
编辑2:
conn.Open()
com.ExecuteNonQuery() //Not ExecuteScalar()
conn.Close()