第1个回答 2009-07-15
1---------------------------------------
#include
#include
#include
#include
#include
#include
#define SNO_LEN 30
#define NAME_LEN 50
#define DEPART_LEN 100
#define SSEX_LEN 5
int main()
{
/* Step 1 定义句柄和变量 */
//以king开头的表示的是连接KINGBASEES的变量
//以server开头的表示的是连接SQLSERVER的变量
SQLHENV kinghenv, serverhenv; //环境句柄
SQLHDBC kinghdbc,serverhdbc; //连接句柄
SQLHSTMT kinghstmt,serverhstmt; //语句句柄
SQLRETURN ret;
SQLCHAR sName[NAME_LEN], sDepart[DEPART_LEN],sSex[SSEX_LEN],sSno[SNO_LEN];
SQLINTEGER sAge;
SQLINTEGER cbAge = 0, cbSno = SQL_NTS, cbSex = SQL_NTS,cbName = SQL_NTS,cbDepart = SQL_NTS;
/* Step 2 初始化环境 */
ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &kinghenv);
ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &serverhenv);
ret = SQLSetEnvAttr(kinghenv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
ret = SQLSetEnvAttr(serverhenv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
/* Step 3 :建立连接 */
ret = SQLAllocHandle(SQL_HANDLE_DBC, kinghenv, &kinghdbc);
ret = SQLAllocHandle(SQL_HANDLE_DBC, serverhenv, &serverhdbc);
ret = SQLConnect(kinghdbc,
"KingbaseES ODBC",SQL_NTS,
"SYSTEM",SQL_NTS,
"MANAGER",SQL_NTS);
if (!SQL_SUCCEEDED(ret)) //连接失败时返回错误值
return -1;
ret = SQLConnect(serverhdbc,
"SQLServer",SQL_NTS,
"sa",SQL_NTS,
"sa",SQL_NTS);
if (!SQL_SUCCEEDED(ret)) //连接失败时返回错误值
return -1;
/* Step 4 :初始化语句句柄 */
ret = SQLAllocHandle(SQL_HANDLE_STMT, kinghdbc, &kinghstmt);
ret = SQLSetStmtAttr(kinghstmt,SQL_ATTR_ROW_BIND_TYPE,(SQLPOINTER)SQL_BIND_BY_COLUMN,SQL_IS_INTEGER );
ret = SQLAllocHandle(SQL_HANDLE_STMT, serverhdbc, &serverhstmt);
/* Step 5 :两种方式执行语句 */
/* 预编译带有参数的语句 */
ret = SQLPrepare(serverhstmt,"INSERT INTO STUDENT (SNO,SNAME,SSEX,SAGE,SDEPT) VALUES ( , , , , )", SQL_NTS);
if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
{
ret = SQLBindParameter(serverhstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR,SQL_CHAR, SNO_LEN, 0, sSno, 0, &cbSno);
ret = SQLBindParameter(serverhstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR,SQL_CHAR, NAME_LEN, 0, sName, 0, &cbName);
ret = SQLBindParameter(serverhstmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR,SQL_CHAR, 2, 0, sSex, 0, &cbSex);
ret = SQLBindParameter(serverhstmt, 4, SQL_PARAM_INPUT, SQL_C_LONG,SQL_INTEGER, 0, 0, &sAge, 0, &cbAge);
ret = SQLBindParameter(serverhstmt, 5, SQL_PARAM_INPUT, SQL_C_CHAR,SQL_CHAR, DEPART_LEN, 0, sDepart, 0, &cbDepart);
}
/*执行SQL语句*/
ret = SQLExecDirect(kinghstmt,"SELECT * FROM STUDENT",SQL_NTS);
if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
{
ret = SQLBindCol(kinghstmt, 1, SQL_C_CHAR,sSno,SNO_LEN, &cbSno);
ret = SQLBindCol(kinghstmt, 2, SQL_C_CHAR, sName, NAME_LEN, &cbName);
ret = SQLBindCol(kinghstmt, 3, SQL_C_CHAR, sSex, SSEX_LEN, &cbSex);
ret = SQLBindCol(kinghstmt, 4, SQL_C_LONG, &sAge, 0, &cbAge);
ret = SQLBindCol(kinghstmt, 5, SQL_C_CHAR, sDepart, DEPART_LEN, &cbDepart);
2-------------------------------------------
1.提取单条记录
//#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
//no_namespace,rename("EOF","adoEOF"),named_guids
CoInitialize(NULL);
_bstr_t varSource="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=*.mdb";
//_bstr_t varSource="Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"
_ConnectionPtr m_pConnection(_uuidof(Connection));
m_pConnection->Open(varSource,"","",adModeUnknow);
_RecordsetPtr m_pSet(_uuid(Recordset));
try {
m_pSet->Open(%%1,m_pConnection.GetInterfacePtr()
adOpenDynamic,adLockPessimistic,adCmdText);
}
catch(_com_error *e){
{
AfxMessageBox(e->ErrorMessage());
return;
}
_variant_t var;
CString %%2="";
long fldc=m_pSet->GetFields()->GetCount();
long i=0;
try {
m_pSet->MoveFirst();
if(!m_pSet->adoEOF)
{
for(i=0;i<fldc;i++)
{
var=m_pSet->GetCollect((long)i);
var.ChangeType(VT_BSTR);
%%2+=var.bstrVal;
%%2+=" ";
}
//m_pSet->MoveNext();
}
}
catch(_com_error *e){
AfxMessageBox(e->ErrorMessage());
delete e;
}
//m_pSet->MoveFirst();
CoUninitialize(NULL);
2.单值比较
//#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
//no_namespace,rename("EOF","adoEOF"),named_guids
CoInitialize(NULL);
_bstr_t varSource="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=*.mdb";
//_bstr_t varSource="Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"
_ConnectionPtr m_pConnection(_uuidof(Connection));
m_pConnection->Open(varSource,"","",adModeUnknow);
_RecordsetPtr m_pSet(_uuid(Recordset));
try {
m_pSet->Open(%%1,m_pConnection.GetInterfacePtr()
adOpenDynamic,adLockPessimistic,adCmdText);
}
catch(_com_error *e){
{
AfxMessageBox(e->ErrorMessage());
return;
}
_variant_t var;
try {
m_pSet->MoveFirst();
if(!m_pSet->adoEOF)
{
var=m_pSet->GetCollect((long)0);
var.ChangeType(VT_I2);
int %%3=var.intVal;
if(%%3==%%4)
{
%%5
}
//m_pSet->MoveNext();
}
catch(_com_error *e){
AfxMessageBox(e->ErrorMessage());
delete e;
}
//m_pSet->MoveFirst();
CoUninitialize(NULL);
3.显示表格
//#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
//no_namespace,rename("EOF","adoEOF"),named_guids
CoInitialize(NULL);
_bstr_t varSource="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=*.mdb";
//_bstr_t varSource="Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"
_ConnectionPtr m_pConnection(_uuidof(Connection));
m_pConnection->Open(varSource,"","",adModeUnknow);
//打开属性为默认(adModeRead(只读),adModeWrite(可写),adModeReadWrite(可读写)等)
_RecordsetPtr m_pSet(_uuid(Recordset));
try {
HRESULT hr=m_pSet->Open(%%1,m_pConnection.GetInterfacePtr(),
adOpenDynamic,adLockPessimistic,adCmdText);
}
catch(_com_error *e){
AfxMessageBox(e->ErrorMessage());
}
if(SUCCESSED(hr))
{
//表打开成功
}
FieldsPtr p_fields=m_pSet->Fields;
FieldPtr p_field;
_variant_t var_index;
LPCSTR field_name;
int index=0;
_bstr_t bstr_field_name;
int countfields=p_fields->GetCount();
CString *Column=new CString[countfields];
CListCtrl *pList=(CListCtrl*)GetDlgItem(%%1);//IDC_LIST_TABLEDATA
VERIFY(pList);
pList->DeleteAllItems();
for(index=0;index<countfields;index++)
{
var_index.vt=VT_I4;
var_index.IVal=index;
p_field=p_fields->Item[var_index];
bstr_field_name=p_field->GetName();
field_name=(LPCSTR)bstr_field_name;
Column[index]=field_name;
int ColumnWidth=Column[index].GetLength()*15;
pList->InsertColumn(index,field_name,LVCFMT_CENTER,ColumnWidth);
}
int i=0;
_bstr_t vCol;
//pList->SetTextBkColor(RGB(122,200,122));
//pList->SetTextColor(RGB(0,0,200));
while(!m_pSet->adoEOF)
{
pList->Insert(i,atoi(i));
for(int j=0;j<countfields;j++)
{
vCol=m_pSet->GetCollect((long)j);
pList->SetItemText(i,j,vCol);
}
m_pSet->MoveNext();
i++;
}
CoUninitialize(NULL);
3----------------------------------------------
BOOL CRoominfo::OnInitDialog()
{
CDialog::OnInitDialog();
m_roomlist.InsertColumn(0,"房间号",LVCFMT_LEFT,60);
m_roomlist.InsertColumn(1,"房间类别",LVCFMT_LEFT,78);
m_roomlist.InsertColumn(2,"房间面积",LVCFMT_LEFT,78);
m_roomlist.InsertColumn(3,"床位数",LVCFMT_LEFT,60);
m_roomlist.InsertColumn(4,"空调",LVCFMT_LEFT,45);
m_roomlist.InsertColumn(5,"地毯",LVCFMT_LEFT,45);
m_roomlist.InsertColumn(6,"电话",LVCFMT_LEFT,45);
m_roomlist.InsertColumn(7,"电视",LVCFMT_LEFT,45);
m_roomlist.InsertColumn(8,"卫生间",LVCFMT_LEFT,60);
m_roomlist.InsertColumn(9,"价格",LVCFMT_LEFT,74);
RECT rect;
m_roomlist.GetWindowRect(&rect);
m_roomlist.SetExtendedStyle(LVS_EX_FULLROWSELECT);
RefreshData();
return TRUE;
}
void CRoominfo::RefreshData()
{
if(!db.IsOpen())
{
db.OpenEx(_T("DSN=Hotel;UID=YILING"),CDatabase::noOdbcDialog);
}
m_roomlist.DeleteAllItems();
CRominfoset m_roomset(&db);
m_roomset.Open(AFX_DB_USE_DEFAULT_TYPE,"SELECT * FROM [RoomInfor]");
if(m_roomset.GetRecordCount()!=0)
m_roomset.MoveFirst();
int i=0;
while(!m_roomset.IsEOF())
{
int temp=0;
CString str;
m_roomlist.InsertItem(i,"");
str.Format("%s",m_roomset.m_RoomID);
m_roomlist.SetItemText(i,0,str);
str.Format("%s",m_roomset.m_RoomType);
m_roomlist.SetItemText(i,1,str);
str.Format("%f",m_roomset.m_Area);
m_roomlist.SetItemText(i,2,str);
str.Format("%d",m_roomset.m_Bednum);
m_roomlist.SetItemText(i,3,str);
str.Format("%s",m_roomset.m_Aircondition);
m_roomlist.SetItemText(i,4,str);
str.Format("%s",m_roomset.m_Carpet);
m_roomlist.SetItemText(i,5,str);
str.Format("%s",m_roomset.m_Telephone);
m_roomlist.SetItemText(i,6,str);
str.Format("%s",m_roomset.m_Television);
m_roomlist.SetItemText(i,7,str);
str.Format("%s",m_roomset.m_Toilet);
m_roomlist.SetItemText(i,8,str);
str.Format("%s",m_roomset.m_Price);
m_roomlist.SetItemText(i,9,str);
m_roomset.MoveNext();
i++;
}
m_roomset.Close();
db.Close();
}
BEGIN_MESSAGE_MAP(CRoominfo, CDialog)
//{{AFX_MSG_MAP(CRoominfo)
ON_BN_CLICKED(IDC_ADDROOM, OnAddroom)
ON_BN_CLICKED(IDC_DELROOM, OnDelroom)
ON_BN_CLICKED(IDCHANGE, OnChange)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRoominfo message handlers
void CRoominfo::OnAddroom()
{
// TODO: Add your control notification handler code here
CAddroom addroom;
if(addroom.DoModal()==IDOK)
{
CString sql;
CAddroomset m_user;
if(!db.IsOpen())
{
db.OpenEx(_T("DSN=Hotel;UID=YILING"),CDatabase::noOdbcDialog);
}
m_user.Open(AFX_DB_USE_DEFAULT_TYPE,"SELECT * FROM [RoomInfor]");
sql.Format("insert into [RoomInfor] values(\'%s\',\'%s\',%f,%d,\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',%f)",addroom.m_roomid,addroom.m_roomtype,addroom.m_area,addroom.m_bednum,addroom.m_air,addroom.m_carpet,addroom.m_tel,addroom.m_tv,addroom.m_wc,addroom.m_price);
db.ExecuteSQL(sql);
db.Close();
RefreshData();
}
}
void CRoominfo::OnDelroom()
{
// TODO: Add your control notification handler code here
int i;
CString str;
CRominfoset m_user;
i=m_roomlist.GetSelectionMark();
str=m_roomlist.GetItemText(i,0);
m_user.Open(AFX_DB_USE_DEFAULT_TYPE,"SELECT * FROM [RoomInfor] WHERE [RoomID]='"+str+"'");
m_user.Delete();
m_user.Close();
RefreshData();
}
void CRoominfo::OnChange()
{
// TODO: Add your control notification handler code here
int i;
CString str;
i=m_roomlist.GetSelectionMark();
str=m_roomlist.GetItemText(i,0);
CModroominfo dlg;
if(dlg.DoModal()==IDOK)
{
CString strsql;
db.OpenEx(_T("DSN=Hotel;UID=YILING"),CDatabase::noOdbcDialog);
strsql.Format("update [RoomInfor] set [RoomID]='%s',[RoomType]='%s',[Area]='%f',[Bednum]='%d',[Aircondition]='%s',[Carpet]='%s',[Telephone]='%s',[Television]='%s',[Toilet]='%s',[Price]='%f' where [RoomID]='%s'",dlg.m_id,dlg.m_type,dlg.m_area,dlg.m_bednum,dlg.m_air,dlg.m_carpet,dlg.m_tel,dlg.m_tv,dlg.m_wc,dlg.m_price,str);
db.ExecuteSQL(strsql);
db.Close();
}
RefreshData();
}