📄 linkdb.cpp
字号:
// LinkDB.cpp : implementation file
//
#include "stdafx.h"
#include "rset.h"
#include <fstream>
#include "LinkDB.h"
#include <afxdb.h>
#include <string.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// LinkDB dialog
LinkDB::LinkDB(CWnd* pParent /*=NULL*/)
: CDialog(LinkDB::IDD, pParent)
{
strcpy(OutPath,"");
// GetDlgItem(IDC_SELECT_SQL)->EnableWindow(FALSE);
// GetDlgItem(IDC_EDIT1)->EnableWindow(FALSE);
//{{AFX_DATA_INIT(LinkDB)
m_table = _T("");
m_sql = _T("");
m_bCheck = TRUE;
//}}AFX_DATA_INIT
}
void LinkDB::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(LinkDB)
DDX_Text(pDX, IDC_TABLE, m_table);
DDX_Text(pDX, IDC_EDIT1, m_sql);
DDX_Check(pDX, IDC_SELECT_SQL, m_bCheck);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(LinkDB, CDialog)
//{{AFX_MSG_MAP(LinkDB)
ON_BN_CLICKED(IDC_CONNECT_DB, OnConnectDb)
ON_BN_CLICKED(IDC_SELECT_SQL, OnSelectDb)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// LinkDB message handlers
void LinkDB::OnSelectDb()
{
if(m_bCheck)
{
GetDlgItem(IDC_EDIT1)->EnableWindow(FALSE);
m_bCheck=0;
}
else
{
GetDlgItem(IDC_EDIT1)->EnableWindow(TRUE);
m_bCheck=1;
}
}
void LinkDB::OnConnectDb()
{//连接数据库,转化为文本
UpdateData(true);
CString strTableName="";
strTableName=m_table;
if(strTableName=="")
{
pDb->Close();//关闭数据库
AfxMessageBox("请输入数据表名称");
return;
}
//连接数据源,统计并显示相关信息
BOOL bStatus=false;//数据库是否成功打开标志
pDb->SetLoginTimeout(10); //设置连接超时属性值为10
try//连接数据库ODBC
{
bStatus=pDb->OpenEx(NULL);//打开对话框
if(bStatus)
{
TRACE("\n DB opened successfully");
// AfxMessageBox("数据源连接成功");
}
else
{
TRACE("\n open dsn failed\n");
AfxMessageBox("连接失败");
}
}
catch(CMemoryException *pEx)//处理内存错误
{
pDb->Close();//关闭数据库
pEx->ReportError();
}
catch(CDBException *pDBEx)//处理数据库异常
{
pDb->Close();//关闭数据库
pDBEx->ReportError();
TRACE("RetCode:%d strError:[%s] strStatus:[%s]\n",
pDBEx->m_nRetCode,
pDBEx->m_strError,
pDBEx->m_strStateNativeOrigin);
}
CRecordset Rst(pDb);//新建记录,指向数据表
if(m_sql=="")
{
m_sql="select * from "+strTableName;
}
BOOL flag=false;
try
{
flag=Rst.Open(CRecordset::dynaset,m_sql);//初始化数据表
}
catch(CDBException* e)
{
pDb->Close();//关闭数据库
e->Delete();
AfxMessageBox("数据库中没有发现数据表:"+strTableName);
return;
}
if (flag)
AfxMessageBox("数据表连接成功");
else
{
pDb->Close();//关闭数据库
return;
}
//转化为文本文件,扫描两次数据表
iAttNumber=Rst.GetODBCFieldCount()-1;//获得条件属性个数,(减掉ID,和决策属性)
iConNum=iAttNumber;//获得条件属性个数
//获取记录条数
int i;
CString *name=new CString [iConNum+1];
CODBCFieldInfo fieldinfo;
SWORD *style=new SWORD [iConNum+1];
for(i=0;i<iConNum+1;i++)
{
Rst.GetODBCFieldInfo(i,fieldinfo);
name[i]=fieldinfo.m_strName;
style[i]=fieldinfo.m_nSQLType;
}
iRecNum=0;
Rst.MoveFirst();
while(!Rst.IsEOF())
{
iRecNum++;
Rst.MoveNext();
}
recCount=iRecNum;
/////////////输出到文本文件
CString str="C:\\DOCUME~1\\ADMINI~1\\LOCALS~1\\Temp\\"+strTableName+".txt";
strcpy(OutPath,str.GetBuffer(200));
fout.open(OutPath,ios::out|ios::trunc); //新建、打开文件
int j;//循环变量
int tempcount=0; //以下写文件头
//输出文件头
fout<<"Style:train"<<'\n'; //表示为规则文件
fout<<"Stage:0"<<'\n'; //为规则文件这个值无用
fout<<"Condition attributes number:"<<iAttNumber<<'\n';//条件属性个数
fout<<"Records number: "<<iRecNum<<'\n';//行数
for(j=0;j<=iAttNumber;j++)
{
fout<<name[j]<<' ';
}
fout<<'\n';
for(j=0;j<iAttNumber;j++)
{
// fout<<"Float"<<' ';
if((style[j]==-6)||(style[j]==3)||(style[j]==4)||(style[j]==5))
fout<<"Integer"<<' ';
else if ((style[j]==2)||(style[j]==6)||(style[j]==7)||(style[j]==8))
fout<<"Float"<<' ';
else fout<<"String"<<' ';
}
fout<<"Integer"<<' ';
fout<<'\n';
//以下写文件数据
Rst.MoveFirst();
CString strValue;
while(!Rst.IsEOF())
{
j=0;
while(j<Rst.GetODBCFieldCount())
{
Rst.GetFieldValue(j,strValue);
fout<<strValue<<" ";
j++;
}
fout<<'\n';
Rst.MoveNext();
}
//写入文件尾部 while(!fin)
fout.close();//关闭文件
Rst.Close();//关闭数据表
pDb->Close();//关闭数据库
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -