Inserting data into a table....

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

JH

I have tried both suggested options shown below to insert simple string
values to an Access table, but to no avail. The executeNonQuery failed
miserably. I got the correct string values from each array items and were
able to check them using messagebox.show. BUT YET i AM UNABLE TO LOG THEM
INTO A TABLE.

Please I need help here


////// OPTION 1

//


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)" +

"Values(aryItemFields[0],aryItemFields[1],aryItemFields[2],aryItemFields[3])
", myConnection);



myOleCommand.ExecuteNonQuery();


//OPTION 2

//

aryItemFields[0] = fielInfo.fileName.ToString();

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) VALUES (?,?,?,?");

myOleCommand.Parameters.Add("@filename",OleDbType.VarChar).Value =
aryItemFields[0];

myOleCommand.Parameters.Add("@fsize",OleDbType.VarChar).Value =
aryItemFields[1];

myOleCommand.Parameters.Add("@fcreationTime",OleDbType.VarChar).Value =
aryItemFields[2];

myOleCommand.Parameters.Add("@faccesstime",OleDbType.VarChar).Value =
aryItemFields[3];





myOleCommand.ExecuteNonQuery();
 
Hi,

How it fails?
Do you get an exception, if so what is the text of it.

I think that the second variant is the best, in the first you would have to
deal with controls chars ( ' , "," ) whether using parameters you eliminate
that.

Cheers,
 
I got it ..........ignore my question. If you use the code below it works
fine.


string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\WmiTestdb.mdb";


string strSQL = "INSERT INTO mytable1 (Name,Address) VALUES(?,?)";

OleDbConnection conn = new OleDbConnection(strConn);

conn.Open();


OleDbCommand cmd = new OleDbCommand();


cmd = new OleDbCommand(strSQL,conn );


cmd.Parameters.Add("@Name", OleDbType.Char, 50).Value = aryItemFields[0];

cmd.Parameters.Add("@Address", OleDbType.Char, 50).Value = aryItemFields[1];

cmd.ExecuteNonQuery();
 
Back
Top