gpt4 book ai didi

asp.net - 未提供的参数化查询

转载 作者:行者123 更新时间:2023-12-02 09:47:37 24 4
gpt4 key购买 nike

我不断收到此错误:

The parameterized query '(@AdminEmail nvarchar(4000),@AdminPassword nvarchar(4000))SELECT' expects the parameter '@AdminEmail', which was not supplied.

代码:

Public Function AuthenticateAdmin() As Boolean
Dim Success As Boolean

Dim strConn As String
strConn = ConfigurationManager.ConnectionStrings("HMVDb").ToString
Dim conn As New SqlConnection(strConn.ToString())

Dim cmd As New SqlCommand("SELECT * FROM Admin WHERE AdminEmail=@AdminEmail AND Adminpassword=@Adminpassword", conn)
cmd.Parameters.AddWithValue("@AdminEmail", EMail)
cmd.Parameters.AddWithValue("@AdminPassword", Password)

Dim da As New SqlDataAdapter(cmd)

Dim ds As New DataSet

conn.Open()
da.Fill(ds, "Admin")
conn.Close()

If ds.Tables("Admin").Rows.Count > 0 Then

Dim aemail As String = ds.Tables("Admin").Rows(0).Item("AdminEmail")
Dim apass As String = ds.Tables("Admin").Rows(0).Item("AdminPassword")
Dim aid As Integer = ds.Tables("Admin").Rows(0).Item("AdminID")
Dim aname As String = ds.Tables("Admin").Rows(0).Item("AdminName")

If EMail = aemail And Password = apass Then
ID = aid ' Shopper ID that identify Ecader
Name = aname
Success = True 'Shopper is authenticated
Else
Success = False 'Authentication fail
End If
End If


'Return the authentication result to calling program
Return Success
End Function

最佳答案

您的@AdminEmail变量EMail为空。您不能在必需参数上传递 null。使用DBNull.Value

当使用null时,您将通知Sql Server您省略了该参数。这对于具有默认值的可选参数很有用,但对于必需参数会导致错误。

我建议您在将值传递给命令参数时始终使用实用程序函数。

例如:

public static object GetDataValue(object value)
{
if(value == null)
{
return DBNull.Value;
}

return value;
}

然后使用

cmd.Parameters.AddWithValue("@AdminEmail", GetDataValue(EMail))

关于asp.net - 未提供的参数化查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6778483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com