Solved

Could someone please check out my OleDbCommand problem?

Posted on 2004-09-02
2
277 Views
Last Modified: 2006-11-17
Hello,

I'm having this strange new problem writing to an Access database.  I made a simple test program which executes a simple SQL query, but I'm getting back the exception: "Syntax error in INSERT INTO statement."  And I can't figure out why.  Here's the code...

OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
      @"Data source=" + "D:\\DataArchive\\test.mdb";

string sql;
try
{
    conn.Open();
    OleDbCommand dbSelectCommand = new OleDbCommand();
    dbSelectCommand.Connection = conn;
    sql = "INSERT INTO Events (Data) VALUES (22)";
    dbSelectCommand.CommandText = sql;                              
    OleDbDataReader reader = dbSelectCommand.ExecuteReader(); // xxxx            
    reader.Close();
}
catch (Exception ex)
{
}

I made a new Access database ("Test.mdb") and table ("Events") containing a singgle column ("Data") of type "Number".  

The previous code compiles and executes, but once it hits ExecuteReader() it throws the exception: "Syntax error in INSERT INTO statement."  Clearly there's something else going on besies the SQL statement, since the SQL is well formed.  Does anyone have any ideas of what I can try?  I'd be really appreciative since it's got me stopped cold.


Thanks,
Bob
0
Comment
Question by:ba272
2 Comments
 
LVL 9

Accepted Solution

by:
Rodney Helsens earned 500 total points
ID: 11968573
I'm not 100% sure, but i don't think you want to user executeReader... you are not reading, but inserting, so there will not be a resultset returned from this... use ExecuteNonQuery() instead, and see if it fixes your problems.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataoledboledbcommandclassexecutescalartopic.asp


OleDbConnection myConnection = new OleDbConnection(myConnectionString);
   OleDbCommand myCommand = new OleDbCommand(myExecuteQuery, myConnection);
   myCommand.Connection.Open();
   myCommand.ExecuteNonQuery();
   myConnection.Close();

0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 11970778
Just change ExecuteReader with ExecuteScalar or ExecuteNonQuery
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question