导航:首页 > 编程大全 > c语言连接数据库

c语言连接数据库

发布时间:2024-11-23 23:07:27

⑴ C语言怎么连接oracle数据库

要在C语言中连接Oracle数据库,您可以使用Oracle提供的ODBC(Open Database Connectivity)接口来实现。以下是连接Oracle数据库的一般步骤:

⑵ c语言如何调用Mysql数据库文件并进行对数据库的操作呢。

MYSQL m_sqlCon;//声明来
mysql_init(&m_sqlCon);//初始化
mysql_real_connect(&m_sqlCon, "127.0.0.1", abc, "root", "hibernate", atoi("3306"),NULL,0)//链接源
mysql_query(&m_sqlCon, "SET NAMES GB2312"); //设置查询编码格式
res = mysql_query(&m_sqlCon,"select * from ms_sendlist where flag = 1 order by style desc");//查询
mysql_query(&m_sqlCon, sql);//插入,删除

⑶ 用C语言怎么实现与数据库的连接

#include<mysql/mysql.h>

#include<stdio.h>

intmain()

{

MYSQL*conn;

MYSQL_RES*res;

MYSQL_ROWrow;

char*server="localhost";//本地连接

char*user="root";//

char*password="525215980";//mysql密码

char*database="student";//数据库名

char*query="select*fromclass";//需要查询的语句

intt,r;

conn=mysql_init(NULL);

if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0))

{

printf("Errorconnectingtodatabase:%s ",mysql_error(conn));

}else{

printf("Connected... ");

}

t=mysql_query(conn,query);

if(t)

{

printf("Errormakingquery:%s ",mysql_error(conn));

}else{

printf("Querymade... ");

res=mysql_use_result(conn);

if(res)

{

while((row=mysql_fetch_row(res))!=NULL)

{

//printf("num=%d ",mysql_num_fields(res));//列数

for(t=0;t<mysql_num_fields(res);t++)

printf("%8s",row[t]);

printf(" ");

}

}

mysql_free_result(res);

}

mysql_close(conn);

return0;

}

(3)c语言连接数据库扩展阅读

C语言使用注意事项:

1、指针是c语言的灵魂,一定要灵活的使用它:

(1)、指针的声明,创建,赋值,销毁等

(2)、指针的类型转换,传参,回调等

2、递归调用也会经常用到:

(1)、递归遍历树结构

(2)、递归搜索

⑷ c语言可以连接access数据库

#import "C:\program files\common files\system\ado\msado15.dll " no_namespace rename( "EOF ", "adoEOF ")
//#import "c:\program files\common files\system\ado\msado15.dll " rename ( "EOF ", "adoEOF ") no_namespace
#define CREATEiNSTANCE(sp,riid) { HRESULT _hr =sp .CreateInstance( __uuidof( riid ) ); \
if (FAILED(_hr)) _com_issue_error(_hr); }

#define RsITEM(rs,x) ((rs-> Fields-> Item[_variant_t(x)]-> Value))

class CHmbAdoDB
{
public:
BOOL m_bOpen;
_ConnectionPtr m_DbConn;
CString m_ErrMsg;
CString m_SQLState;
long mSqlCode;
public:
void Cancel();
BOOL IsOpen();
void RollbackTrans();
void CommitTrans();
long BeginTrans();
void Close();
BOOL Open(LPCSTR lpszConnect);
BOOL Execute(LPCTSTR lpSQL);
void ProviderError();

CHmbAdoDB()
{
}
virtual ~CHmbAdoDB()
{
}

};

class CHmbAdoDBRs : public CObject
{
public:
_RecordsetPtr m_Rs;
public:
CHmbAdoDBRs();
virtual ~CHmbAdoDBRs();
BOOL Open( CHmbAdoDB *pDB, LPCSTR szSource,long option=-1);
void Close();
void GetRs(_variant_t x,_bstr_t &ret);
void GetRs(_variant_t x,CString &ret);
BOOL IsEOF();
BOOL IsOpen();
BOOL MoveNext();
BOOL MovePrev();
BOOL MoveFirst();
BOOL MoveLast();
};

#endif // !defined(AFX_RfidAdoDB_H__7C539954_1C62_47B6_AB6E_1A5BB73A67E5__INCLUDED_) truct InitOle
{
InitOle() { ::CoInitialize(NULL); }
~InitOle() { ::CoUninitialize(); }
} _init_InitOle_;

long mSqlCode;
BOOL CHmbAdoDB::Open(LPCSTR lpszConnect)
{
try
{
CREATEiNSTANCE(m_DbConn,Connection);
m_DbConn-> Open(lpszConnect, " ", " ", adModeUnknown );
}
catch( _com_error &e)
{
return FALSE;
}
catch(...)
{
return FALSE;
}
return TRUE;
}

void CHmbAdoDB::Close()
{
if( IsOpen() )
{
m_DbConn-> Close();
}
}

long CHmbAdoDB::BeginTrans()
{
return m_DbConn-> BeginTrans();
}

void CHmbAdoDB::CommitTrans()
{
m_DbConn-> CommitTrans();
}

void CHmbAdoDB::RollbackTrans()
{
m_DbConn-> RollbackTrans();
}

void CHmbAdoDB::Cancel()
{
m_DbConn-> Cancel();
}

BOOL CHmbAdoDB::IsOpen()
{
return ( (m_DbConn-> GetState() != 0) ? TRUE : FALSE );
}

