如何用C语言嵌入式SQL访问2008SQL数据库???

2024-11-22 12:22:37
推荐回答(1个)
回答1:

#include "resource.h"
#include
#include
SQLHENV hEnv = NULL;
SQLHDBC hConn = NULL;
SQLHSTMT hStmt=NULL;
SQLHSTMT hStmt1=NULL;
HWND hWinMain;
unsigned char ConnString[1024];
unsigned char FullString[1024];
unsigned char buffer1[1024];
unsigned char number[50];
unsigned char branch[50];
unsigned char money[50];
unsigned char szSQL[100]={"insert into account values(?,?,?)"};
long int size1,size2,size3;
void Connect()
{

int ax,length;
short int *Temp=NULL;
ax=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&hEnv);
if(ax!=SQL_SUCCESS && ax!=SQL_SUCCESS_WITH_INFO)
return;

ax=SQLSetEnvAttr(hEnv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);
if(ax!=SQL_SUCCESS && ax!=SQL_SUCCESS_WITH_INFO)
return;

ax=SQLAllocHandle(SQL_HANDLE_DBC,hEnv,&hConn);
if(ax!=SQL_SUCCESS && ax!=SQL_SUCCESS_WITH_INFO)
return;

SQLSetConnectAttr(hConn,SQL_ATTR_AUTOCOMMIT,SQL_AUTOCOMMIT_OFF,0);
length=sizeof(ConnString);
ax=SQLDriverConnect(hConn,hWinMain,ConnString,length,FullString,sizeof(FullString),Temp,SQL_DRIVER_COMPLETE);
if(ax==SQL_SUCCESS || ax==SQL_SUCCESS_WITH_INFO)
MessageBox(hWinMain,(char *)FullString,TEXT("提示:"),0);
}
BOOL CALLBACK MainDlg (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND a;
int ax;
switch(message)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_BUTTON1:
Connect();return true;break;
case IDC_EDIT1:
GetDlgItemText(hwnd,IDC_EDIT1,(char *)ConnString,sizeof(ConnString));
a=GetDlgItem(hwnd,IDC_BUTTON1);
if(*ConnString)
EnableWindow(a,TRUE);
else
EnableWindow(a,FALSE);
return true;break;
case IDC_BUTTON2:
GetDlgItemText(hwnd,IDC_EDIT5,(char *)buffer1,sizeof(buffer1));
ax=SQLAllocHandle(SQL_HANDLE_STMT,hConn,&hStmt);
if(ax!=SQL_SUCCESS && ax!=SQL_SUCCESS_WITH_INFO)
return 0;

SQLSetStmtAttr(hStmt,SQL_ATTR_CURSOR_TYPE,(void *)SQL_CURSOR_STATIC,0);
MessageBox(hWinMain,(char *)buffer1,TEXT("提示:"),0);
ax=lstrlen((char *)buffer1);
SQLExecDirect(hStmt,buffer1,ax);
SQLFreeHandle(SQL_HANDLE_STMT,hStmt);break;
case IDCANCEL:
if(hConn)
{
SQLEndTran(SQL_HANDLE_DBC,hConn,SQL_COMMIT);
SQLDisconnect(hConn);
SQLFreeHandle(SQL_HANDLE_DBC,hConn);
}
if(hEnv)
SQLFreeHandle(SQL_HANDLE_ENV,hEnv);
MessageBox(hWinMain,TEXT("已成功与数据库断开连接!"),TEXT("提示...."),0);
break;
case IDOK:
GetDlgItemText(hwnd,IDC_EDIT2,(char *)number,sizeof(number));
GetDlgItemText(hwnd,IDC_EDIT3,(char *)branch,sizeof(branch));
GetDlgItemText(hwnd,IDC_EDIT4,(char *)money,sizeof(money));
size1=sizeof(number);
size2=sizeof(branch);
size3=sizeof(money);
ax=SQLAllocHandle(SQL_HANDLE_STMT,hConn,&hStmt1);
if(ax!=SQL_SUCCESS && ax!=SQL_SUCCESS_WITH_INFO)
return 0;
SQLSetStmtAttr(hStmt1,SQL_ATTR_CURSOR_TYPE,(void *)SQL_CURSOR_STATIC,0);
SQLPrepare(hStmt1,szSQL,sizeof(szSQL));
SQLBindParameter(hStmt1,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_VARCHAR,50,0,number,10,&size1);
SQLBindParameter(hStmt1,2,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_VARCHAR,50,0,branch,10,&size2);
SQLBindParameter(hStmt1,3,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_VARCHAR,50,0,money,10,&size3);
SQLExecute(hStmt1);
MessageBox(hWinMain,TEXT("已成功添加一条记录!"),TEXT("提示...."),0);
SQLFreeHandle(SQL_HANDLE_STMT,hStmt1);
break;
}
case WM_INITDIALOG:
hWinMain=hwnd;return true;break;
case WM_CLOSE:
EndDialog(hwnd,0);
return true;break;
}
return false;

}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
DialogBoxParam (hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, MainDlg, NULL);
return 0;
}
//Driver={SQL Server};Server=服务器名;Database=eeqt(文件名)
//insert into account values(520,'中国银行',30000)