描述:
void CMyDlg1::OnButton2()
{ //void GetWindowText(CString&rString)const;
CString bianliang;
m_edit.GetWindowText(bianliang);
CString sql;
sql.Format("select * from 表3 where 节点编号>%d",bianliang);
m_adodc.SetRecordSource(sql);
m_adodc.Refresh();
m_grid.SetRefDataSource(m_adodc.GetControlUnknown());
}
OnButton2()是“查询”按钮的事件,下面两句是“EDIT”控件类CEdit提供的GetWindowText方法,是将Edit控件中的内容存到“bianliang”中。
后三句是错误的,怎么改可以显示,请赐教,谢谢!
m_adodc是microsoft ado data控件IDC_ADODC的成员变量,
m_grid是microsoft data grid控件IDC_DATAGRID的成员变量。
解决方案1:
你是用ADO查询数据库吧,给你个ADO的例子,然后你再把得到记录集赋给m_grid变量
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename ("EOF", "adoEOF")
头文件里类里:
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
_CommandPtr m_pCommand;
CPP文件里:
BOOL CDatabaseDll::OpenDatabaseFun(_ConnectionPtr pConnection,_RecordsetPtr pRecordset,_CommandPtr pCommand)
{
BOOL Result=TRUE;
HRESULT hr;
try
{
hr = pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
pConnection->ConnectionTimeout = 10;
// hr = m_pConnection->Open( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\NW.mdb","", "", adModeUnknown);//这是OFFICE2003以下的
hr = pConnection->Open("Provider=Microsoft.Ace.Oledb.12.0;Data Source=D:\\bagayalu.accdb;Jet OLEDB:Database Password=123456","", "", adModeUnknown);//这是OFFICE2007以上的,包括OFFICE2010,这个连接字符串就是在OFFICE2010下用的,没问题
//m_pConnection->PutDefaultDatabase ((_bstr_t)"DB");//设置默认数据库
pCommand.CreateInstance(__uuidof(Command));
pCommand->CommandTimeout = 5;
pCommand->ActiveConnection = pConnection;
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!/r/n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
Result=FALSE;
return Result;
}
_variant_t var;
float v1,v2,v3,v4;
CString Remark;
pRecordset.CreateInstance(__uuidof(Recordset));
try
{
pRecordset->Open("SELECT * FROM 表1",// 查询DemoTable表中所有字段
pConnection.GetInterfacePtr(),// 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
while(!m_pRecordset->adoEOF)
{
var = m_pRecordset->GetCollect("v1");
if(var.vt != VT_NULL)
v1=var.fltVal;
// strName = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("v2");
if(var.vt != VT_NULL)
v2=var.fltVal;
var = m_pRecordset->GetCollect("v3");
if(var.vt != VT_NULL)
v3=var.fltVal;
var = m_pRecordset->GetCollect("v4");
if(var.vt != VT_NULL)
v4=var.fltVal;
var = m_pRecordset->GetCollect("remark");
if(var.vt != VT_NULL)
Remark = (LPCSTR)_bstr_t(var);
m_pRecordset->MoveNext();
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
Result=FALSE;
return Result;
}
return Result
你用的是GridCtrl?
这有个例子
看看能不能参考一下
http://www.happyxiazai.com/source/4705769