mysql - Vb和MySQL连接和代码错误

标签 mysql vb.net visual-studio-2012

大家好,我又卡住了,当表单加载时,我收到错误:“连接错误:位置 0 处没有行”求助!?

 Private Sub frmadduser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim sqlDT As New DataTable
    txtlname.Text = ""
    txtfname.Text = ""
    txtaddress.Text = ""
    txtcontact.Text = ""
    Try
        If Me.Text = "Edit Account" Then
            sqlSTR = "SELECT * FROM users WHERE idUsers =" & FrmSYSUSER.lstusers.FocusedItem.Text
            ExecuteSQLQuery(sqlSTR)
            MsgBox(sqlDT.Rows(0)("Username"))
            With sqlDT
                If .Rows.Count > 0 Then
                    txtlname.Text = .Rows(0)("lname")
                    txtfname.Text = .Rows(0)("fname")
                    txtaddress.Text = .Rows(0)("address")
                    txtcontact.Text = .Rows(0)("contact")
                    txtusername.Text = .Rows(0)("username")
                    txtpassword.Text = .Rows(0)("password")
                    txtconfirm.Text = .Rows(0)("password")
                    cmbaccnttype.Text = .Rows(0)("accesstype")
                End If
            End With
        End If
    Catch ex As Exception
        MsgBox("Connection Error :" & ex.Message, MsgBoxStyle.Information, xTitlename)
    End Try
End Sub

最佳答案

如果您ExecuteSQLQuery(sqlSTR)返回一个DataTable,您可能会这样做:

  sqlDT = ExecuteSQLQuery(sqlSTR)

并在对其执行任何操作之前检查是否有行,例如:

 if (sqlDT.Rows.Count>0) Then
    With sqlDT
         txtlname.Text = .Rows(0)("lname")
         txtfname.Text = .Rows(0)("fname")
         txtaddress.Text = .Rows(0)("address")
         txtcontact.Text = .Rows(0)("contact")
         txtusername.Text = .Rows(0)("username")
         txtpassword.Text = .Rows(0)("password")
         txtconfirm.Text = .Rows(0)("password")
         cmbaccnttype.Text = .Rows(0)("accesstype")
    End With
 End If

关于mysql - Vb和MySQL连接和代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20113500/

相关文章:

MySQL:唯一字段需要是索引吗?

mysql - DataGrip/PhpStorm 未连接到 MySQL DB,错误 : com. intellij.execution.ExecutionException

git - 将分支从 Team Foundation Service pull 到 Visual Studio 2012

php - UTF-8贯穿始终

.net - 保护 app.config - VB.Net 3.5

c# - 为移动设备 (.NET) 在页面中排序框架( block )

.net - 如何获取.NET分配的IP地址和端口号

vb.net - Visual Studio 2013 每次都进行不必要的构建并且速度很慢

visual-studio - Visual Studio 负载测试查看原始结果

php - 没有从数据库中得到结果