Inserting data into a table using c#

  • Thread starter Thread starter JH
  • Start date Start date
J

JH

I have checked my return strings and all are correct. But when I want to
insert them into an Access database table it fails. However, if I change the
insert values just to simple strings it works. Yet I have all the
aryItemFields values as strings correctly. Any HINT>>>>PLEASE.


...(THIS PIECE OF CODE DOES NOT WORK.....). ...
aryItemFields[0] = fielInfo.fileName;

aryItemFields[1] = fielInfo.size.ToString();

aryItemFields[2] = fielInfo.creationTime.ToString();

aryItemFields[3] = fielInfo.lastAccessTime.ToString();

myOleCommand = new OleDbCommand("INSERT INTO mytable1
filename,fsize,fcreationTime,fAccessTime) ValuesaryItemFields[0]
,aryItemFields[1],aryItemFields[2],aryItemFields[3])", myConnection);





(HOWEVER THIS ONE WORKS FINE......)

myOleCommand = new OleDbCommand("INSERT INTO mytable1
filename,fsize,fcreationTime,fAccessTime) Values('a','b','c','d')",
myConnection);
 
Well, the key point is the presence of the single quotes in the statement
that works. Do the same on your real data, such as:

aryItemFields[1] = "'" + fielInfo.size.ToString() + "'";

for all fields that are defined as one of the string types in the column
definitions, and you'll be on your way.

Tom Dacon
Dacon Software Consulting
 
You need to use parameterized queries:

myOleCommand = new OleDbCommand("INSERT INTO mytable1
(filename,fsize,fcreationTime,fAccessTime) VALUES (?,?,?,?");
myOleCommand.Parameters.Add("@filename",OleDbType.Whatever).Value =
aryItemFields[0];
// etc.

--Bob
 
Back
Top