473,563 Members | 2,797 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 6723
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
5598
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
4184
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
1564
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...
3
1499
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
7482
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
3203
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
2262
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...
0
1645
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 --...
5
1506
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...
0
7665
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...
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
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. ...
0
8106
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...
1
7642
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...
0
3643
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...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
924
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...

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.