当前位置:首页 » 数据仓库 » c连接数据库的步骤
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c连接数据库的步骤

发布时间: 2023-06-02 22:42:51

① c怎么连接sql server数据库

1.准备工作: 准备相关的软件(Eclipse除外,开源软件可以从官网下载) <1>.Microsoft SQL server 2005 Express Edition 下载地址:http://download.microsoft.com/download/0/9/0/09020fab-d2c3-4a8c-b9e0-db53a7a30ae8/SQLEXPR_CHS.EXE <2>.SQL Server Management Studio 下载地址:http://www.microsoft.com/downloads/details.aspx?displaylang=zh-cn&FamilyID=c243a5ae-4bd1-4e3d-94b8-5a0f62bf7796#filelist <3>.SQL Server 2005 driver for JDBC 下载地址:http://download.microsoft.com/download/8/B/D/8BDABAE2-B6EA-41D4-B903-7916EF3690EF/sqljdbc_1.2.2323.101_enu.exe 2.JDBC连接SQL Server的驱动安装 ,前两个是属于数据库软件,正常安装即可(注意数据库登陆不要使用windows验证) 是用java连接吗? 如果是,方法如下: <1> 将JDBC解压缩到任意位置,比如解压到C盘program files下面,并在安装目录里找到sqljdbc.jar文件,得到其路径开始配置环境变量 在环境变量classpath 后面追加 C:\Program Files\Microsoft SQL Server2005 JDBC Driver\sqljdbc_1.2\enu\sqljdbc.jar <2> 设置SQLEXPRESS服务器: a.打开SQL Server Configuration Manager -> SQLEXPRESS的协议 -> TCP/IP b.右键单击启动TCP/IP c.双击进入属性,把IP地址中的IP all中的TCP端口设置为1433 d.重新启动SQL Server 2005服务中的SQLEXPRESS服务器 e.关闭SQL Server Configuration Manager <3> 打开刚刚安装好的 SQL Server Management Studio,连接SQLEXPRESS服务器, 新建数据库,起名字为sample <4> 打开Eclipse a.新建工程-> Java -> Java project,起名为Test b.选择eclipse->窗口->首选项->Java->installed JRE 编辑已经安装好的jdk,查找目录添加sqljdbc.jar c.右键单击目录窗口中的Test, 选择Build Path ->Configure Build Path..., 添加扩展jar文件,即把sqljdbc.jar添加到其中 <5> 编写Java代码来测试JDBC连接SQL Server数据库

② 用c语言怎么连接数据库呢

c语言当然可以连接数据库了。看你是想要连接什么数据库呢?各种数据库都有很多相应的函数实现了。看你想要使用什么技术了,比如说ado,odbc,,等等都可以连接数据库。

③ 用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语言怎样连接mysql数据库

mysql是有c语言接口的,安装相应库后就可以链接了,一般连接mysql的函数是mysql_connect或者mysql_real_connect(大概就是这么拼的吧。。。)可以使用mysql_query执行sql语句

⑤ c/c++怎么连接数据库,并执行SQL语句

