#pragma comment(linker, "/ENTRY:Entry")
#pragma comment(linker, "/subsystem:windows")
#include <Windows.h>
#include <commctrl.h>
#pragma comment(lib,"comctl32.lib")
#include "sqlext.h"
HWND h_list;
HWND h_Static;
HWND h_file;
HWND h_table;
HWND h_sql;
SQLHENV hEnv;
SQLHDBC hConn;
wchar_t szFile[260];
wchar_t Conn[MAX_PATH];
LONG StrLen;
SQLHSTMT hstmt;
wchar_t TheName[260];
wchar_t TheSurname[260];
wchar_t TelNo[21];
LONG NameLength;
LONG SurnameLength;
LONG TelnoLength;
TCHAR tem[2600] = {0};
SQLSMALLINT dwRecordCols;
SQLWCHAR szName[128*4] = {0};
SQLSMALLINT dwNameSize;
SQLSMALLINT dwType;
SQLULEN dwSize;
SQLSMALLINT dwSize1;
SQLSMALLINT dwNullable;
//有空把上面的变量给变成局部的。
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HWND h_Static;
switch (uMsg)
{
case WM_CREATE:
h_file = CreateWindowEx(WS_EX_CLIENTEDGE,L"Static",0,WS_CHILD | WS_VISIBLE | SS_LEFT | WS_GROUP,0,0,904,20,hWnd,0,GetModuleHandle(0),0);
SendMessage(h_file,WM_SETTEXT,0,(LPARAM)L"暂不支持拖动");
CreateWindowEx(NULL,L"button",L"打开文件",WS_CHILD | WS_VISIBLE,904,0,90,20,hWnd,(HMENU)9,GetModuleHandle(0),NULL);
h_table = CreateWindowEx(WS_EX_CLIENTEDGE,L"Static",0,WS_CHILD | WS_VISIBLE | SS_LEFT | WS_GROUP,0,20,994,20,hWnd,0,GetModuleHandle(0),0);
SendMessage(h_table,WM_SETTEXT,0,(LPARAM)L"显示数据库中的所有的表");
h_sql = CreateWindowEx(0,L"EDIT",0,WS_CHILD | WS_VISIBLE ,0,40,904,20,hWnd,0,GetModuleHandle(0),0);
SendMessage(h_sql,WM_SETTEXT,0,(LPARAM)L"SQL语句");
CreateWindowEx(NULL,L"button",L"执行语句",WS_CHILD | WS_VISIBLE,904,40,90,20,hWnd,(HMENU)99,GetModuleHandle(0),NULL);
InitCommonControls();
h_list = CreateWindowEx(0,L"SysListView32",0,WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SINGLESEL,0,60,994,500,hWnd,0,GetModuleHandle(0),0);
SendMessage(h_list,LVM_SETEXTENDEDLISTVIEWSTYLE,LVS_EX_FULLROWSELECT,LVS_EX_FULLROWSELECT);
h_Static = CreateWindowEx(WS_EX_CLIENTEDGE,L"Static",0,WS_CHILD | WS_VISIBLE | SS_LEFT | WS_GROUP,0,560,994,80,hWnd,0,GetModuleHandle(0),0);
SendMessage(h_Static,WM_SETTEXT,0,(LPARAM)L"要得到一个表的所有的信息请使用:select * from table\n\
不足之处,敬请指导\n\
email:leguanyuan at 126 dot com\n\
homepage:http://correy.webs.com");
break;
case WM_COMMAND:
if (wParam == 9)
{//选择文件按钮。
OPENFILENAME ofn; //可以直接填写。
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFile = szFile;
ofn.lpstrFile[0] = L'\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = L"mdb\0*.mdb\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
GetOpenFileName(&ofn);
SendMessage(h_file,WM_SETTEXT,0,(LPARAM)szFile);
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv);
SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER*)SQL_OV_ODBC3, 0);
SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hConn);
wchar_t ConnectString[1024] = L"DRIVER={Microsoft Access Driver (*.mdb)};DBQ=";
lstrcat(ConnectString,szFile);
SQLRETURN sqlRet = SQLDriverConnect(hConn,hWnd,(SQLWCHAR * )ConnectString,sizeof(ConnectString),(SQLWCHAR * )Conn,sizeof(Conn),(short *)&StrLen,SQL_DRIVER_COMPLETE);
//MessageBox(hWnd,Conn, L"Complete Connection String",MB_OK|MB_ICONINFORMATION);
SQLAllocHandle(SQL_HANDLE_STMT,hConn,&hstmt);
char tem[260] = {0};//获取一个数据库中的所有的表。
if( SQL_SUCCESS == SQLTables( hstmt, NULL, 0, NULL, 0, NULL, 0, NULL, 0) )
{
SDWORD cb;
char szTable[255];
char szTableType[255];
SQLBindCol( hstmt, 3, SQL_C_CHAR, szTable, 255, &cb );
SQLBindCol( hstmt, 4, SQL_C_CHAR, szTableType, 255, &cb );
while( SQL_SUCCESS == SQLFetch(hstmt) )
{
if (lstrcmpiA(szTableType,"TABLE") == 0)
{
lstrcatA(tem,szTable);
lstrcatA(tem,"\t");
}
}
}
//wchar_t t[260] = {0}; //要想用宽字符的,请用这个。
//MultiByteToWideChar(CP_ACP, 0,(LPCSTR)tem,lstrlenA((LPCSTR)tem),t,sizeof(t));
SendMessageA(h_table,WM_SETTEXT,0,(LPARAM)tem);//加这后,不能显示数据了。再看看。
//SQLFreeHandle( SQL_HANDLE_STMT, hstmt );
}
if (wParam == 99)
{//执行按钮。
SQLAllocHandle(SQL_HANDLE_STMT,hConn,&hstmt);//必须加上这一行。
wchar_t SQLStatement[260] = {0};// L"select * from corporation";
GetWindowText(h_sql,SQLStatement,sizeof(SQLStatement));
SQLExecDirect(hstmt,SQLStatement,sizeof(SQLStatement));//一下功能还没有添加:
//只有dql语句会显示。
//dml,ddl,dcl语句会执行,但不会立即显示。
SendMessage(h_list,LVM_DELETEALLITEMS,0,0); //清除原来的列标题。
for (;;)
{
int x = SendMessage(h_list,LVM_DELETECOLUMN,0,0);
if (x == 0) break;
}
SQLNumResultCols(hstmt,&dwRecordCols);
for(int i = 1;i < (dwRecordCols + 1);i++) //设置列的标题。
{
SQLDescribeCol(hstmt,i,szName,sizeof (szName),& dwNameSize,& dwType,& dwSize,& dwSize1,& dwNullable);
LV_COLUMN lvc;
RtlZeroMemory(&lvc,sizeof(lvc));
lvc.mask = LVCF_TEXT+LVCF_WIDTH;
lvc.pszText = szName;
lvc.cx = 60;
SendMessage(h_list, LVM_INSERTCOLUMN, i-1, (LPARAM)&lvc);
SQLBindCol(hstmt,i,SQL_C_CHAR,TheName,sizeof(TheName),&NameLength);
}
SendMessage(h_list,LVM_DELETEALLITEMS,0,0);
SQLRETURN sqlRet;
LV_ITEM lvi;
//DWORD row = 0;
while(1)
{
TheName[0] = 0;
sqlRet = SQLFetch(hstmt);//取下一条
if(sqlRet == SQL_SUCCESS || sqlRet == SQL_SUCCESS_WITH_INFO)
{//显示所有的记录。
// lvi.iItem = row;
for(int i = 1;i<dwRecordCols + 1;i++)
{//显示一个记录的得到信息。
SQLINTEGER indicator;
char buf[512] = {0};
SQLGetData(hstmt, i, SQL_C_CHAR, buf, sizeof(buf), &indicator);
lvi.mask = LVIF_TEXT;
lvi.iItem = 0;
lvi.iSubItem = i-1;
if (i == 1)
{
RtlZeroMemory(tem,sizeof(tem));
MultiByteToWideChar(CP_ACP, 0,(LPCSTR)buf,lstrlenA((LPCSTR)buf),tem,sizeof(tem));
lvi.pszText = tem ;
SendMessage(h_list,LVM_INSERTITEM,0,(LPARAM)&lvi);
}
else
{
RtlZeroMemory(tem,sizeof(tem));
MultiByteToWideChar(CP_ACP, 0,(LPCSTR)buf,lstrlenA((LPCSTR)buf),tem,sizeof(tem));
lvi.pszText = tem;
SendMessage(h_list,LVM_SETITEM,0,(LPARAM)&lvi);
}
}
}
else
{
break;
}
//row++;
}
SQLCloseCursor(hstmt);
SQLFreeHandle(SQL_HANDLE_STMT,hstmt);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return(DefWindowProc(hWnd, uMsg, wParam, lParam));
}
return(0);
}
void Entry()
{
WNDCLASSEX sWndClassEx = {sizeof(WNDCLASSEX), CS_HREDRAW | CS_VREDRAW,WindowProc,0,0,GetModuleHandle(0),0,LoadCursor(0,IDC_ARROW),(HBRUSH)6,0,L"correy",0};
ATOM a = RegisterClassEx(&sWndClassEx);
ShowWindow(CreateWindowEx(WS_EX_CLIENTEDGE /* 0 */,L"correy",L"made by correy",0x0Ca0000 ,CW_USEDEFAULT,CW_USEDEFAULT,999,666,0,0, GetModuleHandle(0),0),1);
MSG sMsg;
while (GetMessage(&sMsg, 0, 0, 0))
{
TranslateMessage(&sMsg);//少了这一行,会使edit控件不可以输入,不支持backspace。这个花费我2小时的时间才解决。
DispatchMessage(&sMsg);
}
ExitProcess(0);
}
//made at 2012.01.05
没有评论:
发表评论