IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

Problème de requête sql INSERT INTO


Sujet :

VB.NET

  1. #1
    Membre du Club
    Inscrit en
    Décembre 2006
    Messages
    158
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 158
    Points : 56
    Points
    56
    Par défaut Problème de requête sql INSERT INTO
    Bonjour tous le monde,
    je developpe une petite application depuis 15 jours. Le but est d'aller récupérer des informations dans l'active directory, de les mettre dans une base de données SQL Server et de les afficher dans un DataGrid.

    Le problème se situe au niveau de ma requête INSERT INTO, lorsque je veux insérer les valeurs dans ma DB j'ai une erreur du type :

    Compiler Error Message: BC30205: End of statement expected.

    Source Error:



    Line 78:
    Line 79:
    --> Line 80: sqlString = " Insert Into Servers VALUES ("DirEntry.Properties("Name").value&","&a.ToString()&","&DateTime.FromFileTime(fileTime)&","&DirEntry.Properties("operatingSystemServicePack").value&","&DirEntry.Properties("operatingSystemVersion").value&","&DirEntry.Properties("operatingSystem").value&" ) "
    Line 81:
    Line 82:

    personnellement je ne vois pas ce qu'il manque, peut être une erreur de syntaxe ? Si quelqu'un peut m'en dire un peux plus ça sera très gentil de votre part.
    Bien à vous.
    Ps : voici le code pour voir un peux plus claire.
    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
     
    <%@ Page Language="VB" %>
    <%@ Register TagPrefix="wmx" Namespace="Microsoft.Matrix.Framework.Web.UI" Assembly="Microsoft.Matrix.Framework, Version=0.6.0.0, Culture=neutral, PublicKeyToken=6f763c9966660626" %>
    <%@ import Namespace="System.Data" %>
    <%@ import Namespace="System.Data.SqlClient" %>
    <%@ import Namespace="System.Web.Mail" %>
    <%@ import Namespace="System.IO" %>
    <%@ import Namespace="System.DirectoryServices" %>
    <%@ import Namespace="System.Configuration" %>
    <%@ import Namespace="System.Net" %>
    <%@ import Namespace="System" %>
    <%@ import Namespace="System.Net.DNS" %>
    <%@ import Namespace="System.ComponentModel" %>
    <%@ import Namespace="System.Management" %>
    <%@ import Namespace="System.Runtime.InteropServices" %>
    <%@ import Namespace="System" %>
    <%@ import Namespace="ActiveDs" %>
    <script runat="server">
     
        ' Insert page code here
             '
     
             Sub Button1_Click(sender As Object, e As EventArgs)
     
     
             End Sub
     
              Sub Page_Load()
     
             AfficherListeClasses()
     
             End Sub
     
             Sub AfficherListeClasses()
     
              'Try
     
                     'response.write("Button1_Click")
     
                     'lblError.Text = "abc" + 12
                     ' Définition des objets
                     Dim oConnection As SqlConnection
                     Dim oCommand As SqlCommand
                     Dim oDataReader As SqlDataReader
                     Dim sSQL As String
                     Dim Ldap As DirectoryEntry = New DirectoryEntry("LDAP://tamac.local", "faratbi", "faratbi")
                     Dim searcher As DirectorySearcher = New DirectorySearcher(Ldap)
                     searcher.Filter = "(objectClass=computer)"
                     Dim DirEntry As DirectoryEntry
                     Dim largeInteger As Object
                     Dim lastLoggedOnDate As Date
                     Dim sqlString As String
                      'Création de notre connexion
                      oConnection = New SqlConnection()
                     'oConnection.ConnectionString = "server=CPQSQL2;database=PSPIntranet;uid=PSPIntranet;pwd=PSPIntranet;Max Pool Size=100000"
                      'pour l'authentification Windows mettre le paramettre Trusted_Connection=yes
                       oConnection.ConnectionString = "Server=127.0.0.1;Database=pspintranet;Trusted_Connection=yes"
     
                  ' Ouverture de la connexion
                    oConnection.Open()
     
                     For Each result As SearchResult In searcher.FindAll
     
                     Dim IPHost As IPHostEntry = Dns.Resolve(DirEntry.Properties("Name").value)
                     Dim addressList As IPAddress() = IPHost.AddressList
                     Dim a As IPAddress = addressList(0) ' récupère la première adresse
                     largeInteger = DirEntry.Properties("lastLogOn").Value
                     lastLoggedOnDate = ConvertLargeIntToDate(largeInteger)
     
     
     
              'Private Function ConvertLargeIntToDate(ByVal largeInteger As Object) As Date
                 'Dim typ As Type = largeInteger.GetType()
                 'Dim highPart As Integer = CInt(typ.InvokeMember("HighPart", Reflection.BindingFlags.GetProperty, Nothing, largeInteger, Nothing))
                 'Dim lowPart As Integer = CInt(typ.InvokeMember("LowPart", Reflection.BindingFlags.GetProperty, Nothing, largeInteger, Nothing))
                 'Dim fileTime As Int64 = Convert.ToInt64(highPart * (2 ^ 32))
                 'Return (DateTime.FromFileTime(fileTime))
             'End Function
     
     
               sqlString = " Insert Into Servers VALUES ("DirEntry.Properties("Name").value&","&a.ToString()&","&DateTime.FromFileTime(fileTime)&","&DirEntry.Properties("operatingSystemServicePack").value&","&DirEntry.Properties("operatingSystemVersion").value&","&DirEntry.Properties("operatingSystem").value&" ) "
     
     
     
                 'Ecriture de la requête SQL, extraction des data
                  sSQL = "SELECT * FROM Servers"
                 'Création de l'objet SqlCommand, execution de la requête
                  oCommand = New SqlCommand(sSQL, oConnection)
     
                 'Lecture des données
                  oDataReader = oCommand.ExecuteReader()
     
                 'Paramétrage du DataGrid
                  DTG_Liste_Classes.DataSource = oDataReader
                  DTG_Liste_Classes.DataBind()
     
                  ' Fermeture du DataReader et de la connexion
                  oDataReader.Close()
                  oConnection.Close()
                  'End Try
                  Next
     
                  End Sub
     
                  Private Function ConvertLargeIntToDate(ByVal largeInteger As Object) As Date
                 Dim typ As Type = largeInteger.GetType()
                 Dim highPart As Integer = CInt(typ.InvokeMember("HighPart", Reflection.BindingFlags.GetProperty, Nothing, largeInteger, Nothing))
                 Dim lowPart As Integer = CInt(typ.InvokeMember("LowPart", Reflection.BindingFlags.GetProperty, Nothing, largeInteger, Nothing))
                 Dim fileTime As Int64 = Convert.ToInt64(highPart * (2 ^ 32))
                 Return (DateTime.FromFileTime(fileTime))
             End Function
     
     
                  'Catch Ex As Exception
                     'lblError.Text =Ex.tostring
                     'response.write(Ex.tostring)
     
                 'End Try
     
            'End Try
     
     
        'End Sub
     
    </script>
    <html>
    <head>
    </head>
    <body>
        <form runat="server">
            <p>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:DataGrid id="DTG_Liste_Classes" runat="server"></asp:DataGrid>
            </p>
            <p>
            </p>
            <p>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
            </p>
            <!-- Insert content here -->
        </form>
    </body>
    </html>

  2. #2
    Membre du Club
    Inscrit en
    Décembre 2006
    Messages
    158
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 158
    Points : 56
    Points
    56
    Par défaut
    Re bonjour,
    j'ai réussit à résoudre le bug de ce matin, mais là j'ai un autre problème.

    à chaque fois que je veux executer ma requête INSERT le compilateur me renvoit ce type d'erreur :
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:


    Line 93: 'oConnection.Open()
    Line 94: 'execution de la requête
    -->Line 95: oCommand.ExecuteNonQuery()//ligne qui pose problème
    Line 96:
    Line 97: 'fermeture de la connexion
    Pourtant cette objet est déclaré, mais rien n'y fait.

    Quelqu'un pourrait-il m'aider svp parceque là je bloque vraiment

    voici le nouveau 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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
     
    <%@ Page Language="VB" %>
    <%@ Register TagPrefix="wmx" Namespace="Microsoft.Matrix.Framework.Web.UI" Assembly="Microsoft.Matrix.Framework, Version=0.6.0.0, Culture=neutral, PublicKeyToken=6f763c9966660626" %>
    <%@ import Namespace="System.Data" %>
    <%@ import Namespace="System.Data.SqlClient" %>
    <%@ import Namespace="System.Web.Mail" %>
    <%@ import Namespace="System.IO" %>
    <%@ import Namespace="System.DirectoryServices" %>
    <%@ import Namespace="System.Configuration" %>
    <%@ import Namespace="System.Net" %>
    <%@ import Namespace="System" %>
    <%@ import Namespace="System.Net.DNS" %>
    <%@ import Namespace="System.ComponentModel" %>
    <%@ import Namespace="System.Management" %>
    <%@ import Namespace="System.Runtime.InteropServices" %>
    <%@ import Namespace="System" %>
    <%@ import Namespace="ActiveDs" %>
    <script runat="server">
     
        ' Insert page code here
             '
     
             Sub Button1_Click(sender As Object, e As EventArgs)
     
     
             End Sub
     
              Sub Page_Load()
     
             AfficherListeClasses()
     
             End Sub
     
             Sub AfficherListeClasses()
     
              'Try
     
                     'response.write("Button1_Click")
     
                     'lblError.Text = "abc" + 12
                     ' Définition des objets
                     Dim oConnection As SqlConnection
                     'Dim oCommand As SqlCommand
                     Dim oDataReader As SqlDataReader
                     Dim sSQL As String
                     Dim Ldap As DirectoryEntry = New DirectoryEntry("LDAP://tamac.local", "faratbi", "faratbi")
                     Dim searcher As DirectorySearcher = New DirectorySearcher(Ldap)
                     searcher.Filter = "(objectClass=computer)"
                     Dim DirEntry As DirectoryEntry
                     Dim largeInteger As Object
                     Dim lastLoggedOnDate As Date
                     Dim sqlString As String
                      'Création de notre connexion
                      'oConnection = New SqlConnection()
                     'oConnection.ConnectionString = "server=CPQSQL2;database=PSPIntranet;uid=PSPIntranet;pwd=PSPIntranet;Max Pool Size=100000"
                      'pour l'authentification Windows mettre le paramettre Trusted_Connection=yes
                       'oConnection.ConnectionString = "Server=127.0.0.1;Database=pspintranet;Trusted_Connection=yes"
     
                  ' Ouverture de la connexion
                    'oConnection.Open()
     
                     For Each result As SearchResult In searcher.FindAll
                     Dim oCommand As SqlCommand
     
                      DirEntry = result.GetDirectoryEntry
                      'Dim oCommand As SqlCommand
                      'Dim oConnection As SqlConnection
                      oConnection = New SqlConnection()
                      oConnection.ConnectionString = "Server=127.0.0.1;Database=pspintranet;Trusted_Connection=yes"
                      'oConnection.Open()
     
                     'Dim IPHost As IPHostEntry = Dns.Resolve(DirEntry.Properties("Name").value)
                     'Dim addressList As IPAddress() = IPHost.AddressList
                     'Dim a As IPAddress = addressList(0) ' récupère la première adresse
                     'largeInteger = DirEntry.Properties("lastLogOn").Value
                     'lastLoggedOnDate = ConvertLargeIntToDate(largeInteger)
     
     
     
              'Private Function ConvertLargeIntToDate(ByVal largeInteger As Object) As Date
                 'Dim typ As Type = largeInteger.GetType()
                 'Dim highPart As Integer = CInt(typ.InvokeMember("HighPart", Reflection.BindingFlags.GetProperty, Nothing, largeInteger, Nothing))
                 'Dim lowPart As Integer = CInt(typ.InvokeMember("LowPart", Reflection.BindingFlags.GetProperty, Nothing, largeInteger, Nothing))
                 'Dim fileTime As Int64 = Convert.ToInt64(highPart * (2 ^ 32))
                 'Return (DateTime.FromFileTime(fileTime))
             'End Function
     
              'ouvrir la connexion
                 oConnection.Open()
     
     
               sSQL = " Insert Into Servers(nom) VALUES ('" & DirEntry.Properties("Name").value & "')"
               'ouvrir la connexion
                 'oConnection.Open()
                 'execution de la requête
                 oCommand.ExecuteNonQuery()
     
                 'fermeture de la connexion
                   oConnection.Close()
     
               ' Création de l'objet SqlCommand
                  'oCommand = New SqlCommand(sSQL, oConnection)
     
     
     
     
                 'Ecriture de la requête SQL, extraction des data
                    sSQL = "SELECT * FROM Servers"
                 'Création de l'objet SqlCommand, execution de la requête
                   oCommand = New SqlCommand(sSQL, oConnection)
     
                 'Lecture des données
                   oDataReader = oCommand.ExecuteReader()
     
                 'Paramétrage du DataGrid
                  'DTG_Liste_Classes.DataSource = oDataReader
                  'DTG_Liste_Classes.DataBind()
     
                  ' Fermeture du DataReader et de la connexion
                     oDataReader.Close()
                     oConnection.Close()
                  'End Try
                  Next
     
                  'Dim oCommand As SqlCommand
                  'oConnection = New SqlConnection()
                  'oConnection.ConnectionString = "Server=127.0.0.1;Database=pspintranet;Trusted_Connection=yes"
                  'ouvrir la connexion
                    'oConnection.Open()
     
                  'Ecriture de la requête SQL, extraction des data
                  'sSQL = "SELECT * FROM Servers"
                 'Création de l'objet SqlCommand, execution de la requête
                  'oCommand = New SqlCommand(sSQL, oConnection)
     
                 'Lecture des données
                  'oDataReader = oCommand.ExecuteReader()
     
                 'Paramétrage du DataGrid
                  DTG_Liste_Classes.DataSource = oDataReader
                  DTG_Liste_Classes.DataBind()
     
                  ' Fermeture du DataReader et de la connexion
                  'oDataReader.Close()
                  'oConnection.Close()
     
                  End Sub
     
                  Private Function ConvertLargeIntToDate(ByVal largeInteger As Object) As Date
                 Dim typ As Type = largeInteger.GetType()
                 Dim highPart As Integer = CInt(typ.InvokeMember("HighPart", Reflection.BindingFlags.GetProperty, Nothing, largeInteger, Nothing))
                 Dim lowPart As Integer = CInt(typ.InvokeMember("LowPart", Reflection.BindingFlags.GetProperty, Nothing, largeInteger, Nothing))
                 Dim fileTime As Int64 = Convert.ToInt64(highPart * (2 ^ 32))
                 Return (DateTime.FromFileTime(fileTime))
             End Function
     
     
                  'Catch Ex As Exception
                     'lblError.Text =Ex.tostring
                     'response.write(Ex.tostring)
     
                 'End Try
     
            'End Try
     
     
        'End Sub
     
    </script>
    <html>
    <head>
    </head>
    <body>
        <form runat="server">
            <p>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:DataGrid id="DTG_Liste_Classes" runat="server"></asp:DataGrid>
            </p>
            <p>
            </p>
            <p>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
            </p>
            <!-- Insert content here -->
        </form>
    </body>
    </html>

  3. #3
    Modérateur
    Avatar de Sankasssss
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Novembre 2006
    Messages
    1 842
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Novembre 2006
    Messages : 1 842
    Points : 4 232
    Points
    4 232
    Par défaut
    tu déclares cette variable :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Dim oCommand As SqlCommand
    mais tu ne l'instancie nul part...

    exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Dim command As New SqlCommand(queryString, connection)
    regarde l'aide msdn pour sqlcommand

Discussions similaires

  1. Réponses: 31
    Dernier message: 29/04/2014, 09h40
  2. Requête SQL Insert Into
    Par Requiem11 dans le forum JDBC
    Réponses: 3
    Dernier message: 15/01/2010, 10h32
  3. Problème requête sql : INSERT INTO
    Par bobosh dans le forum VBA Access
    Réponses: 2
    Dernier message: 20/08/2008, 10h43
  4. Requête SQL Insert Into
    Par jjg65 dans le forum Requêtes et SQL.
    Réponses: 8
    Dernier message: 07/11/2007, 08h35
  5. [SQL] problème de requête sql d'insertion
    Par belakhdarbts10 dans le forum PHP & Base de données
    Réponses: 9
    Dernier message: 10/05/2007, 14h11

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo