我的论文有问题。它不断显示消息"位置0没有行。"。即使我的MySql数据库中有数据。请帮忙,我是新手。谢谢!
Private Sub frmAccountEdit_Load(sender As Object, e As EventArgs) Handles MyBase.Load
txtUserID.Text = frmAccountList.AccountCaller
da = New MySqlDataAdapter("SELECT * FROM tbl_account WHERE userid = '" & txtUserID.Text & "'", con)
ds.Reset()
da.Fill(ds)
Try
cmbAccType.SelectedIndex = ds.Tables(0).Rows(0)(1).ToString
txtUsername.Text = ds.Tables(0).Rows(0)(2).ToString
txtPassword.Text = ds.Tables(0).Rows(0)(3).ToString
txtFirst.Text = ds.Tables(0).Rows(0)(4).ToString
txtLast.Text = ds.Tables(0).Rows(0)(5).ToString
Catch ex As Exception
MessageBox.Show(ex.Message, "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
答案 0 :(得分:0)
运行mysql workbench并在那里尝试查询以使其工作。您将从查询中获得更多信息。如果没有第0行,则查询不返回任何内容。在尝试访问位置0之前查找行计数。
我参加了一个我一直用的课。您也不希望使用类似的文本框对查询进行硬编码。您可能会受到SQL注入的攻击。使用像这样的东西
sqlquery = "SELECT * FROM tbl_account WHERE userid=@coluid"
然后使用我班级的addparam函数添加文本框
dim xDB as new mysql
xdb.addparam("@coluid", txtuserid.text)
xdb.execquery(sqlquery)
if xdb.rows.count>0 then 'you have a record
Imports MySql.Data.MySqlClient
Public Class mysql
'Connection string for mysql
Public SQLSource As String = "Server=1.1.1.1;userid=xuser;password=xpassword;"
'database connection classes
Private DBcon As New MySqlConnection
Private SQLcmd As MySqlCommand
Public DBDA As New MySqlDataAdapter
Public DBDT As New DataTable
' parameters
Public Params As New List(Of MySqlParameter)
' some stats
Public RecordCount As Integer
Public Exception As String
Sub ExecQuery(SQLQuery As String)
DBcon.ConnectionString = SQLSource
Try
DBcon.Open()
SQLcmd = New MySqlCommand(SQLQuery, DBcon)
'loads params into the query
Params.ForEach(Sub(p) SQLcmd.Parameters.AddWithValue(p.ParameterName, p.Value))
'or like this is also good
'For Each p As MySqlParameter In Params
' SQLcmd.Parameters.AddWithValue(p.ParameterName, p.Value)
' Next
' clears params
Params.Clear()
DBDA.SelectCommand = SQLcmd
DBDA.Update(DBDT)
DBDA.Fill(DBDT)
DBcon.Close()
Catch ex As MySqlException
Exception = ex.Message
Finally
DBcon.Dispose()
End Try
End Sub
' add parameters to the list
Public Sub AddParam(Name As String, Value As Object)
Dim NewParam As New MySqlParameter(Name, Value)
Params.Add(NewParam)
End Sub
End Class