VB .NET - Prelevare le relazioni dal database

martedì 04 ottobre 2005 - 18.51

Gho5t Profilo | Junior Member

ciao..
vorrei sapere come si può prelevare le relazioni esistenti nelle tabelle di un database solamente con la connessione a SQL Server.
Mi spiego meglio... vorrei poter creare un dataset tramite VB .NET solamente avendo una connessione a sql server e conoscendo il nome della tabella; questo dataset dovrebbe avere un dataTable che è uguale (anche le chiavi primarie) ad una tabella di SQL SERVER.....
il mio problema + grande è che non riesco a copiare le chiavi primarie...

sapete come fare?

Grazie a tutti quelli che ci proveranno.....

Cteniza Profilo | Guru

Per recuperare queste informazioni nulla di meglio che prenderle tramite il metodo GetOleDbSchemaTable
Esempio su tabelle:
Dim schemaTable As DataTable = cnaccess.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, _
New Object() {Nothing, Nothing, Nothing, "TABLE"})
Esempio su colonne (mytable contiene il nome della tabella)
Dim schemaColumn As DataTable = cnaccess.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, _
New Object() {Nothing, Nothing, mytable Nothing})
Con un datareader e
Dim dr = MyCommand.ExecuteReader(CommandBehavior.KeyInfo)
Dim schemaTable As DataTable = dr.GetSchemaTable()
E poi cicli sui campi della tabella, ogni campo ha delle proprietà che descrivono una colonna, IsKey ad esempio dice che fa parte della chiave primaria




Gho5t Profilo | Junior Member

esiste una cosa uguale anche per l'SQLconnection?

io dovrei farlo su SQL server, ho provato a vedere ma nn c'è il comando "GetOleDbSchemaTable" per sqlconnection.....
forse nn ho cercato bene......
Help me!!

UN UOMO NON MUORE PER QUALCOSA IN CUI NON CREDE

Gho5t Profilo | Junior Member

ho trovato come fare.........
ve lo posto qua almeno in un eventuale futuro che serve basterà guardarci.....

cn(System.Data.SqlClient.SqlConnection) è la connessione
nometab(Stringa) è il nome della tabella
a(array) è l'array dove ci sono tutte le PK(primary key)


Dim da As New System.Data.SqlClient.SqlDataAdapter("SELECT * FROM " & nometab, cn)
Dim dt As New DataTable
cn.Open()
da.FillSchema(dt, SchemaType.Source)
a = dt.PrimaryKey


alla fine basta scorrere l'array dall'inizio alla fine con un'array come questo:

a(array) è l'array di prima con le PK

Dim c As New DataColumn
Dim lung As Integer
Dim PK As String
For lung = 0 To a.Length - 1
c = a(lung)
PK = c.ColumnName
MessageBox.Show("La chiave primaria è: " & PK, "TABELLA", MessageBoxButtons.OK, MessageBoxIcon.Information)
Next


ciao

UN UOMO NON MUORE PER QUALCOSA IN CUI NON CREDE
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5