OVH Cloud OVH Cloud

Erreur OleDB

3 réponses
Avatar
Michael Bonfils
Bonjour à toutes & tous,

je suis en train de développer un service windows dans lequel j'utilise
une connexion OleDB.

L'architecture se présente de la manière suivante :

OnStart
p_Quit=false;
p_Thread=new Thread( MaFunction );

OnStop
p_Quit=true;
while( p_Thread.IsAlive() )
Thread.Sleep(100);

MaFunction
connexion à la base
tant que !p_Quit
Attente d'une connexion TCP
suivant le contenu de la connexion,
lecture, insertion ou mise à jour de la base
Fermeture de la base


Quand j'essaye de faire une insertion dans la base, je récupère l'erreur
à la fin du message.

au début je croyais que c'était l'ouverture de la base car elle avait
lieu dans le OnStart. Je n'arrive pas à trouver

Le code d'insertion :
OleDbCommand sql = new OleDbCommand();
sql.CommandText = "INSERT INTO client ( nom, info ) VALUES ('1','2')";
sql.Connection = p_DB;
sql.ExecuteNonQuery();

L'erreur :

Erreur : System.Data.OleDb.OleDbException: L'opération doit utiliser une
requête qui peut être mise à jour.
at
System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS
dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object&
executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior
behavior, Object& executeResult)
at
System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior
behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
at wsT2.srv_wsTS2.Fct() in
C:\Dev\Src\Perso\wsT2\wsT2\srv_TS2.cs:line 205

Pour plus d'informations, consultez le centre Aide et support à
l'adresse http://go.microsoft.com/fwlink/events.asp.

3 réponses

Avatar
Delf
Michael Bonfils wrote:

> Le code d'insertion :
> OleDbCommand sql = new OleDbCommand();
> sql.CommandText = "INSERT INTO client ( nom, info ) VALUES ('1','2')";
> sql.Connection = p_DB;
> sql.ExecuteNonQuery();

Essaie avec ceci :

OleDbConnection myConnection = new OleDbConnection(ConnectionString);
OleDbCommand myCommand = new OleDbCommand("INSERT ...", myConnection);

try
{
myConnection.Open();

myCommand.ExecuteNonQuery();
}
catch (Exception e)
{
...
}
finally
{
if (myConnection != null)
{
myConnection.Close();
myConnection = null;
}
}

--
Delf
Avatar
Michael Bonfils
Delf wrote:
Michael Bonfils wrote:

> Le code d'insertion :
> OleDbCommand sql = new OleDbCommand();
> sql.CommandText = "INSERT INTO client ( nom, info ) VALUES ('1','2')";
> sql.Connection = p_DB;
> sql.ExecuteNonQuery();

Essaie avec ceci :

OleDbConnection myConnection = new OleDbConnection(ConnectionString);
OleDbCommand myCommand = new OleDbCommand("INSERT ...", myConnection);

try
{
myConnection.Open();

myCommand.ExecuteNonQuery();
}
catch (Exception e)
{
...
}
finally
{
if (myConnection != null)
{
myConnection.Close();
myConnection = null;
}
}

--
Delf



Malheureusement, j'obtient la même erreur, il doit avoir une subtilité
du fait qu'il y ait un thread.

Au fait, j'ai oublié de préciser que j'utiliser le VS2005 beta 2.

Merci
Avatar
Paul Bacelar
"Michael Bonfils" wrote in message
news:42d3b412$0$4935$
Delf wrote:
> Michael Bonfils wrote:
>
> > Le code d'insertion :
> > OleDbCommand sql = new OleDbCommand();
> > sql.CommandText = "INSERT INTO client ( nom, info ) VALUES


('1','2')";
> > sql.Connection = p_DB;
> > sql.ExecuteNonQuery();
>
> Essaie avec ceci :
>
> OleDbConnection myConnection = new OleDbConnection(ConnectionString);
> OleDbCommand myCommand = new OleDbCommand("INSERT ...", myConnection);
>
> try
> {
> myConnection.Open();
>
> myCommand.ExecuteNonQuery();
> }
> catch (Exception e)
> {
> ...
> }
> finally
> {
> if (myConnection != null)
> {
> myConnection.Close();
> myConnection = null;
> }
> }
>
> --
> Delf

Malheureusement, j'obtient la même erreur, il doit avoir une subtilité
du fait qu'il y ait un thread.

Au fait, j'ai oublié de préciser que j'utiliser le VS2005 beta 2.

Merci



Je vous conseille les requêtes paramétrées car on contrôle plus strictement
les types et les mots réservés du SQL:

sql.CommandText = "INSERT INTO client ( nom, info ) VALUES (?,?)";
sql.Parameters.Add(....

--
Paul Bacelar