Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 23rd, 2007, 3:11 AM   #1
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 154
Rep Power: 3 programmingnoob is on a distinguished road
i'm trying to use c# with mysql

what if I want to insert things in a mysql table using data from a c# array?
it's a lot easier if i am just trying to hardcode values into the table, for example,
command.CommandText = "insert profilet (id, firstn, lastn) values (1, 2, 3)";
would work fine...
but the line below would not quite work...
command.CommandText = "insert profilet (id, firstn, lastn) values (array[0], array[2], array[3])";
please let me know if my question doesnt make sense...
any suggestions, please?
programmingnoob is offline   Reply With Quote
Old Jun 23rd, 2007, 8:06 AM   #2
Samuaijack
Programmer
 
Samuaijack's Avatar
 
Join Date: Jul 2006
Location: using Earth.Africa.Egypt.Cairo;
Posts: 73
Rep Power: 3 Samuaijack is on a distinguished road
command.CommandText = "insert into profilet (id, firstn, lastn) values (array[0], array[2], array[3])";
Samuaijack is offline   Reply With Quote
Old Jun 23rd, 2007, 10:55 AM   #3
teishu
Programmer
 
Join Date: May 2006
Posts: 51
Rep Power: 3 teishu is on a distinguished road
you need to use quotations, that will just insert the words array[0] etc..

command.CommandText = "insert profilet (id, firstn, lastn) values ("array[0]", "array[2]", "array[3])"";
__________________
AMD Athlon X2 4200+ -- Asus V3-M2V890 -- 2GB Kingston -- Vista Ultimate 32bit + Ubuntu 8.04
Intel C2D T5870 2.0GHZ -- Vostro 1510 -- 2048MB -- Windows XP SP2

ASCII stupid question, get a stupid ANSI !
teishu is offline   Reply With Quote
Old Jun 23rd, 2007, 1:11 PM   #4
john Wesley
Hobbyist Programmer
 
john Wesley's Avatar
 
Join Date: May 2006
Location: United Kingdom
Posts: 119
Rep Power: 3 john Wesley is on a distinguished road
Send a message via MSN to john Wesley Send a message via Yahoo to john Wesley
Samuraijack's will insert those words as you say however your command string should not compile at all!

"INSER INTO profilet (id, firstn, lastn) VALUES (" + array[0] + "," + array[1] + "," + array[2] + ")";

The above command string should do what you want it to programmingnoob, considering you have an initialised array of objects named 'array' and those numbers are valid index identifiers.
__________________
Mona Lisa must of had the highway blues you can tell by the way she smiles..
john Wesley is offline   Reply With Quote
Old Jun 23rd, 2007, 9:47 PM   #5
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 154
Rep Power: 3 programmingnoob is on a distinguished road
Quote:
Originally Posted by teishu View Post
you need to use quotations, that will just insert the words array[0] etc..

command.CommandText = "insert profilet (id, firstn, lastn) values ("array[0]", "array[2]", "array[3])"";
but thats not what I want to do....
I want to insert the value of array[0] and so on...

for example, if array[0] = 11, array[2] = 15...
I would want 11 and 15 inserted into the table... not the words array[0] etc
programmingnoob is offline   Reply With Quote
Old Jun 24th, 2007, 12:26 AM   #6
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 381
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
as john Wesley said :
INSER INTO profilet (id, firstn, lastn) VALUES ('" + array[0] + "','" + array[1] + "','" + array[2] + "')";

I just added the ' .. i remember it's necessary for inserting strings.

On the other hand, take a look at MySqlParameter.(aka google_it)
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Jun 24th, 2007, 2:58 AM   #7
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
I suggest using parameterized queries instead of simple concatenation to aviod sql injection attacks.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Jun 24th, 2007, 11:09 AM   #8
teishu
Programmer
 
Join Date: May 2006
Posts: 51
Rep Power: 3 teishu is on a distinguished road
Quote:
Originally Posted by programmingnoob View Post
but thats not what I want to do....
I want to insert the value of array[0] and so on...

for example, if array[0] = 11, array[2] = 15...
I would want 11 and 15 inserted into the table... not the words array[0] etc
precisely, yours would have inserted the words 'array[0]' etc..

look at the pairs of quotations more carefully and you will understand..

Thanks john wesley for completing my code, i knew it was close, not done it for a while lol..
__________________
AMD Athlon X2 4200+ -- Asus V3-M2V890 -- 2GB Kingston -- Vista Ultimate 32bit + Ubuntu 8.04
Intel C2D T5870 2.0GHZ -- Vostro 1510 -- 2048MB -- Windows XP SP2

ASCII stupid question, get a stupid ANSI !
teishu is offline   Reply With Quote
Old Jun 27th, 2007, 3:02 AM   #9
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 154
Rep Power: 3 programmingnoob is on a distinguished road
Quote:
Originally Posted by InfoGeek View Post
I suggest using parameterized queries instead of simple concatenation to aviod sql injection attacks.
umm elaborate please?!
I'm new to this stuff you see
if you could point me to a weblink, then that would be great too
programmingnoob is offline   Reply With Quote
Old Jun 27th, 2007, 8:24 AM   #10
free-zombie
Programmer
 
free-zombie's Avatar
 
Join Date: May 2006
Location: Bavaria, Germany
Posts: 50
Rep Power: 0 free-zombie is an unknown quantity at this point
Send a message via ICQ to free-zombie Send a message via MSN to free-zombie Send a message via Yahoo to free-zombie
I don't know how it's done in C# with whatever MySQL API you're using, but this is the same thing in python:

cursor.execute('insert into profilet (id, firstn, lastn) values (%s, %s, %s)', array[:3])

the point is that the SQL library takes care of putting the values into the query with correct quoting as to avoid danger if someone sets array[2] to something like Butcher"); update profilet set ( firstn = "idiot (you get the general idea)
free-zombie is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Connecting MySQL and PHP titaniumdecoy PHP 10 Feb 25th, 2008 7:47 PM
Use PEAR DB or MySQL ssrun PHP 6 Apr 27th, 2007 9:38 AM
MySQl simple problem paulchwd Other Web Development Languages 7 Feb 27th, 2007 10:31 AM
MySql paulchwd Other Web Development Languages 8 Feb 8th, 2007 9:17 PM
Tutorial - Using MySQL in C# Darkhack C# 12 Jan 17th, 2006 9:28 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:21 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC