c语言关于从数据库读取数据写文件

c语言关于从数据库读取数据写文件,第1张

#include  <stdio.h>

exec sql include sqlca

int main(){

    exec sql begin declare section

        char userpasswd[30]="openlab/123456"

        struct{

            int  id

            char name[30]

            double salary

        }emp

    exec sql end   declare section

    exec sql connect:userpasswd

    exec sql declare empcursor cursor for 

        select id,first_name,salary  from 

        s_emp order by salary

    exec sql  open  empcursor

    exec sql  whenever notfound do break

    for(){

    exec sql  fetch empcursor into :emp

    printf("%d:%s:%lf\n",emp.id,emp.name,

                     emp.salary)

    }

    exec sql  close empcursor

    exec sql commit work release    

}

把数据存到结构体里。

没看懂lz想表达什么意思?

lz举个例子先。。

----------------

还是没看懂。lz不是自定义了的数据库文件格式么,那全部按照自定义的结构来读写该文件就行了。

您是说的“载入资源函数”的输入参数是文件吧?而你想给他传你自定义的二进制流?那么这些"函数"不是lz自己写的么?如果不是lz自己写的,那么又怎么能传自定义的数据格式呢?函数都有自己所要求的文件格式的。。

1.提取单条记录

/*

#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \

no_namespace rename("EOF", "EndOfFile")

*/

CoInitialize(NULL)

_bstr_t varSource="Provider=Microsoft.Jet.OLEDB.4.0Data Source=*.mdb"

//_bstr_t varSource="Data Source=myServerAddressInitial Catalog=myDataBaseUser Id=myUsernamePassword=myPassword"

_ConnectionPtr m_pConnection(_uuidof(Connection))

m_pConnection->Open(varSource,"","",adModeUnknow)

_RecordsetPtr m_pSet(_uuid(Recordset))

try {

m_pSet->Open(%%1,m_pConnection.GetInterfacePtr()

adOpenDynamic,adLockPessimistic,adCmdText)

}

