vb.net2010无法把参数传给SQL2008存储过程,显示无法转换数据类型
一个存储过程控制的登陆程序,ComboBox_username是用户列表,绑定数据源表中的字段
VB部分代码
Private Sub Button_login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_login.Click
Dim dbConnection = New SqlClient.SqlConnection("Data Source=192.168.0.134;Initial Catalog=crm;Persist Security Info=True;User ID=sa;Password=111111")
Dim cmdSQL = New SqlClient.SqlCommand("check_login", dbConnection)
cmdSQL.Parameters.Add("@user", SqlDbType.VarChar, 50).Value = Me.ComboBox_username.SelectedItem
cmdSQL.Parameters.Add("@pass", SqlDbType.VarChar, 50).Value = Me.TextBox_pwd.Text
cmdSQL.Parameters.Add("@zj", SqlDbType.Int, 50).Value = ParameterDirection.Input
dbConnection.Open()
cmdSQL.ExecuteNonQuery()
If "@zj" = 0 Then
MsgBox("返回假", vbYes)
Else
MsgBox("返回真", vbYes)
End If
dbConnection.Close()
End Sub
存储过程代码
USE [crm]
GO
/****** Object: StoredProcedure [dbo].[check_login] Script Date: 07/11/2012 02:13:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[check_login]
@user varchar(50),
@pass varchar(50)
AS
if exists (select * from user_info where (user_name=@user)and(user_pwd=@pass))
return 1
else
return 0
[解决办法]
先把 cmdSQL.Parameters.Add 部分的代码注释,然后把这句改为这样试试:
Dim strSQL AS String="Execute check_login N'"& Me.ComboBox_username.SelectedItem.ToString &"',N'"& Me.TextBox_pwd.Text &"'"
Dim cmdSQL = New SqlClient.SqlCommand(strSQL, dbConnection)