2012-10-25 55 views
0

我想使用此代碼顯示在何處的信息根據先前標記場DataGrid中使用MySQL

SQL = "SELECT * FROM funcionario WHERE setor ='" + TxtPesq.Text + "'" 
       myCommand.Connection = conn 
       myCommand.CommandText = SQL 
       myAdapter.SelectCommand = myCommand 
       myAdapter.Fill(myData) 
       DataGridView1.DataSource = myData 
+0

恰好是不工作怎麼辦?請提供更多細節。 – Arrow

+0

嗨,你可以檢查數據是否在mydata對象...? – jainvikram444

回答

0
  • 使用using語句對象的妥善處置研究電網。
  • 參數化查詢以避免SQL注入

全碼

Dim dSet As New Dataset 
Dim SQL AS String= "SELECT * FROM funcionario WHERE setor = @setor" 
Using conn As New MySqlConnection("connectionStr HERE") 
    Using myCommand As New MysqlCommand() 
     With MyCommand 
      .Connection = conn 
      .CommandText = SQL 
      .CommandType = CommandType.Text 
      .Parameters.AddWithValue("@setor", TxtPesq.Text) 
     End With 
     Using myAdapter As New MySQLDataAdapter(myCommand) 
      Try 
       myAdapter.Fill(dset) 
       DataGridView1.DataSource = dset.tables(0) 
      Catch(ex as MySQLException) 
       Msgbox (ex.Messge) 
      End Try 
     End Using 
    ENd Using 
ENd Using