BOOL CHmbAdoDB::Execute(LPCTSTR lpSQL)
{
if (!IsOpen())
{
AfxMessageBox( "Network error, Database do not open. ");
return FALSE;
}

CString strSQL(lpSQL);
strSQL.TrimLeft();
strSQL.TrimRight();

try
{
m_DbConn-> Execute(_bstr_t(strSQL), NULL, adExecuteNoRecords);
}
catch( _com_error &e)
{
return FALSE;
}
catch(...)
{
return FALSE;
}

return TRUE;
}

void CHmbAdoDB::ProviderError()
{
// Print Provider Errors from Connection object.
// pErr is a record object in the Connection 's Error collection.
ErrorPtr pErr = NULL;

if( (m_DbConn-> Errors-> Count) > 0)
{
long nCount = m_DbConn-> Errors-> Count;

// Collection ranges from 0 to nCount -1.
for(long i = 0; i < nCount; i++)
{
pErr = m_DbConn-> Errors-> GetItem(i);
mSqlCode = pErr-> NativeError;
m_ErrMsg.Format( "SQLCODE : %ld\t%s ",mSqlCode, LPCTSTR(pErr-> Description));
m_SQLState.Format( "%s ",(char*)pErr-> SQLState);
printf( "%s // %s\n ",m_SQLState,m_ErrMsg);
}
}
m_DbConn-> Errors-> Clear();
}

CHmbAdoDBRs::CHmbAdoDBRs( )
{
}

CHmbAdoDBRs::~CHmbAdoDBRs()
{

}

BOOL CHmbAdoDBRs::Open( CHmbAdoDB *pDB, LPCSTR szSource,long option )
{
CString strSQL;
strSQL = CString(szSource);
strSQL.TrimLeft();
strSQL.TrimRight();
_variant_t vRecsAffected(0L);
// CREATEiNSTANCE( m_Rs,Recordset) ;
// m_Rs-> PutRefActiveConnection( pDB-> m_DbConn );
try
{
m_Rs = pDB-> m_DbConn-> Execute(_bstr_t(strSQL),
&vRecsAffected,
adOptionUnspecified);
m_Rs-> AddRef();
// m_Rs-> Open(szSource, vtMissing, adOpenKeyset, adLockBatchOptimistic, adOptionUnspecified);
// m_Rs-> Open(szSource, vtMissing, adOpenKeyset, adLockReadOnly, option);
// m_Rs-> Open(szSource,pDB-> m_DbConn.GetInterfacePtr(),adOpenDynamic,adLockOptimistic, adCmdText);

}
catch( _com_error &e)
{
//ErrorMessage();
m_Rs-> Close();
m_Rs-> Release();
return FALSE;
}
catch(...)
{
m_Rs-> Close();
m_Rs-> Release();
return FALSE;
}
return TRUE;
}

void CHmbAdoDBRs::Close()
{
m_Rs-> Close();
m_Rs-> Release();
}
void CHmbAdoDBRs::GetRs(_variant_t x,_bstr_t &ret)
{
ret = (m_Rs-> Fields-> Item[x]-> Value);
}

void CHmbAdoDBRs::GetRs(_variant_t x,CString &ret)
{
_bstr_t retVal;

try{
retVal = (m_Rs-> Fields-> Item[x]-> Value);
}
catch(...){

ret = " ";
return;
}

ret = CString((char*) retVal);
}

BOOL CHmbAdoDBRs::IsEOF()
{
return m_Rs-> adoEOF;
}

BOOL CHmbAdoDBRs::IsOpen()
{
return ( (m_Rs-> GetState() != 0) ? TRUE : FALSE );
}

BOOL CHmbAdoDBRs::MoveNext() { return (FAILED(m_Rs-> MoveNext()) ? FALSE : TRUE ); }
BOOL CHmbAdoDBRs::MovePrev() { return (FAILED(m_Rs-> MovePrevious()) ? FALSE : TRUE ); }
BOOL CHmbAdoDBRs::MoveFirst() { return (FAILED(m_Rs-> MoveFirst()) ? FALSE : TRUE ) ; }
BOOL CHmbAdoDBRs::MoveLast() { return (FAILED(m_Rs-> MoveLast()) ? FALSE : TRUE ) ; }
以上为一个ADO的封装类.
c语言如何连接access数据库
现在一般是用ADO的方法.

阅读全文

与c语言连接数据库相关的资料

热点内容
ubuntu翻译工具 浏览:665
wifi安装教程 浏览:398
苹果有些qq文件打不开 浏览:139
微信分身图片缓存在哪个文件 浏览:544
众筹用什么网站 浏览:1
天马座的幻想版本 浏览:536
微云保存文件图片没有了 浏览:236
如何把excel表格图片导出到文件夹 浏览:387
qq三国快速升级攻略 浏览:660
js监听手机home事件 浏览:439
第2章linux的桌面管理副本 浏览:452
qq邮箱手机上登录微信账号密码错误 浏览:627
编程如何让人物重复发射子弹 浏览:853
db2查看表空间文件 浏览:607
ps文件界面设置 浏览:779
c语言12位的数据应该怎么存储 浏览:953
将ape导入iphone 浏览:107
js组合快捷键 浏览:174
linux系统盘默认挂在的文件夹 浏览:667
淘宝数据包如何操作上架 浏览:567

友情链接