如何把vc中已读出的信息存入到数据库中?
做课程设计的时候,遇到这一步不懂,望详细指教.
首先,我做了一个读写ini的界面后,希望把读到的信息存入到数据库中进行保存,下面是我的读和写代码:
读
void CWuDlg::OnButton1()
{// TODO: Add your control notification handler code here
char appPath[256];
GetCurrentDirectory(256,appPath);
CString FilePath;
FilePath.Format("%s",appPath);
FilePath+="\\Test_ini.ini";
CString strSection="Section1";
CString strSectionKey="Item1";
CString strValue=_T("");
char inBuf[80];
GetPrivateProfileString(strSection,strSectionKey,NULL,inBuf,80,FilePath);
strValue=inBuf;
SetDlgItemText(IDC_EDIT1,strValue);
strSection="Section2";
strSectionKey="Item2";
strValue=_T("");
GetPrivateProfileString(strSection,strSectionKey,NULL,inBuf,80,FilePath);
strValue=inBuf;
SetDlgItemText(IDC_EDIT2,strValue);
strSection="Section3";
strSectionKey="Item3";
strValue=_T("");
GetPrivateProfileString(strSection,strSectionKey,NULL,inBuf,80,FilePath);
strValue=inBuf;
SetDlgItemText(IDC_EDIT3,strValue);
strSection="Section4";
strSectionKey="Item1";
strValue=_T("");
GetPrivateProfileString(strSection,strSectionKey,NULL,inBuf,80,FilePath);
strValue=inBuf;
SetDlgItemText(IDC_EDIT4,strValue);
strSectionKey="Item2";
strValue=_T("");
GetPrivateProfileString(strSection,strSectionKey,NULL,inBuf,80,FilePath);
strValue=inBuf;
int num=0;
atoi(inBuf);
SetDlgItemInt(IDC_EDIT5,num,true);
strSectionKey="Item3";
strValue=_T("");
GetPrivateProfileString(strSection,strSectionKey,NULL,inBuf,80,FilePath);
strValue=inBuf;
SetDlgItemText(IDC_EDIT6,strValue);
}
写
void CWuDlg::OnButton2()
{
// TODO: Add your control notification handler code here
char appPath[256];
GetCurrentDirectory(256,appPath);
CString FilePath;
FilePath.Format("%s",appPath);
FilePath+="\\Test_ini.ini";
CString strSection="Section1";
CString strSectionKey="Item1";
CString strValue=_T("");
GetDlgItemText(IDC_EDIT1,strValue);
WritePrivateProfileString(strSection,strSectionKey,strValue,FilePath);
strSection="Section2";
strSectionKey="Item2";
strValue=_T("");
GetDlgItemText(IDC_EDIT2,strValue);
WritePrivateProfileString(strSection,strSectionKey,strValue,FilePath);
strSection="Section3";
strSectionKey="Item3";
strValue=_T("");
GetDlgItemText(IDC_EDIT3,strValue);
WritePrivateProfileString(strSection,strSectionKey,strValue,FilePath);
strSection="Section4";
strSectionKey="Item1";
strValue=_T("");
GetDlgItemText(IDC_EDIT4,strValue);
WritePrivateProfileString(strSection,strSectionKey,strValue,FilePath);
strSectionKey="Item2";
int num=0;
num=GetDlgItemInt(IDC_EDIT5);
strValue.Format("%d",num);
WritePrivateProfileString(strSection,strSectionKey,strValue,FilePath);
strSectionKey="Item3";
strValue=_T("");
GetDlgItemText(IDC_EDIT6,strValue);
WritePrivateProfileString(strSection,strSectionKey,strValue,FilePath);
}
问题:如何把我读出来的信息写入到数据库中??能详细说明吗?数据库这关真难倒我了,没有这方面的基础,望详细告诉我如何做.谢谢!!
参考答案:你这个是注册表/配置文件的操作,和数据库没关系。
进行数据库操作,你需要建立一个数据库连接,然后通过执行SQL语句实现操作。
现在比较常用的方式就是ADO了。
具体的语法可以(注意strSRC根据你的数据库不同要修改):
1、引入ADO库文件
使用ADO前必须在工程的stdafx.h头文件里用直接引入符号#import引入ADO库文件,以使编译器能正确编译。代码如下所示:
用#import引入ADO库文件
#import "c:\program files\common files\system\ado\msado15.dll"no_namespaces rename("EOF" adoEOF")
这行语句声明在工程中使用ADO,但不使用ADO的名字空间,并且为了避免常数冲突,将常数EOF改名为adoEOF。现在不需添加另外的头文件,就可以使用ADO接口了。
2、初始化OLE/COM库环境
必须注意的是,ADO库是一组COM动态库,这意味应用程序在调用ADO前,必须初始化OLE/COM库环境。在MFC应用程序里,一个比较好的方法是在应用程序主类的InitInstance成员函数里初始化OLE/COM库环境。
BOOL CMyAdoTestApp::InitInstance()
{
if(!AfxOleInit())//这就是初始化COM库
{
AfxMessageBox(“OLE初始化出错!”);
return FALSE;
}
……
}
_ConnectionPtr pConn;
if (FAILED(pConn.CreateInstance("ADODB.Connection")))
{
AfxMessageBox("Create Instance failed!");
return;
}
CString strSRC;
strSRC="Driver=SQL Server;Server=";
strSRC+="suppersoft";
strSRC+=";Database=";
strSRC+="mydb";
strSRC+=";UID=SA;PWD=";
CString strSQL = "Insert into student(no,name,sex,address) values(3,'aaa','male','beijing')";
_variant_t varSRC(strSRC);
_variant_t varSQL(strSQL);
_bstr_t bstrSRC(strSRC);
if (FAILED(pConn->Open(bstrSRC,"","",-1)))
{
AfxMessageBox("Can not open Database!");
pConn.Release();
return;
}
COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
pConn->Execute(_bstr_t(strSQL),&vtOptional,-1);
pConn.Release();
AfxMessageBox("ok!");
5、使用_RecordsetPtr接口(以连接SQL Server为例)
_RecordsetPtr pPtr;
if (FAILED(pPtr.CreateInstance("ADODB.Recordset")))
{
AfxMessageBox("Create Instance failed!");
return FALSE;
}
CString strSRC;
strSRC="Driver=SQL Server;Server=";
strSRC+="210.46.141.145";
strSRC+=";Database=";
strSRC+="mydb";
strSRC+=";UID=sa;PWD=";
strSRC+="sa";
CString strSQL = "select id,name,gender,address from personal";
_variant_t varSRC(strSRC);
_variant_t varSQL(strSQL);
if(FAILED(pPtr->Open(varSQL,varSRC,adOpenStatic,adLockOptimistic,adCmdText)))
{
AfxMessageBox("Open table failed!");
pPtr.Release();
return FALSE;
}
while(!pPtr->GetadoEOF())
{
_variant_t varNo;
_variant_t varName;
_variant_t varSex;
_variant_t varAddress;
varNo = pPtr->GetCollect ("id");
varName = pPtr->GetCollect ("name");
varSex = pPtr->GetCollect ("gender");
varAddress = pPtr->GetCollect ("address");
CString strNo =(char *)_bstr_t(varNo);
CString strName =(char *)_bstr_t(varName);
CString strSex =(char *)_bstr_t(varSex);
CString strAddress =(char *)_bstr_t(varAddress);
strNo.TrimRight();
strName.TrimRight();
strSex.TrimRight();
strAddress.TrimRight();
int nCount = m_list.GetItemCount();
int nItem = m_list.InsertItem (nCount,_T(""));
m_list.SetItemText (nItem,0,strNo);
m_list.SetItemText (nItem,1,strName);
m_list.SetItemText (nItem,2,strSex);
m_list.SetItemText (nItem,3,strAddress);
pPtr->MoveNext();
}
pPtr->Close();
pPtr.Release();