Posted By

cirio on 03/28/10


Tagged

DatasetDataAdapterDataView


Versions (?)

FIlter DataSet


 / Published in: Visual Basic
 

URL: http://lineguides.netsons.org

RiempiDataGrid : Variabile per il riempimento o meno di un oggetto DataGridView SqlFilter : Variabile contenente i filtri nella forma "CAMPO='Valore' " . Accetta operatori AND/OR. Sql: Variabile contenente la query di base non filtrata.
  1. Public Function crea_dtset(ByVal sql As String, ByVal RiempiDataGrid As Boolean, ByVal SqlFilter As String) As DataSet
  2. Try
  3.  
  4. Dim objAdapter As New SqlClient.SqlDataAdapter()
  5. Dim objDataSet As DataSet
  6. Dim objConnection As New SqlClient.SqlConnection
  7. Dim objCommand As SqlClient.SqlCommand
  8. Dim objDataView As New DataView()
  9.  
  10.  
  11. 'Setto ed apro la connessione
  12. objConnection.ConnectionString = "Server=myServer\SQLEXPRESS;Database=mydb;User ID=myid;Password=mypwd;Trusted_Connection=False;"
  13. objConnection.Open()
  14.  
  15. If (objConnection.State = ConnectionState.Open) Then
  16.  
  17. 'Creo un nuovo oggetto command a cui viene passata la nostra stringa
  18. 'sql e i parametri di connessione
  19. objCommand = New SqlClient.SqlCommand(sql, objConnection)
  20. objCommand.CommandText = sql
  21. objCommand.CommandType = CommandType.Text
  22.  
  23.  
  24. 'Viene Riempito il DataSet
  25. objAdapter = New SqlClient.SqlDataAdapter(objCommand)
  26. objDataSet = New DataSet()
  27. objAdapter.Fill(objDataSet)
  28.  
  29. objConnection.Close()
  30.  
  31. crea_dtset = objDataSet
  32.  
  33. 'Filtro i miei dati
  34.  
  35. If RiempiDataGrid = True Then
  36. 'Riempio il dataview con l'intero contenuto della mia query
  37. objDataView = objDataSet.Tables(0).DefaultView
  38. objDataView.RowFilter = sqlFilter
  39.  
  40. 'Assegno come datasource l'oggetto objdataview(filtrato)
  41. frmMain.DGVDati.DataSource = objDataView
  42. End If
  43.  
  44. 'Svuoto gli oggetti per la prossima query
  45. objDataView = Nothing
  46. objAdapter = Nothing
  47. objCommand = Nothing
  48. objDataSet = Nothing
  49. objConnection = Nothing
  50. End If
  51.  
  52.  
  53.  
  54. Catch ex As Exception
  55.  
  56. MsgBox("Error!")
  57.  
  58. End Try
  59. End Function

Report this snippet  

You need to login to post a comment.