catch(_com_error *e){

{

AfxMessageBox(e->ErrorMessage())

return

}

_variant_t var

CString %%2=""

long fldc=m_pSet->GetFields()->GetCount()

long i=0

try {

m_pSet->MoveFirst()

if(!m_pSet->adoEOF)

{

for(i=0i<fldci++)

{

var=m_pSet->GetCollect((long)i)

var.ChangeType(VT_BSTR)

%%2+=var.bstrVal

%%2+=" "

}

//m_pSet->MoveNext()

}

}

catch(_com_error *e){

AfxMessageBox(e->ErrorMessage())

delete e

}

//m_pSet->MoveFirst()

CoUninitialize(NULL)

3.显示表格

/*

#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \

no_namespace rename("EOF", "EndOfFile")

*/

CoInitialize(NULL)

_bstr_t varSource="Provider=Microsoft.Jet.OLEDB.4.0Data Source=*.mdb"

//_bstr_t varSource="Data Source=myServerAddressInitial Catalog=myDataBaseUser Id=myUsernamePassword=myPassword"

_ConnectionPtr m_pConnection(_uuidof(Connection))

m_pConnection->Open(varSource,"","",adModeUnknow)

//打开属性为默认(adModeRead(只读),adModeWrite(可写),adModeReadWrite(可读写)等)

_RecordsetPtr m_pSet(_uuid(Recordset))

try {

HRESULT hr=m_pSet->Open(%%1,m_pConnection.GetInterfacePtr(),

adOpenDynamic,adLockPessimistic,adCmdText)

}

catch(_com_error *e){

AfxMessageBox(e->ErrorMessage())

}

if(SUCCESSED(hr))

{

//表打开成功

}

FieldsPtr p_fields=m_pSet->Fields

FieldPtr p_field

_variant_t var_index

LPCSTR field_name

int index=0

_bstr_t bstr_field_name

int countfields=p_fields->GetCount()

CString *Column=new CString[countfields]

CListCtrl *pList=(CListCtrl*)GetDlgItem(%%1)//IDC_LIST_TABLEDATA

VERIFY(pList)

pList->DeleteAllItems()

for(index=0index<countfieldsindex++)

{

var_index.vt=VT_I4

var_index.IVal=index

p_field=p_fields->Item[var_index]

bstr_field_name=p_field->GetName()

field_name=(LPCSTR)bstr_field_name

Column[index]=field_name

int ColumnWidth=Column[index].GetLength()*15

pList->InsertColumn(index,field_name,LVCFMT_CENTER,ColumnWidth)

}

int i=0

_bstr_t vCol

//pList->SetTextBkColor(RGB(122,200,122))

//pList->SetTextColor(RGB(0,0,200))

while(!m_pSet->adoEOF)

{

pList->Insert(i,atoi(i))

for(int j=0j<countfieldsj++)

{

vCol=m_pSet->GetCollect((long)j)

pList->SetItemText(i,j,vCol)

}

m_pSet->MoveNext()

i++

}

CoUninitialize(NULL)

4. *** 作表格

/*

#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \

no_namespace rename("EOF", "EndOfFile")

*/

CoInitialize(NULL)

_bstr_t varSource="Provider=Microsoft.Jet.OLEDB.4.0Data Source=*.mdb"

//_bstr_t varSource="Data Source=myServerAddressInitial Catalog=myDataBaseUser Id=myUsernamePassword=myPassword"

_ConnectionPtr m_pConnection(_uuidof(Connection))

m_pConnection->Open(varSource,"","",adModeUnknow)

//打开属性为默认(adModeRead(只读),adModeWrite(可写),adModeReadWrite(可读写)等)

_RecordsetPtr m_pSet(_uuid(Recordset))

try {

HRESULT hr=m_pSet->Open(%%1,m_pConnection.GetInterfacePtr(),

adOpenDynamic,adLockPessimistic,adCmdText)

}

catch(_com_error *e){

AfxMessageBox(e->ErrorMessage())

}

if(SUCCESSED(hr))

{

//表打开成功

}

FieldsPtr p_fields=m_pSet->Fields

FieldPtr p_field

_variant_t var_index

LPCSTR field_name

int index=0

_bstr_t bstr_field_name

int countfields=p_fields->GetCount()

CString *Column=new CString[countfields]

CListCtrl *pList=(CListCtrl*)GetDlgItem(%%1)//IDC_LIST_TABLEDATA

VERIFY(pList)

pList->DeleteAllItems()

for(index=0index<countfieldsindex++)

{

var_index.vt=VT_I4

var_index.IVal=index

p_field=p_fields->Item[var_index]

bstr_field_name=p_field->GetName()

field_name=(LPCSTR)bstr_field_name

Column[index]=field_name

int ColumnWidth=Column[index].GetLength()*15

pList->InsertColumn(index,field_name,LVCFMT_CENTER,ColumnWidth)

}

int i=0

_bstr_t vCol

//pList->SetTextBkColor(RGB(122,200,122))

//pList->SetTextColor(RGB(0,0,200))

while(!m_pSet->adoEOF)

{

pList->Insert(i,atoi(i))

for(int j=0j<countfieldsj++)

{

vCol=m_pSet->GetCollect((long)j)

pList->SetItemText(i,j,vCol)

}

m_pSet->MoveNext()

i++

}

CoUninitialize(NULL)

5.数值范围查询

/*

#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \

no_namespace rename("EOF", "EndOfFile")

*/

try

{

//创建连接对象实例

m_pConnection.CreateInstance("ADODB.Connection")

//设置连接字符串

CString strConnect="DRIVER={Microsoft Access Driver (*.mdb)}\

uid=pwd=DBQ=shujuku.mdb"

//使用Open方法连接数据库

m_pConnection->Open((_bstr_t)strConnect,"","",adModeUnknown)

}

catch(_com_error e)

{

AfxMessageBox(e.Description())

}

}

try {

int min = %%1

int max = %%2

CString sql

sql.Format("select count(*) as pro_count from ProPrice where price between %d and %d",min,max)

HRESULT hr=m_pSet->Open(sql,m_pConnection.GetInterfacePtr(),

adOpenDynamic,adLockPessimistic,adCmdText)

}

catch(_com_error *e){

AfxMessageBox(e->ErrorMessage())

}

if(SUCCESSED(hr))

{

//表打开成功

}

FieldsPtr p_fields=m_pSet->Fields

FieldPtr p_field

_variant_t var_index

LPCSTR field_name

int index=0

_bstr_t bstr_field_name

int countfields=p_fields->GetCount()

CString *Column=new CString[countfields]

CListCtrl *pList=(CListCtrl*)GetDlgItem(%%1)//IDC_LIST_TABLEDATA

VERIFY(pList)

pList->DeleteAllItems()

for(index=0index<countfieldsindex++)

{

var_index.vt=VT_I4

var_index.IVal=index

p_field=p_fields->Item[var_index]

bstr_field_name=p_field->GetName()

field_name=(LPCSTR)bstr_field_name

Column[index]=field_name

int ColumnWidth=Column[index].GetLength()*15

pList->InsertColumn(index,field_name,LVCFMT_CENTER,ColumnWidth)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9969021.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-03
下一篇 2023-05-03

发表评论

登录后才能评论

评论列表(0条)

保存