我的Access 2000数据表temp中有这样几种类型的字段:
FieldName Type
1. index long
2. name char
3. salary CCurrency
4. student true/false
我在对话框中分别定义了四个编辑框的自动更新变量:
long m_nIndex;
CString m_strName;
CCurrency m_cySalary;
bool m_bStudent;
怎样才能用这四个变量的值写入SQL语句中,从而用Execute插入数据表中?谢谢了。
8 个解决方案
#1
很简单的啊
#2
try
{
hr=m_pCommand.CreateInstance(__uuidof(Command));
if(SUCCEEDED(hr))
{
m_pCommand->ActiveConnection=m_pConnection;
m_pCommand->CommandText="SELECT word_desc from word_table";
m_pCommand->CommandType=adCmdText;
m_pCommand->Parameters->Refresh();
m_pRecordset=m_pCommand->Execute(NULL,NULL,adCmdUnknown);
}
}
catch(_com_error e)
{
MessageBox("Failed to created the command object!");
return ;
}
{
hr=m_pCommand.CreateInstance(__uuidof(Command));
if(SUCCEEDED(hr))
{
m_pCommand->ActiveConnection=m_pConnection;
m_pCommand->CommandText="SELECT word_desc from word_table";
m_pCommand->CommandType=adCmdText;
m_pCommand->Parameters->Refresh();
m_pRecordset=m_pCommand->Execute(NULL,NULL,adCmdUnknown);
}
}
catch(_com_error e)
{
MessageBox("Failed to created the command object!");
return ;
}
#3
void CTestAdoDlg::OnButton1()
{
// TODO: Add your control notification handler code here
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
_CommandPtr m_pCommand;
HRESULT hr;
try
{
hr=m_pConnection.CreateInstance(__uuidof(Connection));
if(SUCCEEDED(hr))
{
//Not use DSN
// hr=m_pConnection->Open("Driver={SQL Server};Database=csdn_db;Server=127.0.0.1\\psusong;",
// "","",adModeUnknown);
//Use DSN
hr=m_pConnection->Open("DSN=Test_Word;UID=sa;PWD=psusong","","",-1);
}
if(SUCCEEDED(hr))
{
hr=m_pRecordset.CreateInstance(__uuidof(Recordset));
CString str="SELECT * FROM word_table";
if(SUCCEEDED(hr))
{
m_pRecordset->Open((_variant_t)str,
//_variant_t((IDispatch*)m_pConnection,true),
m_pConnection.GetInterfacePtr(),
adOpenKeyset,
adLockPessimistic,
adCmdText);
this->m_DataGridCtrl.SetRefDataSource((LPUNKNOWN)m_pRecordset);
this->m_DataGridCtrl.Refresh();
}
}
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
return ;
}
_variant_t data_holder;
/* try
{
m_pRecordset->MoveFirst();
while(!m_pRecordset->adoEOF)
{
data_holder=m_pRecordset->GetCollect("word_name");
if(data_holder.vt!=VT_NULL)
{
this->m_ListBoxCtrl.AddString((char*)(_bstr_t)(data_holder));
m_pRecordset->MoveNext();
}
}
}
catch(_com_error e)
{
MessageBox("Occured error when fetch the record!");
rerturn;
}
*/
try
{
hr=m_pCommand.CreateInstance(__uuidof(Command));
if(SUCCEEDED(hr))
{
m_pCommand->ActiveConnection=m_pConnection;
m_pCommand->CommandText="SELECT word_desc from word_table";
m_pCommand->CommandType=adCmdText;
m_pCommand->Parameters->Refresh();
m_pRecordset=m_pCommand->Execute(NULL,NULL,adCmdUnknown);
}
}
catch(_com_error e)
{
MessageBox("Failed to created the command object!");
return ;
}
try
{
m_pRecordset->MoveFirst();
while(!m_pRecordset->adoEOF)
{
data_holder=m_pRecordset->GetCollect("word_desc");
if(data_holder.vt!=VT_NULL)
{
this->m_ListBoxCtrl.AddString((char*)(_bstr_t)(data_holder));
m_pRecordset->MoveNext();
}
}
}
catch(_com_error e)
{
MessageBox("Occured error when fetch the record!");
return ;
}
m_pCommand.Release();
m_pRecordset.Release();
m_pConnection.Release();
}
{
// TODO: Add your control notification handler code here
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
_CommandPtr m_pCommand;
HRESULT hr;
try
{
hr=m_pConnection.CreateInstance(__uuidof(Connection));
if(SUCCEEDED(hr))
{
//Not use DSN
// hr=m_pConnection->Open("Driver={SQL Server};Database=csdn_db;Server=127.0.0.1\\psusong;",
// "","",adModeUnknown);
//Use DSN
hr=m_pConnection->Open("DSN=Test_Word;UID=sa;PWD=psusong","","",-1);
}
if(SUCCEEDED(hr))
{
hr=m_pRecordset.CreateInstance(__uuidof(Recordset));
CString str="SELECT * FROM word_table";
if(SUCCEEDED(hr))
{
m_pRecordset->Open((_variant_t)str,
//_variant_t((IDispatch*)m_pConnection,true),
m_pConnection.GetInterfacePtr(),
adOpenKeyset,
adLockPessimistic,
adCmdText);
this->m_DataGridCtrl.SetRefDataSource((LPUNKNOWN)m_pRecordset);
this->m_DataGridCtrl.Refresh();
}
}
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
return ;
}
_variant_t data_holder;
/* try
{
m_pRecordset->MoveFirst();
while(!m_pRecordset->adoEOF)
{
data_holder=m_pRecordset->GetCollect("word_name");
if(data_holder.vt!=VT_NULL)
{
this->m_ListBoxCtrl.AddString((char*)(_bstr_t)(data_holder));
m_pRecordset->MoveNext();
}
}
}
catch(_com_error e)
{
MessageBox("Occured error when fetch the record!");
rerturn;
}
*/
try
{
hr=m_pCommand.CreateInstance(__uuidof(Command));
if(SUCCEEDED(hr))
{
m_pCommand->ActiveConnection=m_pConnection;
m_pCommand->CommandText="SELECT word_desc from word_table";
m_pCommand->CommandType=adCmdText;
m_pCommand->Parameters->Refresh();
m_pRecordset=m_pCommand->Execute(NULL,NULL,adCmdUnknown);
}
}
catch(_com_error e)
{
MessageBox("Failed to created the command object!");
return ;
}
try
{
m_pRecordset->MoveFirst();
while(!m_pRecordset->adoEOF)
{
data_holder=m_pRecordset->GetCollect("word_desc");
if(data_holder.vt!=VT_NULL)
{
this->m_ListBoxCtrl.AddString((char*)(_bstr_t)(data_holder));
m_pRecordset->MoveNext();
}
}
}
catch(_com_error e)
{
MessageBox("Occured error when fetch the record!");
return ;
}
m_pCommand.Release();
m_pRecordset.Release();
m_pConnection.Release();
}
#4
谢谢二位,可能我说的不太明白,我是想用已定义的四个变量的值用insert into语句写入表中,这四个变量在SQL语句中怎样变换并表示出来。是用参数还是用CString的Format()?该如何写?
#5
没错.,,用CString的Format
类似于这句
char* UserName;
char* PassWord;
CString sql;
sql.Format("select baccauid from members where baccauid = '%s' and baccapwd = '%s'",UserName,PassWord);
记住字符串用%s,整数用%d...相应的可查msdn
类似于这句
char* UserName;
char* PassWord;
CString sql;
sql.Format("select baccauid from members where baccauid = '%s' and baccapwd = '%s'",UserName,PassWord);
记住字符串用%s,整数用%d...相应的可查msdn
#6
char str[256]={0};
sprintf(str,"Insert into tablename values(%d,\'%s\',%d,true", m_nIndex,m_strName,m_cySalary)
str就是你的运行是的参数执行注意首先判断一下布尔值上面的为true
上面的写法还有另外一种形式
insert into tablename(fieldname1,fieldname2...) values(....)
这时的值为一一对应的而insert into tablename values(..)必须为表中定义的字段的顺序否则回出错误
sprintf(str,"Insert into tablename values(%d,\'%s\',%d,true", m_nIndex,m_strName,m_cySalary)
str就是你的运行是的参数执行注意首先判断一下布尔值上面的为true
上面的写法还有另外一种形式
insert into tablename(fieldname1,fieldname2...) values(....)
这时的值为一一对应的而insert into tablename values(..)必须为表中定义的字段的顺序否则回出错误
#7
重点是把查询的字符串转为bstr,用
USES_CONVERSION: BSTR bstr=SysAllocString(A2W(lpstr));
可以,至于用语句组织SQL的插入查询(insert into table (fields) values())自己用CString生成该没问题的。
USES_CONVERSION: BSTR bstr=SysAllocString(A2W(lpstr));
可以,至于用语句组织SQL的插入查询(insert into table (fields) values())自己用CString生成该没问题的。
#8
谢谢大家的帮助,我基本明白了,并且也会用了,再次感谢!
#1
很简单的啊
#2
try
{
hr=m_pCommand.CreateInstance(__uuidof(Command));
if(SUCCEEDED(hr))
{
m_pCommand->ActiveConnection=m_pConnection;
m_pCommand->CommandText="SELECT word_desc from word_table";
m_pCommand->CommandType=adCmdText;
m_pCommand->Parameters->Refresh();
m_pRecordset=m_pCommand->Execute(NULL,NULL,adCmdUnknown);
}
}
catch(_com_error e)
{
MessageBox("Failed to created the command object!");
return ;
}
{
hr=m_pCommand.CreateInstance(__uuidof(Command));
if(SUCCEEDED(hr))
{
m_pCommand->ActiveConnection=m_pConnection;
m_pCommand->CommandText="SELECT word_desc from word_table";
m_pCommand->CommandType=adCmdText;
m_pCommand->Parameters->Refresh();
m_pRecordset=m_pCommand->Execute(NULL,NULL,adCmdUnknown);
}
}
catch(_com_error e)
{
MessageBox("Failed to created the command object!");
return ;
}
#3
void CTestAdoDlg::OnButton1()
{
// TODO: Add your control notification handler code here
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
_CommandPtr m_pCommand;
HRESULT hr;
try
{
hr=m_pConnection.CreateInstance(__uuidof(Connection));
if(SUCCEEDED(hr))
{
//Not use DSN
// hr=m_pConnection->Open("Driver={SQL Server};Database=csdn_db;Server=127.0.0.1\\psusong;",
// "","",adModeUnknown);
//Use DSN
hr=m_pConnection->Open("DSN=Test_Word;UID=sa;PWD=psusong","","",-1);
}
if(SUCCEEDED(hr))
{
hr=m_pRecordset.CreateInstance(__uuidof(Recordset));
CString str="SELECT * FROM word_table";
if(SUCCEEDED(hr))
{
m_pRecordset->Open((_variant_t)str,
//_variant_t((IDispatch*)m_pConnection,true),
m_pConnection.GetInterfacePtr(),
adOpenKeyset,
adLockPessimistic,
adCmdText);
this->m_DataGridCtrl.SetRefDataSource((LPUNKNOWN)m_pRecordset);
this->m_DataGridCtrl.Refresh();
}
}
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
return ;
}
_variant_t data_holder;
/* try
{
m_pRecordset->MoveFirst();
while(!m_pRecordset->adoEOF)
{
data_holder=m_pRecordset->GetCollect("word_name");
if(data_holder.vt!=VT_NULL)
{
this->m_ListBoxCtrl.AddString((char*)(_bstr_t)(data_holder));
m_pRecordset->MoveNext();
}
}
}
catch(_com_error e)
{
MessageBox("Occured error when fetch the record!");
rerturn;
}
*/
try
{
hr=m_pCommand.CreateInstance(__uuidof(Command));
if(SUCCEEDED(hr))
{
m_pCommand->ActiveConnection=m_pConnection;
m_pCommand->CommandText="SELECT word_desc from word_table";
m_pCommand->CommandType=adCmdText;
m_pCommand->Parameters->Refresh();
m_pRecordset=m_pCommand->Execute(NULL,NULL,adCmdUnknown);
}
}
catch(_com_error e)
{
MessageBox("Failed to created the command object!");
return ;
}
try
{
m_pRecordset->MoveFirst();
while(!m_pRecordset->adoEOF)
{
data_holder=m_pRecordset->GetCollect("word_desc");
if(data_holder.vt!=VT_NULL)
{
this->m_ListBoxCtrl.AddString((char*)(_bstr_t)(data_holder));
m_pRecordset->MoveNext();
}
}
}
catch(_com_error e)
{
MessageBox("Occured error when fetch the record!");
return ;
}
m_pCommand.Release();
m_pRecordset.Release();
m_pConnection.Release();
}
{
// TODO: Add your control notification handler code here
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
_CommandPtr m_pCommand;
HRESULT hr;
try
{
hr=m_pConnection.CreateInstance(__uuidof(Connection));
if(SUCCEEDED(hr))
{
//Not use DSN
// hr=m_pConnection->Open("Driver={SQL Server};Database=csdn_db;Server=127.0.0.1\\psusong;",
// "","",adModeUnknown);
//Use DSN
hr=m_pConnection->Open("DSN=Test_Word;UID=sa;PWD=psusong","","",-1);
}
if(SUCCEEDED(hr))
{
hr=m_pRecordset.CreateInstance(__uuidof(Recordset));
CString str="SELECT * FROM word_table";
if(SUCCEEDED(hr))
{
m_pRecordset->Open((_variant_t)str,
//_variant_t((IDispatch*)m_pConnection,true),
m_pConnection.GetInterfacePtr(),
adOpenKeyset,
adLockPessimistic,
adCmdText);
this->m_DataGridCtrl.SetRefDataSource((LPUNKNOWN)m_pRecordset);
this->m_DataGridCtrl.Refresh();
}
}
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
return ;
}
_variant_t data_holder;
/* try
{
m_pRecordset->MoveFirst();
while(!m_pRecordset->adoEOF)
{
data_holder=m_pRecordset->GetCollect("word_name");
if(data_holder.vt!=VT_NULL)
{
this->m_ListBoxCtrl.AddString((char*)(_bstr_t)(data_holder));
m_pRecordset->MoveNext();
}
}
}
catch(_com_error e)
{
MessageBox("Occured error when fetch the record!");
rerturn;
}
*/
try
{
hr=m_pCommand.CreateInstance(__uuidof(Command));
if(SUCCEEDED(hr))
{
m_pCommand->ActiveConnection=m_pConnection;
m_pCommand->CommandText="SELECT word_desc from word_table";
m_pCommand->CommandType=adCmdText;
m_pCommand->Parameters->Refresh();
m_pRecordset=m_pCommand->Execute(NULL,NULL,adCmdUnknown);
}
}
catch(_com_error e)
{
MessageBox("Failed to created the command object!");
return ;
}
try
{
m_pRecordset->MoveFirst();
while(!m_pRecordset->adoEOF)
{
data_holder=m_pRecordset->GetCollect("word_desc");
if(data_holder.vt!=VT_NULL)
{
this->m_ListBoxCtrl.AddString((char*)(_bstr_t)(data_holder));
m_pRecordset->MoveNext();
}
}
}
catch(_com_error e)
{
MessageBox("Occured error when fetch the record!");
return ;
}
m_pCommand.Release();
m_pRecordset.Release();
m_pConnection.Release();
}
#4
谢谢二位,可能我说的不太明白,我是想用已定义的四个变量的值用insert into语句写入表中,这四个变量在SQL语句中怎样变换并表示出来。是用参数还是用CString的Format()?该如何写?
#5
没错.,,用CString的Format
类似于这句
char* UserName;
char* PassWord;
CString sql;
sql.Format("select baccauid from members where baccauid = '%s' and baccapwd = '%s'",UserName,PassWord);
记住字符串用%s,整数用%d...相应的可查msdn
类似于这句
char* UserName;
char* PassWord;
CString sql;
sql.Format("select baccauid from members where baccauid = '%s' and baccapwd = '%s'",UserName,PassWord);
记住字符串用%s,整数用%d...相应的可查msdn
#6
char str[256]={0};
sprintf(str,"Insert into tablename values(%d,\'%s\',%d,true", m_nIndex,m_strName,m_cySalary)
str就是你的运行是的参数执行注意首先判断一下布尔值上面的为true
上面的写法还有另外一种形式
insert into tablename(fieldname1,fieldname2...) values(....)
这时的值为一一对应的而insert into tablename values(..)必须为表中定义的字段的顺序否则回出错误
sprintf(str,"Insert into tablename values(%d,\'%s\',%d,true", m_nIndex,m_strName,m_cySalary)
str就是你的运行是的参数执行注意首先判断一下布尔值上面的为true
上面的写法还有另外一种形式
insert into tablename(fieldname1,fieldname2...) values(....)
这时的值为一一对应的而insert into tablename values(..)必须为表中定义的字段的顺序否则回出错误
#7
重点是把查询的字符串转为bstr,用
USES_CONVERSION: BSTR bstr=SysAllocString(A2W(lpstr));
可以,至于用语句组织SQL的插入查询(insert into table (fields) values())自己用CString生成该没问题的。
USES_CONVERSION: BSTR bstr=SysAllocString(A2W(lpstr));
可以,至于用语句组织SQL的插入查询(insert into table (fields) values())自己用CString生成该没问题的。
#8
谢谢大家的帮助,我基本明白了,并且也会用了,再次感谢!