Bonjour,

j ' ai besoin d 'aide pour mettre le résultat d 'une requête SQL dans un objet DataGridView. Pour l' instant je n'arrive qu à afficher le résultat dans un fichier texte ou dans une comboBox ou à afficher dans une datagridView tous les champs d 'une table...

Voici mon code:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 
 
Imports System
Imports System.IO
Imports System.Windows.Forms
Imports System.Data.SqlClient
 
Public Class Form1
 
    Private Sub inbox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles inbox.TextChanged
 
        'Recherche Variables
        'Dim myDS = New DataSet
        'Dim myDT As New DataTable
        Dim input1 As String
        Dim ctc As String
        Dim cpy As String
        Dim search As String
        input1 = inbox.Text
 
            'requetes
            ctc = "SELECT p_ctc.CtcNamDsc, p_ctc.CtcPhnNum, p_ctc.CtcMailNum FROM p_ctc WHERE p_ctc.CtcNamDsc LIKE '%" & input1 & "%';"
            cpy = "SELECT p_ctc.CtcNamDsc, p_ctc.CtcPhnNum, p_ctc.CtcMailNum, p_cpy.CpyTrdNamDsc FROM p_ctc, p_cpyaddr, p_cpy WHERE p_ctc.CpyAddrInCde = p_cpyaddr.CpyAddrInCde AND p_cpyaddr.cpyincde = p_cpy.CpyInCde AND p_cpy.CpyTrdNamDsc LIKE '%" & input1 & "%';"
 
            'gestion des options
            If (Option1.Checked = True) Then
                search = ctc
            Else
                search = cpy
            End If
 
            'connection
            Dim connectString As String = "Data Source=NEPTUNE;Initial Catalog=absyss;Integrated Security=True"
            Dim connection As SqlConnection = New SqlConnection(connectString)
            connection.Open()
 
            'execution requete
            Dim commandText As String
            commandText = search
            Dim command As SqlCommand = New SqlCommand(commandText, connection)
            Dim reader As SqlDataReader = command.ExecuteReader()
 
            'remplir datagridview   
            'myDT.Rows.Clear()
            'myDS.Tables.Add(myDT)
            'DataGridView1.DataSource = myDS
 
            ' Call Read before accessing data.
            While reader.Read()
                'combo.Items.Add(reader.GetValue(0) + "      " + reader.GetValue(1) + "      " + reader.GetValue(2))
                Dim myresult = reader(0).ToString() + " " + reader(1).ToString() + " " + reader(2).ToString()
 
                'ecriture fichier
                Dim write As StreamWriter = New StreamWriter("c:\resultat.txt", True)
                write.Write(reader.GetValue(0) + "      " + reader.GetValue(1) + "      " + reader.GetValue(2))
                write.WriteLine(" ")
                write.Close()
            End While
 
            ' Call Close when done reading.
            reader.Close()
 
            'deconnection
            connection.Close()
 
        End If
    End Sub
End Class