.net—linq vb.net提供的标量函数sql

uqdfh47h  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(393)

我在sql中有一个检查用户名和密码是否正确的函数,返回1或0

CREATE Function [dbo].[checklogin](@user varchar(30),@pass varchar(30))
Returns  BIT  
AS 
BEGIN  
DECLARE @check as bit
if (SELECT count(*) from dbo.Users where dbo.USERS.user_name= @user and  dbo.USERS.user_password=@pass)=1
set @check = 1
else
set @check=0
Return @check
END

在没有sql的情况下调用此函数时,linq返回实际信息

Sub loginCheck(ByVal username As String, ByVal password As String, ByVal frm As Form)

        Using con As New SqlConnection(inform.connectionstring)//
            con.Open()
            Using cmd As New SqlCommand("select dbo.checklogin('Manager','123');", con)
                Dim a As Boolean = CBool(cmd.ExecuteScalar)
                If a Then
                    MsgBox("Success")
                Else
                    MsgBox("Failed")
                End If
            End Using

        End Using
    End Sub

但是当我需要使用linq时,总是返回false

Sub loginCheck(ByVal username As String, ByVal password As String, ByVal frm As Form)

        Using con As New SqlConnection(inform.connectionstring)
            con.Open()
            Dim dta As New DataAj
            Dim ab As Boolean = dta.checklogin(username, password).Value
            If ab Then
                MsgBox("Success")
            Else
                MsgBox("Failed")
            End If

        End Using
    End Sub

0md85ypi

0md85ypi1#

这个简单的函数可以用来从一个字段和一个记录查询结果中获取值

Public Function getDataScalar(ssql As String)
    openConnection()

    Try
        Dim q As New MySqlCommand

        q.Connection = db
        q.CommandText = ssql
        getDataScalar = q.ExecuteScalar

    Catch ex As Exception
        'Exception
    End Try
End Function

如何使用:

Dim userid as String = getDataScalar("select username from user where userid=99")

变量'username'将由该查询的结果字段username的值填充。

相关问题