//在工程设置-》链接》库模块中添加 libmysql.lib
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <winsock.h>
#include "E:\wamp\bin\mysql\mysql5.5.8\include\mysql.h"
void main(){
MYSQL *conn
MYSQL_RES *res
MYSQL_ROW row
char *server ="localhost"
char *user ="root"
char *password=""
char *database="test"
char sql[1024]="select * from chinaren"
conn=mysql_init(NULL)
if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0)){
fprintf(stderr,"%s\n",mysql_error(conn))
exit(1)
}
if(mysql_query(conn,sql)){
fprintf(stderr,"%s\n",mysql_error(conn))
exit(1)
}
res=mysql_use_result(conn)
while((row = mysql_fetch_row(res))!=NULL){
printf("%s\n",row[2])
}
mysql_free_result(res)
mysql_close(conn)
}
===============================
#if defined(_WIN32) || defined(_WIN64) //为了支持windows平台上的编译
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "mysql.h"
//定义数据库 *** 作的宏,也可以不定义留着后面直接写进代码
#define SELECT_QUERY "show tables"
int main(int argc, char **argv) //char **argv 相当于 char *argv[]
{
MYSQL mysql,*handle //定义数据库连接的句柄,它被用于几乎所有的MySQL函数
MYSQL_RES *result //查询结果集,结构类型
MYSQL_FIELD *field//包含字段信息的结构
MYSQL_ROW row //存放一行查询结果的字符串数组
char querysql[160] //存放查询sql语句字符串
//初始化
mysql_init(&mysql)
//连接数据库
if (!(handle = mysql_real_connect(&mysql,"localhost","user","pwd","dbname",0,NULL,0))) {
fprintf(stderr,"Couldn't connect to engine!\n%s\n\n",mysql_error(&mysql))
}
sprintf(querysql,SELECT_QUERY,atoi(argv[1]))
//查询数据库
if(mysql_query(handle,querysql)) {
fprintf(stderr,"Query failed (%s)\n",mysql_error(handle))
}
//存储结果集
if (!(result=mysql_store_result(handle))) {
fprintf(stderr,"Couldn't get result from %s\n", mysql_error(handle))
}
printf("number of fields returned: %d\n",mysql_num_fields(result))
//读取结果集的内容
while (row = mysql_fetch_row(result)) {
printf("table: %s\n",(((row[0]==NULL)&&(!strlen(row[0]))) ? "NULL" : row[0]) )
}
//释放结果集
mysql_free_result(result)
//关闭数据库连接
mysql_close(handle)
system("PAUSE")
//为了兼容大部分的编译器加入此行
return 0
}
对于SQL Server数据库,C++使用MFC库,主要有两种方法可以连接sql数据库
1.利用ADO连接:
#import "msado15.dll" no_namespace rename("EOF", "EndOfFile")
//必须import这个dll,这个文件通常放在C:\Program Files\Common Files\System\ado路径下.
_ConnectionPtr m_ptrConnection//数据库连接对象
构造函数中添加如下语句
m_ptrConnection = NULL
::CoInitialize(NULL)
//连接数据库的主要代码
BOOL DataVisitor::ConnectDataBase(_bstr_t connectionStr)
{
/*
Added by stone. If IDOConnection has not been set up,then create one.
*/
if(m_ptrConnection == NULL)
{
HRESULT hr = m_ptrConnection.CreateInstance(__uuidof(Connection))
if (FAILED(hr))
{
return FALSE
}
else
{
_bstr_t strConnect = connectionStr
//"Provider=SQLOLEDBServer=(local)Database=navigationuid=sapwd=3277625"
m_ptrConnection->CursorLocation = adUseClient
m_ptrConnection->IsolationLevel = adXactReadCommitted
try
{
m_ptrConnection->Open(strConnect,"","",adModeUnknown)
return TRUE
}
catch (_com_error e)
{
// AfxMessageBox((char *)e.Description())
return FALSE
}
}
}
return TRUE
}
2. 利用ODBC连接
#include <afxdao.h>
CDaoDatabase *MyDataBase
BOOL MyDB_OperSqL::Open_MyDatabase(CString connstr)
{
try
{
if (MyDataBase == NULL)
{
MyDataBase = new CDaoDatabase()
}
MyDataBase->Open(NULL,0,0,connstr)
}
catch( CDaoException* e )
{
CString message = _T("MyDB_OperSqL 数据库异常: ")
message += e->m_pErrorInfo->m_strDescription
char info[400]
sprintf(info,message)
DispErrorMessage(info,__LINE__)
e->Delete( )
return FALSE
}
catch (CMemoryException *e)
{
DispErrorMessage("MyDB_OperSqL 内存异常!",__LINE__)
e->Delete( )
return FALSE
}
catch(...)
{
DispErrorMessage("MyDB_OperSqL 其它异常!",__LINE__)
return FALSE
}
return TRUE
}
这里的连接字符串connstr一般是如下内容
"ODBCDRIVER={SQL Server}SERVER=(local)DATABASE=yourDataBaseUID=yourIDPWD=yourPassword"
如果直接用Microsoft ADO Datebase Control控件的话,连接字串可以自己生成,在控件的属性里面找到拷贝就行,这种最简单
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)