473,508 Members | 2,454 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use SQL statement with ADODB.Recordset in VC++

Hi everybody

I am a newcomer in VC++ developement
I work VC with Oracle database
I want to manipulate the database with ADODB
After connected , i do as follow:
------------------------
recordset->Open("INSERT INTO other_info(card_id) VALUES

(200)",connection.GetInterfacePtr(),adOpenForwardO nly,adLockReadOnly,adCmdText);
------------------------
It worked , but when i want to use the editbox or combobox to take the
values from end_users to put in
the SQL statements for updating or inserting --> big problem

recordset->Open("INSERT INTO other_info(card_id) VALUES
(m_editbox1) : --> error

In VB 6.0 it is allright ,How can we do it in VC++
Pls , help me to handle this error

Thanks in advance !!!!
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 17 '05 #1
1 4636
The short answer is that you'll need to build up the SQL string and then pass it to recordset->Open. The longer answer is that if you plan to give this code to others to use, you'll need to sanitize the input that you recieved from the user first to prevent serious security problems on the server, and to prevent users from hurting themselves accidentally. (Try googling "SQL injection attack" if you need to be convinced that this can be a very serious problem.) :-

So, the short answer: If you are new to C in general, manipulating strings can be a little more difficult than in most other languages, and there are a lot of tricky parts to handling that as well. There are easier functions to use, but my favorite is StringCbPrintf because it is not strickened by most of the problems that the others are. In this example, you would probably want to do something like

CHAR szCommand[256]
if(FAILED(StringCbPrintf(szCommand, sizeof(szCommand), "INSERT INTO other_info(card_id) VALUES (%s)", m_editbox1))
return -1; //if the command failed, you'll need to display an error or something and qui
recordset->Open(szCommand)

Take a look at the docs for the "safe" string functions at http://msdn.microsoft.com/library/en...es/strings.asp for more detailed information

The longer answer: This code will not work if the user were to enter something malformed. For example, if a used entered "5) exec xp_cmdshell 'format b:' --" into the edit box, the SQL string would become: "INSERT INTO other_info(card_id) VALUES (5) exec xp_cmdshell 'format b:' --)" which would format the b drive (if one existed, that is)! Preventing that is a much more challenging problem! The process of removing the problem characters from user input is called "sanitizing" the input, which is exactly what you'd need to do for this example to be safe for your end users. Luckily, in your example, the value you get from the user is an integer. In that case, you can convert the value in the edit box to an int using atoi() or a similar function, and then change the "%s" to a "%u" in the example code above to prevent the problem. Be aware though, that when you do use strings, you are opening the machine wide open to running arbitrary sql commands on the server. Here's the "safer" way

CHAR szCommand[256]
int i = atoi(m_editbox1)
if(FAILED(StringCbPrintf(szCommand, sizeof(szCommand), "INSERT INTO other_info(card_id) VALUES (%u)", i))
return -1; //if the command failed, you'll need to display an error or something and qui
recordset->Open(szCommand)

I hope that helps. It's also a good idea to be thinking more about SQL injections, even if you aren't concerned with them right now

-Aaro
Please post questions to the newsgroup - everyone benefits
This post is provided "AS IS" with no warranties, and confers no right
Sample code subject to http://www.microsoft.com/info/cpyright.ht
Nov 17 '05 #2

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

Similar topics

0
3053
by: Randi | last post by:
Hi all, I have this project to use the ADODB control to acces and manipulate the Access DB. I amd the mistake of first doing this project with just the data control. It worked fine with this...
9
9158
by: Ben | last post by:
I have two 'Do While Not' statements, that are getting information from the same recordset. If I comment out the first one I can get the results for the second one, and vice-versa. Why is this...
0
2844
by: elcc1958 | last post by:
I need to support a VB6 application that will be receiving disconnected ADODB.Recordset from out DotNet solution. Our dotnet solution deals with System.Data.DataTable. I need to populate a...
0
2466
by: Channing Jones | last post by:
Hello everyone, I am trying to store data in a binary field of an SQL-Server table using ADODB. So far, I have managed to store a record but not any data in the binary field. I only get...
6
1704
by: David | last post by:
I am using ADO to run some SQL statements in Access. First, I run query to create a table using SELECT . . . INTO Table A. Then, I run a query to SELECT . . . FROM Table A to get some data and...
5
6692
by: josephrthomas | last post by:
hi.. i am using ASP.NET with VB.NET to connect to a MS Access database... can someone pls tell me how do i make the sql statement to insert a new record into the existing table pls?? also...
3
3070
by: divya | last post by:
Hi, I have a table tblbwday with 2 fields Name and Birthday.I have written this script for displaying evryday names of the people on that day. <% set objConn...
7
1756
by: ammmmmu | last post by:
Hi all, I am using VB 5.0 and msaccess as a database, I am reading the data from logfiles and inserting it in db, its not throwing any error, but after excecution I not find any records in table...
6
5140
by: Oko | last post by:
I'm currently developing an MS Access Data Project (.adp) in MS Access 2002. One of the reports within the DB uses data that is Dynamic and cannot be stored on the SQL Server. To resolve this, I...
0
7229
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,...
0
7129
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...
0
7333
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,...
0
7398
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...
1
7061
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...
0
5637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5057
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...
0
4716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
428
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...

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.