ho provato un po' ma non trovo problemi :master:
vedi se può servire.
codice:
Option Strict On
Partial Class prove_a
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim vista As String = "REPORT_5"
Dim con As OleDbConnection = Nothing
Try
'creo nuova connessione
con = New OleDbConnection(StringaConnessione)
con.Open() 'la apro
'se la query non esiste la creo
If Not ExistReport(con, vista) Then
Dim command As OleDbCommand = con.CreateCommand()
command.CommandText = "create view " & vista & " as select distinct id_comune, nome_comune from comuni where codice_istat_regione = '20'"
command.ExecuteNonQuery()
End If
'popolo il GridView
'la procedura utilizza un OleDbDataReader
libreria.DataBind(Me.GridView1, con, "SELECT * FROM REPORT_5", Nothing)
Catch ex As Exception
'se errore stampo messaggio a video
PrintLn(ex.Message)
'svuoto la griglia
Me.GridView1.DataSource = Nothing
Me.GridView1.DataBind()
Finally
'sempre e comunque chiudo la connessione
If con IsNot Nothing Then con.Close()
End Try
End Sub
'restituisco una tabella con le query
Private Function GetTables(ByVal conn As OleDbConnection) As DataTable
Return conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "VIEW"})
End Function
'dà vero o falso se la query esiste già
Private Function ExistReport(ByVal con As OleDbConnection, ByVal report As String) As Boolean
Dim dt As DataTable = GetTables(con)
For i As Integer = 0 To dt.Rows.Count - 1
If dt.Rows(i)("table_name").ToString() = report Then
Return True
End If
Next
Return False
End Function
End Class