473,729 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# / SQL Related - what's wrong with this Insert Into syntax?

Hello all,

Strange little problem here... am just trying to insert some basic
information into an Access Database using OleDB.

I'm getting a "Syntax error in Insert Into statement" when it tries to
execute the SQL. The strange thing is if i take the exact SQL being
executed from the debugger and insert and execute it using the MS
Access query engine, it works fine!

What you need to know is regarding the C# Data Types:

Strings:
MovieTitle,Movi eDirector,Movie Actors,MoviePlo t,fileLocation, contentStr
Ints: MovieYear,Movie Rating,MovieRun time

What you need to know is regarding the Access Data Types:

Text: Title, Director, fileLocation
Memo: Actors, Plot, picLocation (due to being possibly larger than 255
chars)
Number: Year, Runtime, Rating

string strDSN = "Provider=Micro soft.Jet.OLEDB. 4.0;Data
Source=mediaInf o.MDB";
string strSQL = "INSERT INTO MovieInfo ([Title], [Year], [Director],
[Actors], [Plot], [Runtime], [Rating], [fileLocation], [picLocation]) "
+ "VALUES '" + CleanSQL(MovieT itle) + "',"
+ MovieYear + ",'"
+ CleanSQL(MovieD irector) + "','"
+ CleanSQL(MovieA ctors) + "','"
+ CleanSQL(MovieP lot) + "',"
+ MovieRuntime + ","
+ MovieRating + ",'"
+ CleanSQL(fileLo cation) + "','"
+ CleanSQL(conten tStr) + "')";

OleDbConnection myConn = new OleDbConnection (strDSN);
OleDbCommand myCmd = new OleDbCommand( strSQL, myConn );

try
{
myConn.Open();
myCmd.ExecuteNo nQuery();
}

Oh, and the CleanSQL method is just replacing all instances of
apostrophe's in the parameters with double apostrophe's to prevent
confusion with the SQL.

Also the Access DB is stored in the Debug folder of my project so no
path is necessary to it.

Any suggestions as to where i'm going wrong?

Regards,

Brian

Feb 16 '06 #1
3 6734
Apologies.. should correct this:

"VALUES "

to

"VALUES ('"

Feb 16 '06 #2
br************@ gmail.com wrote in
news:11******** **************@ g43g2000cwa.goo glegroups.com:
Hello all,

Strange little problem here... am just trying to insert some
basic information into an Access Database using OleDB.

I'm getting a "Syntax error in Insert Into statement" when it
tries to execute the SQL. The strange thing is if i take the
exact SQL being executed from the debugger and insert and
execute it using the MS Access query engine, it works fine!

What you need to know is regarding the C# Data Types:

Strings:
MovieTitle,Movi eDirector,Movie Actors,MoviePlo t,fileLocation, conte
ntStr Ints: MovieYear,Movie Rating,MovieRun time

What you need to know is regarding the Access Data Types:

Text: Title, Director, fileLocation
Memo: Actors, Plot, picLocation (due to being possibly larger
than 255 chars)
Number: Year, Runtime, Rating

string strDSN = "Provider=Micro soft.Jet.OLEDB. 4.0;Data
Source=mediaInf o.MDB";
string strSQL = "INSERT INTO MovieInfo ([Title], [Year],
[Director], [Actors], [Plot], [Runtime], [Rating],
[fileLocation], [picLocation]) " + "VALUES '" +
CleanSQL(MovieT itle) + "'," + MovieYear + ",'"
+ CleanSQL(MovieD irector) + "','"
+ CleanSQL(MovieA ctors) + "','"
+ CleanSQL(MovieP lot) + "',"
+ MovieRuntime + ","
+ MovieRating + ",'"
+ CleanSQL(fileLo cation) + "','"
+ CleanSQL(conten tStr) + "')";

OleDbConnection myConn = new OleDbConnection (strDSN);
OleDbCommand myCmd = new OleDbCommand( strSQL, myConn );

try
{
myConn.Open();
myCmd.ExecuteNo nQuery();
}

Oh, and the CleanSQL method is just replacing all instances of
apostrophe's in the parameters with double apostrophe's to
prevent confusion with the SQL.

Also the Access DB is stored in the Debug folder of my project
so no path is necessary to it.

Any suggestions as to where i'm going wrong?


