C语言使用ODBC连接SQL Server2005出错
一直听说数据库很麻烦,没想到麻烦到如此地步,以下是代码:
程序代码:
// stdafx.h : 标准系统包含文件的包含文件, // 或是经常使用但不常更改的 // 特定于项目的包含文件 //这里是stdafx.h #pragma once #include "targetver.h" #define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料 // Windows 头文件: #include <windows.h> // C 运行时头文件 #include <stdlib.h> #include <malloc.h> #include <memory.h> #include <tchar.h> // TODO: 在此处引用程序需要的其他头文件 #include <process.h> #include <sql.h> #include <sqlext.h> #include <sqltypes.h> #include <sqlucode.h> #include <Wininet.h> #include <WinSock2.h> #pragma comment(lib,"odbc32.lib") #pragma comment(lib,"odbccp32.lib") #pragma comment(lib,"ws2_32") #pragma comment(lib,"wininet") -------------------------------------------------------------------------- // Server.cpp : 定义应用程序的入口点。 // #include "stdafx.h" #include "Server.h" #define SQLDRIVERSTRING TEXT("Dsn=TEST;description=test;uid=sa;app=Microsoft? Visual Studio? 2008;wsid=NOKIA;database=TEST") // 全局变量: HINSTANCE hInst; // 当前实例 HWND hDLG=0; BOOL ExitSignal=TRUE; SQLHANDLE hSqlEnv=0; SQLHANDLE hSqlCon=0; SQLHANDLE hSqlStmt=0; UINT_PTR hTimer=0; INT_PTR CALLBACK WndProc(HWND , UINT , WPARAM , LPARAM ); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); VOID PrintSqlMessage(SQLSMALLINT Type,SQLHANDLE hSql,HWND hWnd=hDLG) { //////打印SQL错误消息 TCHAR Status[20],Str[256],ErrMsg[256]; SQLSMALLINT SqlMsgLen; SQLINTEGER SqlErr; LONG Erg2=SQLGetDiagRec(Type,hSql,1,(SQLTCHAR *)Status,&SqlErr,(SQLTCHAR*)Str,\ 255,&SqlMsgLen); wsprintf(ErrMsg,TEXT("%s (%d)\n"),Str,(INT)SqlErr); MessageBox(hWnd,ErrMsg,TEXT("ERROR"),MB_OK); return; } int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { DialogBoxParam(hInstance,(LPCWSTR)DLG_MAIN,0,WndProc,0); return 0; } INT_PTR CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; SQLRETURN SqlRet; SQLTCHAR OutConnStr[255]; SQLSMALLINT OutConnStrLen; switch (message) { case WM_TIMER:///每分钟刷新一次数据库. break; case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // 分析菜单选择: switch (wmId) { case IDM_EXIT: ExitSignal=FALSE; EndDialog(hWnd,0); Sleep(2000); break; default: return FALSE; } break; case WM_INITDIALOG: hDLG=hWnd; SqlRet=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&hSqlEnv); //SQL 环境句柄 if(SqlRet==SQL_SUCCESS||SqlRet==SQL_SUCCESS_WITH_INFO) { SQLSetEnvAttr(hSqlEnv,SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,0); SqlRet=SQLAllocHandle(SQL_HANDLE_DBC,hSqlEnv,&hSqlCon); ////SQL连接句柄 if(SqlRet==SQL_SUCCESS||SqlRet==SQL_SUCCESS_WITH_INFO) { SQLSetConnectAttr(hSqlCon,SQL_ATTR_AUTOCOMMIT,SQL_AUTOCOMMIT_OFF,0); [COLOR="Red"] SqlRet=SQLDriverConnect(hSqlCon, hWnd, \ (SQLTCHAR*)SQLDRIVERSTRING, _countof(SQLDRIVERSTRING),\ OutConnStr,255,&OutConnStrLen,SQL_DRIVER_NOPROMPT ); [/COLOR] ////问题就出现在这,连接时老是错误 if(SqlRet!=SQL_SUCCESS||SqlRet!=SQL_SUCCESS_WITH_INFO) { PrintSqlMessage(SQL_HANDLE_DBC,hSqlCon); goto Error1; } } else { PrintSqlMessage(SQL_HANDLE_DBC,hSqlCon); goto Error1; } } else { PrintSqlMessage(SQL_HANDLE_ENV,hSqlEnv); Error1: SendMessage(hWnd,WM_CLOSE,0,0); break; } hTimer=SetTimer(hWnd,1111,1000*60,0);//更新数据库数据 _beginthread(_Thread,0,0);//网络连接部分 break; case WM_CLOSE: ExitSignal=FALSE; EndDialog(hWnd,0); Sleep(2000); if(hSqlStmt)SQLFreeStmt(hSqlStmt,SQL_CLOSE); if(hSqlCon){SQLDisconnect(hSqlCon);SQLFreeHandle(SQL_HANDLE_DBC,hSqlCon);} if(hSqlEnv)SQLFreeHandle(SQL_HANDLE_ENV,hSqlEnv); if(hTimer)KillTimer(hWnd,1111); break; default: return FALSE; } return TRUE; }
包括连接字符串,都是vS生成的:
如果我不设置ODBC数据源,出错信息如下:
如果设置了,如下:
求各位指点迷津~~~~