Meilleur auteur de réponses
Selection de données multi-table

Question
-
Réponses
-
Bonjour errata,
Alors là-dedans, il y a 2 choses.
D'une la requête.
De deux le code VB.
Concernant la requête :
SELECT Tab_alarme.alarme,Tab_equipement.nom_equipement FROM Tab_alarme NATURAL JOIN Tab_equipement
Petite vérification : si tu la tapes dans SQL Management Studio, elle marche ?
Concernant le code VB, essaye comme cela :Private Sub VORToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VORToolStripMenuItem.Click 'Set the connection string of the SqlConnection object to connect to the 'SQL Server database in which you created the sample table. objConnection.ConnectionString = strConn objConnection.Open() 'Initialize the SqlDataAdapter object by specifying a Select command 'that retrieves data from the sample table. objDataAdapter = New SqlDataAdapter(strSql, objConnection) ' Map Table to Contacts. objDataAdapter.TableMappings.Add("Table", "Your_Table") ' Fill the DataSet. objDataset = New DataSet() 'Populate the dataset by running the Fill method of the SqlDataAdapter. objDataAdapter.Fill(objDataset, "Your_Table") ' Bind the DataGrid control to the Contacts DataTable. dataGridView.SetDataBinding(objDataset, "Your_Table") 'Close the database connection. objConnection.Close() End Sub
J'espère que ca t'aidera.
Cordialement,
Thomas
Thomas Aimonetti - C# - Sharplog Engineering - http://www.sharplog.fr
- Proposé comme réponse Alex Petrescu mardi 12 janvier 2010 09:18
- Marqué comme réponse Alex Petrescu vendredi 15 janvier 2010 07:40
Toutes les réponses
-
Bonjour
Auriez vous plus de détails pour nous puissions vous répondre ?
Tel que la requête et le code que vous utilisez pour remplir la DataGridView.
Merci
Christian Robert - MVP SQL Server
Blog : http://blogs.codes-sources.com/christian/
Groupe des Utilisateurs Francophone de SQL Server : http://www.guss.fr -
Imports System Imports System.Data.SqlClient Public Class Form3 Private objConnection As SqlConnection Private objCommand As SqlCommand Private objDataAdapter As SqlDataAdapter Private objDataset As New DataSet Private objDataTable As New DataTable Private strSql As String Private strConn As String Private objCB As SqlCommandBuilder Private Sub VORToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VORToolStripMenuItem.Click strConn = "Server=SWEET-A9F3CD063\SQLEXPRESS;Database=SUPERVISION;Trusted_connection=True;" strSql = "SELECT Tab_alarme.alarme,Tab_equipement.nom_equipement FROM Tab_alarme NATURAL JOIN Tab_equipement" objConnection = New SqlConnection objConnection.ConnectionString = strConn objConnection.Open() objCommand = New SqlCommand(strSql) objDataAdapter = New SqlDataAdapter(objCommand) objDataTable.Clear() objCommand.Connection = objConnection objCB = New SqlCommandBuilder(objDataAdapter) objDataAdapter.Fill(objDataset, "Tab_alarme") objDataTable = objDataset.Tables("Tab_alarme") objDataAdapter.Update(objDataset, "Tab_alarme") DataGridView1.DataSource = objDataTable DataGridView1.Refresh() objConnection.Close() End Sub
Voici le code que j'ai eu à faire pour seulement deux tables mais ca ne marche pas. -
Bonjour errata,
Alors là-dedans, il y a 2 choses.
D'une la requête.
De deux le code VB.
Concernant la requête :
SELECT Tab_alarme.alarme,Tab_equipement.nom_equipement FROM Tab_alarme NATURAL JOIN Tab_equipement
Petite vérification : si tu la tapes dans SQL Management Studio, elle marche ?
Concernant le code VB, essaye comme cela :Private Sub VORToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VORToolStripMenuItem.Click 'Set the connection string of the SqlConnection object to connect to the 'SQL Server database in which you created the sample table. objConnection.ConnectionString = strConn objConnection.Open() 'Initialize the SqlDataAdapter object by specifying a Select command 'that retrieves data from the sample table. objDataAdapter = New SqlDataAdapter(strSql, objConnection) ' Map Table to Contacts. objDataAdapter.TableMappings.Add("Table", "Your_Table") ' Fill the DataSet. objDataset = New DataSet() 'Populate the dataset by running the Fill method of the SqlDataAdapter. objDataAdapter.Fill(objDataset, "Your_Table") ' Bind the DataGrid control to the Contacts DataTable. dataGridView.SetDataBinding(objDataset, "Your_Table") 'Close the database connection. objConnection.Close() End Sub
J'espère que ca t'aidera.
Cordialement,
Thomas
Thomas Aimonetti - C# - Sharplog Engineering - http://www.sharplog.fr
- Proposé comme réponse Alex Petrescu mardi 12 janvier 2010 09:18
- Marqué comme réponse Alex Petrescu vendredi 15 janvier 2010 07:40
-
-
-
Bonjour errata,
Oups ...
J'ai laissé trainer une ligne en trop, celle du TableMapping, elle n'a rien a faire là :)
En gros, tu crées ton SqlDataAdapter, et tu t'en sers pour remplir ton DataSet en lui passant un nom de table.
Si ce nom n'existe pas, la table est créée dans le DataSet.
D'où le nom Tab_Equipement_Alarme.
Une fois le Fill effectué, tu as une DataTable "Tab_Equipement_Alarme" dans ton DataSet, et tu n'as plus qu'a faire un Fill sur ta DataGridView.
Voilà le code :
Private Sub VORToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VORToolStripMenuItem.Click 'Set the connection string of the SqlConnection object to connect to the 'SQL Server database in which you created the sample table. objConnection.ConnectionString = strConn objConnection.Open() 'Initialize the SqlDataAdapter object by specifying a Select command 'that retrieves data from the sample table. objDataAdapter = New SqlDataAdapter(strSql, objConnection) ' Fill the DataSet. objDataset = New DataSet() 'Populate the dataset by running the Fill method of the SqlDataAdapter. objDataAdapter.Fill(objDataset, "Tab_Equipement_Alarme") ' Bind the DataGrid control to the Contacts DataTable. dataGridView.SetDataBinding(objDataset, "Tab_Equipement_Alarme") 'Close the database connection. objConnection.Close() End Sub
Encore désolé pour l'erreur,
Pense à enlever ma réponse précédente comme réponse, et à mettre celle là à la place si elle fonctionne.
Cordialement,
Thomas
Thomas Aimonetti - C# - Sharplog Engineering - http://www.sharplog.fr -
-
Bonjour errata,
Ca fonctionne ?
Concernant la table Tab_Equipement_Alarme ...
C'est ce que je t'ai mis au-dessus :
En gros, tu crées ton SqlDataAdapter, et tu t'en sers pour remplir ton DataSet en lui passant un nom de table.
Quand ton SqlDataAdapter remplit ton DataSet, il remplit la table que tu lui indiques en argument.
Si ce nom n'existe pas, la table est créée dans le DataSet.
D'où le nom Tab_Equipement_Alarme.
Une fois le Fill effectué, tu as une DataTable "Tab_Equipement_Alarme" dans ton DataSet, et tu n'as plus qu'a faire un Fill sur ta DataGridView.
Si cette table existe, les données sont ajoutées dans la table, si elle n'existe pas, le SqlDataAdapter la crée automatiquement dans le DataSet, et ensuite la remplit.
Ici, tu lui passes Tab_Equipement_Alarme en paramètre, comme elle n'existe pas dans ton DataSet, elle est créée.
Mais tu aurais très bien l'appeler "Toto", que cela ne changeait rien, il t'aurait juste fallu remplir ton datagridview à partir de la table Toto :)
Si jamais ce n'est pas encore très clair, voici le lien de la MSDN sur le Fill :
http://msdn.microsoft.com/en-us/library/y4b211hz.aspx
Cordialement,
Thomas
Thomas Aimonetti - C# - Sharplog Engineering - http://www.sharplog.fr -
-
Salut errata,
Bon, je vais finir par me résoudre à installer VB.Net sur mon VS.
On prend les mêmes et on recommence ...
Private Sub VORToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VORToolStripMenuItem.Click 'Set the connection string of the SqlConnection object to connect to the 'SQL Server database in which you created the sample table. objConnection.ConnectionString = strConn objConnection.Open() 'Initialize the SqlDataAdapter object by specifying a Select command 'that retrieves data from the sample table. objDataAdapter = New SqlDataAdapter(strSql, objConnection) ' Fill the DataSet. objDataset = New DataSet() 'Populate the dataset by running the Fill method of the SqlDataAdapter. objDataAdapter.Fill(objDataset, "Tab_Equipement_Alarme") ' Bind the DataGrid control to the Tab_Equipement_Alarme DataTable. Dim bsTabEquipementAlarme as New BindingSource(objDataset, "Tab_Equipement_Alarme") dataGridView.DataSource = bsTabEquipementAlarme 'Close the database connection. objConnection.Close() End Sub
Bon, allez, c'est la bonne cette fois :)
Cordialement,
Thomas
Thomas Aimonetti - C# - Sharplog Engineering - http://www.sharplog.fr