C++连接SQL数据库第一步 系统配置
1.设置SQLSERVER服务器为SQL登录方式,并且系统安全性中的sa用户要设置登录功能为“启用”,还有必须要有密码。
2.需要在ODBC中进行数据源配置,数据源选\”SQL SERVER”,登录方式使用“使用输入用户登录ID和密码的SQL SERVER验证”,并填写登录名(sa)和密码,注意一点,密码不能为空,这就意味着你的sa用户必须得有密码。否则无法通过系统本身的安全策略。测试通过就完成了配置。
C++连接SQL数据库第二步 C++与SQL连接初始化
1.在你所建立的C++项目中的stdafx.h头文件中引入ADO
具体代码如下
#import “c:\Program Files\Common Files\System\ado\msado15.dll”
no_namespace rename(”EOF”, “adoEOF”) rename(”BOF”, “adoBOF”)
2.定义_ConnectionPtr变量后调用Connection对象的Open方法建立与服务器的连接。
数据类型_ConnectionPtr实际上是由类模板_com_ptr_t得到的一个具体的实例类。_ConnectionPtr类封装了Connection对象的Idispatch接口指针及其一些必要的操作。可以通过这个指针操纵Connection对象。
例如连接SQLServer数据库,代码如下:
//连接到MS SQL Server
//初始化指针
_ConnectionPtr pMyConnect = NULL;
HRESULT hr = pMyConnect.CreateInstance(__uuidof(Connection));
if (FAILED(hr))
return;
//初始化链接参数
_bstr_t strConnect = “Provider=SQLOLEDB;
Server=hch;
Database=mytest;
uid=sa; pwd=sa;”; //Database指你系统中的数据库
//执行连接
try
{
// Open方法连接字串必须四BSTR或者_bstr_t类型
pMyConnect->Open(strConnect, “”, “”, NULL);
}
catch(_com_error &e)
{
MessageBox(e.Description(), “警告”, MB_OK|MB_ICONINFORMATION);
}//发生链接错误

C++连接SQL数据库第三步 简单的数据连接
//定义_RecordsetPtr变量,调用它Recordset对象的Open,即可打开一个数据集
//初始化过程 以下是个实例
_RecordsetPtr pRecordset;
if (FAILED(pRecordset.CreateInstance(__uuidof(Recordset))))
{
return;
}
//执行操作
try
{
pRecordset->Open(_variant_t(”userinfo”),
_variant_t((IDispatch*)pMyConnect),
adOpenKeyset, adLockOptimistic, adCmdTable);
}
catch (_com_error &e)
{
MessageBox(”无法打开userinfo表\”, “系统提示”,
MB_OK|MB_ICONINFORMATION);
}

C++连接SQL数据库第四步 执行SQL语句
这里是关键,我认为只要你懂点SQL语句那么一切都会方便许多比用上面的方法简单,更有效率点。
首先
m_pConnection.CreateInstance(_uuidof(Connection));
//初始化Connection指针
m_pRecordset.CreateInstance(__uuidof(Recordset));
//初始化Recordset指针
CString strSql=”select * from tb_goods”;//具体执行的SQL语句
m_pRecordset=m_pConnection->Execute(_bstr_t(strSql),
NULL, adCmdText);//将查询数据导入m_pRecordset数据容器
至此 你的SQL语句已经执行完成了m_pRecordset内的数据就是你执行的结果。
取得记录:
while(!m_pRecordset->adoEOF)//遍历并读取name列的记录并输出
{
CString temp = (TCHAR *)(_bstr_t)m_pRecordset->GetFields()->GetItem
(”name”)->Value;
AfxMessageBox(temp);
pRecordset->MoveNext();
}

插入记录
//记得初始化指针再执行以下操作
CString strsql;
strsql.Format(”insert into tb_goods(no,name, price)
values(’%d’,'%s’, %d)”,m_intNo,m_strName,m_intPrice);
m_pRecordset=m_pConnection->
Execute(_bstr_t(strsql),NULL,adCmdText);

修改记录
CString strsql;
strsql.Format(”update tb_goods set name=’%s’ ,
price=%d where no=%d “,m_strName,m_intPrice,m_intNo);
m_pRecordset=m_pConnection->Execute(_bstr_t(strsql),NULL,adCmdText);

删除记录
CString strsql;
strsql.Format(”delete from tb_goodswhere no= ‘%d’ “,m_intNo);
m_pRecordset=m_pConnection->Execute(_bstr_t(strsql),NULL,adCmdText)

⑥ c语言怎么连接mysql数据库 代码

//vc工具中添加E:\WAMP\BIN\MYSQL\MYSQL5.5.8\LIB 路径
//在工程设置-》链接》库模块中添加 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;
}