源程序是这样的
SQL是 SQL2008 开发版本(DEV)
#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#include "resource.h"
#include "MainDlg.h"
#define LOGIN_TIMEOUT 30
#define MAXBUFLEN 255
#define CHECKDBSTMTERROR(hwnd,result,hstmt) if(SQL_ERROR==result){ShowDBStmtError(hwnd,hstmt);return;}
BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);
HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);
HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);
}
return FALSE;
}
BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
return TRUE;
}
void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
switch(id)
{
case IDC_OK:
DBTest(hwnd);
break;
default:
break;
}
}
void Main_OnClose(HWND hwnd)
{
EndDialog(hwnd, 0);
}
void ShowDBError(HWND hwnd,SQLSMALLINT type,SQLHANDLE sqlHandle)
{
char pStatus[10], pMsg[101];
SQLSMALLINT SQLmsglen;
char error[200] = {0};
SQLINTEGER SQLerr;
long erg2 = SQLGetDiagRec(type, sqlHandle,1,(SQLCHAR *)pStatus,&SQLerr,(SQLCHAR *)pMsg,100,&SQLmsglen);
wsprintf(error,"%s (%d)\n",pMsg,(int)SQLerr);
MessageBox(hwnd,error,TEXT("数据库执行错误"),MB_ICONERROR|MB_OK);
}
void ShowDBConnError(HWND hwnd,SQLHDBC hdbc)
{
ShowDBError(hwnd,SQL_HANDLE_DBC,hdbc);
}
void ShowDBStmtError(HWND hwnd,SQLHSTMT hstmt)
{
ShowDBError(hwnd,SQL_HANDLE_STMT,hstmt);
}
void DBTest(HWND hwnd)
{
SQLHENV henv = NULL;
SQLHDBC hdbc = NULL;
SQLHSTMT hstmt = NULL;
SQLRETURN result;
SQLCHAR ConnStrIn[MAXBUFLEN] = "DRIVER={SQL Server};SERVER=127.0.0.1;UID=sa;PWD=SQL2008;DATABASE=part;";
SQLCHAR ConnStrOut[MAXBUFLEN];
//分配环境句柄
result = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
//设置管理环境属性
result = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
//分配连接句柄
result = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
//设置连接属性
result = SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (void*)LOGIN_TIMEOUT, 0);
//连接数据库
result = SQLDriverConnect(hdbc,NULL,
ConnStrIn,SQL_NTS,
ConnStrOut,MAXBUFLEN,
(SQLSMALLINT *)0,SQL_DRIVER_NOPROMPT);
if(SQL_ERROR==result)
{
ShowDBConnError(hwnd,hdbc);
return;
}
//初始化语句句柄
result = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
//SQL_NTS telling the function the previous parameter is Null-Terminated String,
//please alculate the string length for me
result = SQLPrepare(hstmt,(SQLCHAR*)"insert into qty values(20)",SQL_NTS);
CHECKDBSTMTERROR(hwnd,result,hstmt);
result =SQLExecute(hstmt);//执行语句
CHECKDBSTMTERROR(hwnd,result,hstmt);
SQLFreeStmt(hstmt,SQL_CLOSE);
SQLDisconnect(hdbc);
SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
SQLFreeHandle(SQL_HANDLE_ENV,henv);
MessageBox(hwnd,TEXT("执行成功"),TEXT("标题"),MB_OK);
}
有疑问的语句: SQLCHAR ConnStrIn[MAXBUFLEN] = "DRIVER={SQL Server};SERVER=127.0.0.1;UID=sa;PWD=SQL2008;DATABASE=part;";
运行后出现这个问题:[Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server 不存在或访问被拒绝(17)
不知道是程序有问题还是SQL2008配置的问题 麻烦解决下
PS:SQL是 SQL2008 开发版本(DEV)
DRIVER=你的服务器名称或IP
打开 sql Server 配置工具 —Sql server 网络配置—MSSQLSERVER协议
选中TCP/IP,然后把里面的1433端口启用,重新SQL SERVER服务,应该就可以解决你的问题
你好,请问这个问题您最后解决了没?怎么解决的?能教下我吗?我最近一直在弄,但就是连不上。
你好,我也是一直在用控制台程序连接数据库,但是始终连不上,不知道怎么回事?您确定没有使用mfc吗