标题:为什么程序运行后数据没有写入数据库?
只看楼主
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
结帖率:79.17%
 问题点数:0 回复次数:0 
为什么程序运行后数据没有写入数据库?
// Sample ODBC3 console application to write SQL_LONGVARCHAR data
// using SQLPutData.
// Assumes DSN has table:
//  SQLSrvr: CREATE TABLE emp3 (NAME char(30), AGE int,
//           BIRTHDAY datetime, Memo1 text)

#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <odbcss.h>

#define TEXTSIZE   12000

SQLHENV      henv = SQL_NULL_HENV;
SQLHDBC      hdbc1 = SQL_NULL_HDBC;     
SQLHSTMT     hstmt1 = SQL_NULL_HSTMT;

int main()
{
   RETCODE retcode;
      
   // SQLBindParameter variables.
   SDWORD      cbTextSize, lbytes;
   //SQLParamData variable.
   PTR         pParmID;
   //SQLPutData variables.
   UCHAR   Data[] =
         "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
         "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
         "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
         "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
         "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
         "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
         "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
         "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
         "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
         "abcdefghijklmnopqrstuvwxyz";
   SDWORD   cbBatch = (SDWORD)sizeof(Data)-1; // 得到实际字符长度
   
    // Allocate the ODBC environment and save handle.
   retcode = SQLAllocHandle (SQL_HANDLE_ENV, NULL, &henv);
   // Notify ODBC that this is an ODBC 3.0 app.
   retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
                     (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_INTEGER);
   // Allocate ODBC connection handle and connect.
   retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1);
   retcode = SQLConnect(hdbc1, "MyDSN", SQL_NTS,
                   "sa", SQL_NTS, "MyPassWord", SQL_NTS);
   
   // Allocate statement handle.
   retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc1, &hstmt1);

   // Set parameters based on total data to send.
   lbytes = (SDWORD)TEXTSIZE;
   cbTextSize = SQL_LEN_DATA_AT_EXEC(lbytes);
   // Bind the parameter marker.
   retcode = SQLBindParameter(hstmt1,   // hstmt
         1,                             // ipar
         SQL_PARAM_INPUT,               // fParamType
         SQL_C_CHAR,                    // fCType
         SQL_LONGVARCHAR,               // FSqlType
         lbytes,                        // cbColDef
         0,                             // ibScale
         (VOID *)1,                     // rgbValue
         0,                             // cbValueMax
         &cbTextSize);                  // pcbValue

   // Execute the command.
   retcode = SQLExecDirect(hstmt1, "INSERT INTO emp3 VALUES('Paul Borm', 46,'1950-11-24 00:00:00', ?)", SQL_NTS);
   // Check to see if NEED_DATA; if yes, use SQLPutData.
   retcode = SQLParamData(hstmt1, &pParmID);
   // SQL_NEED_DATA : While processing a statement, the driver
   //                 determined that the application needs to send parameter data values.
   if (retcode == SQL_NEED_DATA)
   {
      while (lbytes > cbBatch)
      {
         SQLPutData(hstmt1, Data, cbBatch);
         lbytes -= cbBatch;
      }
      // Put final batch.
      SQLPutData(hstmt1, Data, lbytes);
   }
   else
   {
         //ProcessErrorMessages(SQL_HANDLE_STMT, hstmt1,"SQLPutData Failed\n\n");
         fprintf(stderr,"SQLPutData Failed\n\n");
         return(9);
   }

   // Make final SQLParamData call.
   retcode = SQLParamData(hstmt1, &pParmID);

   /* Clean up. */
   SQLFreeHandle(SQL_HANDLE_STMT, hstmt1);
   SQLDisconnect(hdbc1);
   SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
   SQLFreeHandle(SQL_HANDLE_ENV, henv);
   return(0);
}以上程序是MSDN上的示例程序,但是我手动配置了一个数据源 MyDSN用户名"sa", 密码 "MyPassWord后,运行完毕数据库文件中兵没有写入数据字符串 data ,这是为什么呢?
搜索更多相关主题的帖子: 数据库 运行 
2009-09-12 19:41



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-285540-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.156606 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved