2015-10-20 82 views
0

這是我的代碼過濾數據轉換成文本框/標籤如何顯示消息,如果沒有記錄,發現

Dim reader As MySqlDataReader 
    Try 
     If conn.State = ConnectionState.Closed Then 
      conn.Open() 
     End If 

     Dim query As String 
     query = "SELECT * FROM member WHERE (memor = '" & memor.Text & "')" 

     sqlcom = New MySqlCommand(query, conn) 
     reader = sqlcom.ExecuteReader 
     While reader.Read() 
      Me.lblname.Text = reader("membname").ToString 
      Me.txtmembtype.Text = reader("membtype").ToString 
      Me.lblmembdate.Text = reader("membdate").ToString 
     End While 

     conn.Close() 
    Catch ex As Exception 
     MessageBox.Show(ex.Message) 
    Finally 
     conn.Dispose() 
    End Try 

現在我想有提示,有沒有在我的MySQL數據庫中找到數據的消息框?任何想法?

+3

'如果reader.HasRows().../ELSE MSGBOX /結束If' – Plutonix

+1

你應該看看'Using'塊 - 你的連接,命令對象和讀者都應該[當你完成它們時處置](http://stackoverflow.com/a/29187199/1070452)。 – Plutonix

回答

0

您可以簡單地使用reader.HasRows()檢查記錄

reader = sqlcom.ExecuteReader 
if Reader.HasRows() Then 
    While reader.Read() 
     Me.lblname.Text = reader("membname").ToString 
     Me.txtmembtype.Text = reader("membtype").ToString 
     Me.lblmembdate.Text = reader("membdate").ToString 
    End While 
Else 
    MessageBox.Show("No records found") 
End If 
0

毆打,但仍發佈了強制性SQL parameterization警告。

修改後的代碼:

Try 
    If conn.State = ConnectionState.Closed Then 
     conn.Open() 
    End If 


    Dim query As String 
    query = "SELECT * FROM member WHERE (memor = @memor)" 

    sqlcom = New MySqlCommand(query, conn) 
    sqlcom.Parameters.AddWithValue("@memor", memor.Text) 
    reader = sqlcom.ExecuteReader 
    If reader.HasRows Then 
     While reader.Read() 
      Me.lblname.Text = reader("membname").ToString 
      Me.txtmembtype.Text = reader("membtype").ToString 
      Me.lblmembdate.Text = reader("membdate").ToString 
     End While 
    Else 
     MsgBox("No data found.") 
    End If 

    conn.Close() 
Catch ex As Exception 
    MessageBox.Show(ex.Message) 
Finally 
    conn.Dispose() 

End Try 
相關問題