【问题标题】:vb.net dataset returning null valuevb.net 数据集返回空值
【发布时间】:2012-02-07 17:06:32
【问题描述】:

您好,我已经在 Google 上搜索过了……已经 2 天了……这个空的东西真的让我头疼! ...请有人告诉我我的代码有什么问题吗????

这里是:-

Public Sub checkemail()
    sqlcheckemail = "select Email from WEBUSER where Email='" + TBEmail.Text + "'"
    Dim dscheckemail As New DataSet
    Dim sqlCnn As New SqlConnection
    'Dim MYNULL As String
    sqlCnn = New SqlConnection(connStr)
    sqlCmd = New SqlCommand(sqlcheckemail, sqlCnn)
    sqlCnn.Open()
    da.SelectCommand = sqlCmd
    da.Fill(dscheckemail)
    'MYNULL = CheckDBNull(dscheckemail)
    'If Not (dscheckemail.Tables.Count > 0) AndAlso (dscheckemail.Tables(0).Rows.Count > 0) Then
    'If Not IsDBNull(dscheckemail) Then
    'If Not (dscheckemail Is Nothing) Then
    'If MYNULL = "NULL" Then
    If Not dscheckemail Is Nothing Then
        LabelGender.Text = "There is something"
        'MsgBox("Unable to register because the E-mail address has already registered as a user, Please register using different Email address or contact administrator")
        'Response.Redirect("~/Rnewuser.aspx")
    Else
        'MsgBox("u can register")
        LabelGender.Text = "NULL"
    End If

    sqlCmd.Dispose()
    sqlCnn.Close()
End Sub

如果你看到我评论过的那个是我已经用谷歌搜索过的那个...所以现在如果数据库中存在电子邮件地址..一切都很好..但是如果它不存在代码应该处理不满足第一个 if 语句的那个​​......但现在我得到的结果是“有一些东西”,即使数据库中不存在电子邮件......

请帮助我!!!!我以前做过这个空的事情..但是很久以前我没有保存代码..现在我正在为我自己的网站做这件事..n 它让我崩溃了

【问题讨论】:

    标签: vb.net null dataset


    【解决方案1】:

    数据集不是什么都不是,为什么要输入if,你应该检查里面的数据表。

    'is not nothing, but is safer this way
    If Not dscheckemail Is Nothing Then
    
        'Check there is a datatable inside the dataset and that it has rows
        If dscheckemail.Tables(0) Is Nothing OrElse dscheckemail.Tables(0).Rows.Count = 0 Then
            'is really empty
        Else
            'You have a dataTable with data.
        End If
    
    End If
    

    【讨论】:

      【解决方案2】:

      你让自己的生活有点太难了,你的代码对 sql 注入是开放的,你的资源不能保证被处理掉。幸运的是,这些都很容易纠正。

      以下代码使用 SQL Server EXISTS 语句,以便您可以轻松确定数据库中是否有任何记录与请求的电子邮件地址。如果有,则返回 1,否则返回 0,这意味着您不必测试 null。

      由于我们知道只会返回一个值,我们可以将命令的执行更改为使用 executescalar。

      代码还在命令中使用了一个参数来防止SQL注入。

      最后,我们将一次性对象包装在 using 语句中,以确保它们被正确处理。

      这里是修改后的代码:

      Public Sub checkemail()
          Using sqlCnn As New SqlConnection(connStr)
              Dim sqlcheckemail As String = "IF EXISTS(select 1 FROM WEBUSER WHERE Email=@Email) SELECT 1 ELSE SELECT 0"
              Using sqlCmd As New SqlCommand(sqlcheckemail, sqlCnn)
                  sqlCmd.Parameters.AddWithValue("@Email", TBEMail.Text)
                  sqlCnn.Open()
      
                  If CBool(sqlCmd.ExecuteScalar) Then
                      LabelGender.Text = "There is something"
                      'MsgBox("Unable to register because the E-mail address has already registered as a user, Please register using different Email address or contact administrator")
                      'Response.Redirect("~/Rnewuser.aspx")
                  Else
                      'MsgBox("u can register")
                      LabelGender.Text = "NULL"
                  End If
              End Using
              sqlCnn.Close()
          End Using
      End Sub
      

      【讨论】:

      • thanksgiving_tech...真的很感激.. 测试了它,效果很好!!!.. 从来不知道有功能 CBool​​.. 非常感谢 :D
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多