Brian,

Nothing of a syntax nature immediately jumps out.

Using the Visual Studio debugger, examine the value of strSQL after
it's been assigned. Copy that value into a query window in Access
and execute it. Access might give you more info as to what's wrong
w/ the statement.

I would also suggest using parameters instead of dynamically
building a string. OleDbParameter does a better job of ensuring the
parameter value gets formatted and inserted correctly than
do homegrown methods like CleanSQL. Parameters also prevent most
kinds of SQL injection attacks.

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Feb 16 '06 #3
Chris R. Timmons wrote:
Oh, and the CleanSQL method is just replacing all instances of
apostrophe' s in the parameters with double apostrophe's to
prevent confusion with the SQL.


I recommend rewriting this to use OleDbParameter' s. It's a bit more
typing (code), but it becomes more robust and you don't have to recreate
the (admittedly small) wheel.

<aside>
I remember I was using the same Clean() function for MySQL for a while,
but then a strange problem cropped up: I was getting errors INSERTing
into MySQL. Turned out I was trying to insert \0, which Mysql doesn't like.

I switched to MySqlParameter' s and then looked at their implementation
of Clean() --turned out I was missing a bunch of stuff and was doing it
slower than the implementation version.
</aside>
Scott
Feb 16 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5614
by: Simom Thorpe | last post by:
Hi, I'm trying to insert a line into a MS access DB using ASP on IIS 5. This is the line: con.execute "INSERT INTO newProds(title,desc,catcode) VALUES ('Champagne Muff Scarf','','AC304B')" But it throws up this error:
6
4196
by: Oli | last post by:
Hi What's wrong with this? sql="insert into tblNumbers (Number) values ('" & i & "')" I get... Error Type: Microsoft JET Database Engine (0x80040E14) Syntax error in INSERT INTO statement.
2
1571
by: Uri Lazar | last post by:
hi, im working on this for a long time. i'm using MSsql-server2000 i have a table that records users visits to rooms. the columns are room_id, user_id, visits. i want to write a query that can calculate the top 10 rooms that are related to any given room. i was thinking of firstly making a function that counts how many users visited both room A and room B, and then running this function on A and all other rooms, and order by the result. i...
3
1509
by: Matik | last post by:
Hello all, I belive, my problem is probably very easy to solve, but still, I cannot find solution: declare @i int declare @z int create table bubusilala (
2
7495
by: Fons Roelandt | last post by:
Heelo, I have to Update all fields from a table with the values of a related table, i've tried some querys i found on the internet, but nothing seems to word, i even tried to lookup the value using dlookup, but even that doesnt seem to word in a update query. The query that i think should work is this one: UPDATE tblOrderLines AS tblO
10
3233
by: Protoman | last post by:
Could you tell me what's wrong with this program, it doesn't compile: #include <iostream> #include <cstdlib> using namespace std; class Everything { public: static Everything* Instance()
5
2273
by: strawberry | last post by:
In the function below, I'd like to extend the scope of the $table variable such that, once assigned it would become available to other parts of the function. I thought 'global $table;' would solve this but it's clear that I'm misunderstanding $variable persistence. I posted a similar enquiry over at alt.php.mysql, but I guess this is a more appropriate forum because the problems I'm having relate to PHP. Any help appreciated. ...
0
1669
by: Gawn | last post by:
Dear all, Greeding from Thailand. Need help for my news script. I am trying to display news which keywords match the current page keywords. I am using Dreamweaver 8 and PhpMyAdmin to manage MySQL. May I explain you what I have done. My database and data in that look like this. //* Data Base -- phpMyAdmin SQL Dump -- version 2.6.2-pl1 -- http://www.phpmyadmin.net --
5
1513
by: spudpeel | last post by:
Hi, I have to insert 10 pieces of data (8 from text or combo boxes, and 2 from variables), into my table. I'm sure Im getting the quotes wrong somewhere, but whatever I try I just cant see anything wrong. I'd really appreciate it If someone could help me with this, Its driving me crazy! The Code is below: DoCmd.RunSQL "INSERT INTO tblDocs (,,,,,,,,,) Values (" '" & Me.combo18 & "','" & Me. & "','" & Me.Description & "','" & Me.combo16 &...
0
8930
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9428
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9291
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9218
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4799
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3244
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2171
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.