Message posté par : Christophe Vergon (christophe.vergon(a)protonmail.com)
----------------------------------------
Bonjour,
Je vous donne ci-dessous une partie de mon code.
Vous trouverez les instructions SQL pour vérifier l'existence d'une BD, d'un
schéma, d'une table, et l'on peut faire de même avec les colonnes, index, pk, fk
......
Si en plus vous respectez une convention de nommage, vous pouvez faire du traitement
automatique.
Exemple pour toute table :
pk = "id_" & nomtable
gix_nomtable = index spatial
idx_nomtable = index btree
-----------------
Code :
Protected Function Database_Exist() As Boolean
m_cmd = New Npgsql.NpgsqlCommand
m_cmd.CommandText = "SELECT COUNT(*) FROM pg_catalog.pg_database WHERE datname =
:database_name"
m_cmd.Connection = mPostGisCnn
m_cmd.Parameters.Add(New Npgsql.NpgsqlParameter(":database_name",
mNomBase))
Return m_cmd.ExecuteScalar > 0
m_cmd.Dispose()
End Function
Protected Function schema_exists(schemaname As String) As Boolean
Dim ds As New DataSet
m_cmd = New NpgsqlCommand
m_cmd.Connection = mPostGisCnn
m_cmd.CommandText = "SELECT schema_name FROM information_schema.schemata WHERE
schema_name='" & schemaname & "';"
m_da = New NpgsqlDataAdapter
m_da.SelectCommand = m_cmd
m_da.Fill(ds)
m_da.Dispose()
m_cmd.Dispose()
If ds.Tables(0).Rows.Count > 0 Then
Return True
Else
Return False
End If
End Function
Protected Sub Create_schema(schemaname As String)
If schema_exists(schemaname) Then
Exit Sub
End If
If schemaname <> "public" Then
m_cmd = New NpgsqlCommand
m_cmd.Connection = mPostGisCnn
m_cmd.CommandText = "CREATE SCHEMA " & schemaname &
";"
m_cmd.ExecuteNonQuery()
End If
End Sub
Protected Function table_exists(nomschema As String, nomtable As String) As Boolean
Dim ds As New DataSet
m_cmd = New NpgsqlCommand
m_cmd.Connection = mPostGisCnn
m_cmd.CommandText = "select table_name from information_schema.tables WHERE
table_schema=:p AND table_name=:p1;"
Dim p As New NpgsqlParameter("p", nomschema)
m_cmd.Parameters.Add(p)
Dim p1 As New NpgsqlParameter("p1", nomtable)
m_cmd.Parameters.Add(p1)
m_da = New NpgsqlDataAdapter
m_da.SelectCommand = m_cmd
m_da.Fill(ds)
m_da.Dispose()
m_cmd.Dispose()
If ds.Tables(0).Rows.Count > 0 Then
Return True
Else
Return False
End If
End Function
-----------------
----------------------------------------
Le message est situé
https://georezo.net/forum/viewtopic.php?pid=365125#p365125
Pour y répondre : geobd(a)ml.georezo.net ou reply de votre messagerie
Pour vous désabonner connectez-vous sur le forum puis Profil / Abonnement
--
Association GeoRezo - le portail géomatique
https://georezo.net