Sql 无法将参数值从文本框转换为Int32

Sql 无法将参数值从文本框转换为Int32,sql,vb.net,Sql,Vb.net,由于工作缓慢,我决定用Visual Basic进行实验和学习。我已经决定,最好开发一个应用程序,使用计算机名作为搜索条件来查询我的SCCM数据库中的Bitlocker密钥。如果运行vissqlstudio,我的SQL查询可以工作,但是我得到以下错误 我相信我的错误是两行粗体代码,我通过文本框的搜索条件是由用户键入的LAP-123AB456。用户键入computername后,单击按钮 错误:无法将参数值从文本框转换为Int32 任何帮助都将不胜感激 代码狙击手: Dim cmd As N

由于工作缓慢,我决定用Visual Basic进行实验和学习。我已经决定,最好开发一个应用程序,使用计算机名作为搜索条件来查询我的SCCM数据库中的Bitlocker密钥。如果运行vissqlstudio,我的SQL查询可以工作,但是我得到以下错误

我相信我的错误是两行粗体代码,我通过文本框的搜索条件是由用户键入的LAP-123AB456。用户键入computername后,单击按钮

错误:无法将参数值从文本框转换为Int32

任何帮助都将不胜感激

代码狙击手:

    Dim cmd As New SqlCommand("select a.Id, a.Name, b.VolumeId, c.RecoveryKeyId, c.RecoveryKey, c.LastUpdateTime, c.Disclosed from RecoveryAndHardwareCore_Machines a inner join RecoveryAndHardwareCore_Machines_Volumes b ON a.Id = b.MachineId inner join RecoveryAndHardwareCore_Keys c ON b.VolumeId = c.VolumeId where a.name = @entry", connection)
            
    **cmd.Parameters.Add("@entry", SqlDbType.Int).Value = CObj(TXT_ComputerName)**
    
    Dim adapter As New SqlDataAdapter(cmd)
    
    **Dim table As New DataTable()**
    
    adapter.Fill(table)
错误详细信息:

System.InvalidCastException
  HResult=0x80004002
  Message=Failed to convert parameter value from a TextBox to a Int32.
  Source=System.Data
  StackTrace:
   at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType, Boolean& coercedToDataFeed, Boolean& typeChanged, Boolean allowStreaming)
   at System.Data.SqlClient.SqlParameter.GetCoercedValue()
   at System.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean isCommandProc)
   at System.Data.SqlClient.SqlCommand.BuildParamList(TdsParser parser, SqlParameterCollection parameters, Boolean includeReturnValue)
   at System.Data.SqlClient.SqlCommand.BuildExecuteSql(CommandBehavior behavior, String commandText, SqlParameterCollection parameters, _SqlRPC& rpc)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
   at TervitaBitlockerLookup.FRM_BitlockerSearch.BTN_Search_Click(Object sender, EventArgs e) in C:\Users\admin\source\repos\BitlockerLookup\BitlockerLookup\BitlockerSearch.vb:line 12
   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 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at TervitaBitlockerLookup.My.MyApplication.Main(String[] Args) in :line 81

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
InvalidCastException: Object must implement IConvertible.

为什么要尝试绑定字符串值并显式声明参数类型为Int?
cmd.Parameters.Add(“@entry”,SqlDbType.Int)。value=CInt(TXT\u ComputerName.Text)
,假设
TXT\u ComputerName
是WinForms的文本框控件。顺便说一句,您应该事先
Integer.TryParse()。但是,这个文本框看起来并不是包含一个可以转换为整数的值:它看起来像是,从字段和文本框的名称(
a.name
TXT\u ComputerName
)来看,它是完全不同的东西,一个包含某个东西的名称的字符串。检查DB数据类型。感谢您的帮助,“a.name”指的是nvarchar(63)数据类型。数据库中的此字段包含计算机名(例如:LAP-AB123CD456)。我的希望是允许用户键入计算机名并对SQL查询结果进行subit。抱歉,按Enter键,它提交了回复。正如您所建议的,如果我理解正确,我将CObj更改为CInt,并且我有以下错误。错误:“从字符串“lap-pc12”转换为类型“Integer”无效。不确定Integer.TryParse()的含义或在代码中的位置。
a.name
属于
nvarchar
类型,因此必须提供字符串值,而不是整数,并指定
SqlDbType.nvarchar
。您可以(或者应该在代码中指定长度,具体到让其他开发人员看到确切的字段规格)为什么要尝试绑定字符串值并显式地将参数类型声明为Int?
cmd.Parameters.Add(“@entry”,SqlDbType.Int)。value=CInt(TXT\u ComputerName.Text)
,假设
TXT\u ComputerName
是WinForms的文本框控件。顺便说一句,您应该事先
Integer.TryParse()。但是,这个文本框看起来并不是包含一个可以转换为整数的值:它看起来像是,从字段和文本框的名称(
a.name
TXT\u ComputerName
)来看,它是完全不同的东西,一个包含某个东西的名称的字符串。检查DB数据类型。感谢您的帮助,“a.name”指的是nvarchar(63)数据类型。数据库中的此字段包含计算机名(例如:LAP-AB123CD456)。我的希望是允许用户键入计算机名并对SQL查询结果进行subit。抱歉,按Enter键,它提交了回复。正如您所建议的,如果我理解正确,我将CObj更改为CInt,并且我有以下错误。错误:“从字符串“lap-pc12”转换为类型“Integer”无效。不确定Integer.TryParse()的含义或在代码中的位置。
a.name
属于
nvarchar
类型,因此必须提供字符串值,而不是整数,并指定
SqlDbType.nvarchar
。您可以(或者应该在代码中也指定长度,具体到让其他开发人员看到确切的字段规格)