何志丹
1,利用SQL词句理论上可以使用任何数据库,以sql为例.标签:dlg,自定义,rs,CString,db,CSetODBC,strTemp,strSQL,使用 From: https://blog.51cto.com/u_15724537/5732441
body.clientHeight)this.width=body.clientHeight" src="http://www.3.com/VCShare/images/upfile/200461521239.jpg" notallow="return yuzi_img(event,this)">
2,使用方法
void CTestODBCDlg::OnUseSql()
{
CSetODBC dlg;
dlg.SetFileName( "f://setodbc.con");
dlg.SetAppName("test");
const CString strConnect = dlg.GetConnectStr();//获得连接串
CString strSQL;
CDatabase db;
db.OpenEx(strConnect);//打开数据库
{//追加记录
strSQL = "insert into character(Name,Country,Age) values(/'he/',/'china/',24)";
db.ExecuteSQL(strSQL);
}
{//删除记录
strSQL.Format("delete from character where Age = %d",24);
db.ExecuteSQL(strSQL);
}
{//修改记录
strSQL.Format("update character set age = 25 where name = /'he/'");
db.ExecuteSQL(strSQL);
}
//显示所有记录
{
CString strOutPut ;
CRecordset rs(&db);
rs.Open(CRecordset::snapshot,"select * from character");
int nField = rs.GetODBCFieldCount();
while(!rs.IsEOF())
{
for(int i = 0 ; i < nField ; i++)
{
CString strTemp;
rs.GetFieldValue((short)i,strTemp);
strTemp.TrimLeft();
strTemp.TrimRight();
strOutPut +=(strTemp + '/t');
}
strOutPut += "/n";
rs.MoveNext();
}
rs.Close();
AfxMessageBox(strOutPut);
}
db.Close();
}
3,相关信息记录在f://setodbc.con,其内容为:
[test]
DSN=testodbc_sql
UID=sa
PWD=
4,如果用户想重新设置ODBC,则:
void CTestODBCDlg::OnResetOdbc()
{
CSetODBC dlg;
dlg.SetFileName( "f://setodbc.con");
dlg.SetAppName("test");
const CString strConnect = dlg.GetConnectStr(true);//重新设置ODBC
}