how to insert data in sqlserver 2005 using vc++6.0
-
hi everybody. in my vc++ application cannot insert data into database.... success fully inserting to passing values...using this code line SQLExecDirect(hstmt,(unsigned char *)"insert into emp values(222,'shan',5676)" ,SQL_NTS); but using varibles(dynamically) cannot inserting. SQLINTEGER empnum=1006; SQLVARCHAR ename[20]="weewe"; SQLFLOAT esal=5656; SQLExecDirect(hstmt,(unsigned char *)"insert into emp values(empnum,'ename',esal)",SQL_NTS); please solve this porblem...... thanks in advance......
-
hi everybody. in my vc++ application cannot insert data into database.... success fully inserting to passing values...using this code line SQLExecDirect(hstmt,(unsigned char *)"insert into emp values(222,'shan',5676)" ,SQL_NTS); but using varibles(dynamically) cannot inserting. SQLINTEGER empnum=1006; SQLVARCHAR ename[20]="weewe"; SQLFLOAT esal=5656; SQLExecDirect(hstmt,(unsigned char *)"insert into emp values(empnum,'ename',esal)",SQL_NTS); please solve this porblem...... thanks in advance......
eswar pothula wrote:
SQLExecDirect(hstmt,(unsigned char *)"insert into emp values(empnum,'ename',esal)",SQL_NTS);
Code is treating (empnum,'ename',esal)as the values and not as variable...So this code tries to insert string into Interger data type for the colum Emp number and salary and so it errors out If you want to dynamically assign the values, my sugesstion would be bind the parameters using
SQLBindParameter
and execute using
SQLExecute
cheers, Super ------------------------------------------ Too much of good is bad,mix some evil in it
modified on Tuesday, November 3, 2009 5:21 AM
-
hi everybody. in my vc++ application cannot insert data into database.... success fully inserting to passing values...using this code line SQLExecDirect(hstmt,(unsigned char *)"insert into emp values(222,'shan',5676)" ,SQL_NTS); but using varibles(dynamically) cannot inserting. SQLINTEGER empnum=1006; SQLVARCHAR ename[20]="weewe"; SQLFLOAT esal=5656; SQLExecDirect(hstmt,(unsigned char *)"insert into emp values(empnum,'ename',esal)",SQL_NTS); please solve this porblem...... thanks in advance......
eswar pothula wrote:
SQLExecDirect(hstmt,(unsigned char *)"insert into emp values(empnum,'ename',esal)",SQL_NTS);
That's wrong. Try to change it to:
const int SIZE = 256;
char sqlcmd[SIZE];
sprintf(sqlcmd, "insert into emp values(%d,'%s',%f)", empnum, ename, esal);
SQLExecDirect(hstmt,(unsigned char *) sqlcmd, SQL_NTS);of course, in real life, you should check for buffer overruns, etc... :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]