Bonjour à tous,

J'essaye de remplir une table en fonction de différentes données mais j'ai un soucis qui me bloque depuis un petit moment.

En effet dans ma table a remplir j'ai une colonne qui est en mode auto increment (donc IDENTITY) cependant impossible d'effectuer une requete :

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
 Public Sub fonction_sauvegarde_validation_formulaire(ByVal nom_table)
        Dim mycommand As SqlCommand
        Dim strSQl_insert As String
 
 
        'strSQl_insert_formulaire = Maconnexion.CreateCommand()
        'strSQl_insert_formulaire.CommandText = "INSERT INTO dbo.Table_historique VALUES('','" & nom_table & "','" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "')"
 
        strSQl_insert = "SET IDENTITY_INSERT dbo.Table_historique ON; INSERT INTO dbo.Table_historique VALUES ( '1', @TYPE, @Date, @Utilisateur, @Commentaires); SET IDENTITY_INSERT dbo.Table_historique OFF"
        mycommand = New SqlCommand(strSQl_insert, Fonctions_CONNEXION.Maconnexion)
 
        With mycommand.Parameters
            '.Add(New SqlParameter("@IDHISTO", SqlDbType.Int, 32))
            .Add(New SqlParameter("@TYPE", SqlDbType.NVarChar, 50))
            .Add(New SqlParameter("@Date", SqlDbType.SmallDateTime, 32))
            .Add(New SqlParameter("@Utilisateur", SqlDbType.NVarChar, 50))
            .Add(New SqlParameter("@Commentaires", SqlDbType.NVarChar, 50))
        End With
 
        With mycommand
 
            '.Parameters("@IDHISTO").Value = 1
            .Parameters("@TYPE").Value = nom_table
            .Parameters("@Date").Value = CDate(TextBox1.Text)
            .Parameters("@Utilisateur").Value = TextBox2.Text
            .Parameters("@Commentaires").Value = TextBox3.Text
        End With
 
 
        compteur_histo += 1
        Try
            'Connection_SQL()
            mycommand.Connection.Open()
            mycommand.ExecuteNonQuery()
            Deconnection_SQL()
 
        Catch e As System.Data.SqlClient.SqlException
            MsgBox(e.Message)
        End Try
 
    End Sub
Le message d'erreur est :
An explicit value for the identity column in table 'db.table_historique' can only be specified when a column list is used and IDENTITY_INSERT is ON
Merci de vos futurs conseils !