请问数据库的高手,用VC实现一个ODBC的程序,请给我个简单的例子?程序内容最好包含数据的录入,删除,编辑,更新等操作,里面用SQL语句来实现,是用数据库对象来实现,而不是用绑定的形式。或者推荐个网站?有关介绍用VC做数据库的。
解决方案 »
- 自己写的软键盘程序,希望大家关注,小撒分数
- 关于一个操作声音的问题(内详)
- 求救..word嵌入
- 能否在线程里再开一个线程?
- 大家帮帮忙,一个关于listbox 的问题?
- 如何实现客户端在指定时间内,如果未受到服务器的数据就timeout?SetSockOpt如何用?
- 关于属性页的小问题!!!!!!!!进来有分
- 串口通讯中 收到一个特定字符 利马发送一串数据 为什么同样的操作系统 反应时间 一个是1 ms有的是 6。8ms 问题可能会出在哪呢?
- “temporary pointer” 为什么指针会是临时的,msdn上说GetParent()得到的是临时指针,难道父窗口的地址还会随机变化吗?
- 网页中含有控件,如何取网页数据和控件数据
- 你知不知道一个usb硬件插入计算机,怎么知道它的驱动是否已经装好了
- 要多少分给多少:我用DirectDraw来显示YUV格式文件但却不能在一个View中同时显示好几个不同的图象如何解决?详细代码请进。
#include <stdio.h>
#include <sql.h>
#include <sqlext.h>void CreateTable(HSTMT);main()
{ RETCODE rc;
HENV henv;
HDBC hdbc;
HSTMT hstmt;// Variables for SQLConnect
char * dsn = "UniteDB";
char * uid = "InterSvr";
char * pwd = "InterSvr";// Variables for SQLBindCol
char m_charOddScale[20] = {"\0"};
char m_charNoScale[20] = {"\0"};
char m_charTestChar[21] = {"\0"};
long sqlnts = SQL_NTS;// miscellaneous variables
char strOutput[100] = {"\0"};
char * SQLStr = "select OddScale, NoScale, Testchar from ScaleTable";
// Allocate ODBC handles and connect to Oracle
rc = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&henv);
rc = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3,0);
rc = SQLSetEnvAttr(henv,SQL_ATTR_CONNECTION_TIMEOUT,(void*)1000,0);
rc = SQLSetEnvAttr(henv,SQL_ATTR_LOGIN_TIMEOUT,(void*)SQL_LOGIN_TIMEOUT_DEFAULT,0);
rc = SQLAllocHandle(SQL_HANDLE_DBC,henv, &hdbc); rc = SQLConnect(hdbc, (unsigned char *)dsn,
SQL_NTS, (unsigned char *)uid,
SQL_NTS, (unsigned char *)pwd, SQL_NTS); rc = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
rc = SQLSetStmtAttr(hstmt,SQL_ATTR_MAX_ROWS,(void *)2,0);
// Table creation function
CreateTable(hstmt);
// Execute the statement and bind the column as SQL_C_CHAR
rc = SQLExecDirect(hstmt, (unsigned char *)SQLStr, SQL_NTS);
//rc = SQLColAttribute(hstmt, 3, SQL_DESC_OCTET_LENGTH, NULL, 0, NULL, (SQLPOINTER)&sqlnts); rc = SQLBindCol(hstmt, 1, SQL_C_CHAR, m_charOddScale, 20, &sqlnts);
rc = SQLBindCol(hstmt, 2, SQL_C_CHAR, m_charNoScale, 20, &sqlnts);
rc = SQLBindCol(hstmt, 3, SQL_C_CHAR, m_charTestChar, sizeof(m_charTestChar), &sqlnts); printf("\nOddScale (3) and NoScale as SQL_C_CHAR\n\n");// Fetch records and print the results
FILE *fp = NULL;
fp = fopen("test.txt","w");
while (SQLFetch(hstmt) != SQL_NO_DATA_FOUND)
{ //memset(strOutput,' ',sizeof(strOutput));
//strncpy(strOutput, m_charOddScale, strlen(m_charOddScale));
//strncpy(&strOutput[15],m_charNoScale, strlen(m_charNoScale)+1);
//strncpy(&strOutput[]) printf("\t%s\t%s\t%sEND\n",m_charOddScale,m_charNoScale,m_charTestChar);//strOutput);
fprintf(fp,"\t%s\t%s\t%sEND\n",m_charOddScale,m_charNoScale,m_charTestChar); }
fclose(fp);// Cleanup
SQLFreeStmt(hstmt, SQL_CLOSE);
SQLFreeStmt(hstmt, SQL_DROP);
SQLDisconnect(hdbc);
SQLFreeConnect(hdbc);
SQLFreeEnv(henv);
return(TRUE);
};//------- CreateTable() ----------------void CreateTable(HSTMT hstmt)
{ RETCODE rc = 0;
char SqlStatements[6][255] =
{"Drop table ScaleTable",
"Create table ScaleTable (OddScale numeric(10,3), NoScale numeric, testchar char(20))",
"Insert into ScaleTable values (1.1, 1.1,'12345 1 ')",
"Insert into ScaleTable values (1.12, 1.12,'12345 1')",
"Insert into ScaleTable values (1.123, 1.123,'12345 1')",
"Insert into ScaleTable values (1.005, 1.005,'1234567890')"}; rc = SQLExecDirect(hstmt, (unsigned char *)SqlStatements[0], SQL_NTS);
SQLFreeStmt(hstmt, SQL_CLOSE);
rc = SQLExecDirect(hstmt, (unsigned char *)SqlStatements[1], SQL_NTS);
SQLFreeStmt(hstmt, SQL_CLOSE);
rc = SQLExecDirect(hstmt, (unsigned char *)SqlStatements[2], SQL_NTS);
SQLFreeStmt(hstmt, SQL_CLOSE);
rc = SQLExecDirect(hstmt, (unsigned char *)SqlStatements[3], SQL_NTS);
SQLFreeStmt(hstmt, SQL_CLOSE);
rc = SQLExecDirect(hstmt, (unsigned char *)SqlStatements[4], SQL_NTS);
SQLFreeStmt(hstmt, SQL_CLOSE);
rc = SQLExecDirect(hstmt, (unsigned char *)SqlStatements[5], SQL_NTS);
SQLFreeStmt(hstmt, SQL_CLOSE);
}
但是有的地方看不懂,能不能给点注视,或者说明阿。rc = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3,0);
rc = SQLSetEnvAttr(henv,SQL_ATTR_CONNECTION_TIMEOUT,(void*)1000,0);
rc = SQLSetEnvAttr(henv,SQL_ATTR_LOGIN_TIMEOUT,(void*)QL_LOGIN_TIMEOUT_DEFAULT,0);
连续三个SQLSetEnvAttr什么意思?
呵呵。借楼主的光,学点东西了。希望楼主不要见怪。