#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\n",mysql_error(conn))
}else{
printf("Connected...\n")
}
t=mysql_query(conn,query)
if(t)
{
printf("Errormakingquery:%s\n",mysql_error(conn))
}else{
printf("Querymade...\n")
res=mysql_use_result(conn)
if(res)
{
while((row=mysql_fetch_row(res))!=NULL)
{
//printf("num=%d\n",mysql_num_fields(res))//列数
for(t=0t<mysql_num_fields(res)t++)
printf("%8s",row[t])
printf("\n")
}
}
mysql_free_result(res)
}
mysql_close(conn)
return0
}
扩展资料
C语言使用注意事项:
1、指针是c语言的灵魂,一定要灵活的使用它:
(1)、指针的声明,创建,赋值,销毁等
(2)、指针的类型转换,传参,回调等
2、递归调用也会经常用到:
(1)、递归遍历树结构
(2)、递归搜索
1.c++连接连接access程序代码:
using system.data
using system.data.oledb
..
string strconnection="provider=microsoft.jet.oledb.4.0"
strconnection+=@"data source=c:begaspnetnorthwind.mdb"
oledbconnection objconnection=new oledbconnection(strconnection)
..
objconnection.open()
objconnection.close()
2.c++连接sql server
程序代码:
using system.data
using system.data.sqlclient
..
string strconnection="user id=sapassword="
strconnection+="initial catalog=northwindserver=yoursqlserver"
strconnection+="connect timeout=30"
sqlconnection objconnection=new sqlconnection(strconnection)
..
objconnection.open()
objconnection.close()
3.c++连接mysql
程序代码:
using mysqldrivercs
// 建立数据库连接
mysqlconnection dbconn
dbconn = new mysqlconnection(new mysqlconnectionstring("localhost","mysql","root","",3306).asstring)
dbconn.open()
// 执行查询语句
mysqlcommand dbcomm
dbcomm = new mysqlcommand("select host,user from user",dbconn)
// 读取数据
mysqldatareader dbreader = dbcomm.executereaderex()
// 显示数据
try
{
while (dbreader.read())
{
console.writeline("host = {0} and user = {1}", dbreader.getstring(0),dbreader.getstring(1))
}
}
finally
{
dbreader.close()
dbconn.close()
}
//关闭数据库连接
dbconn.close()
4.c++连接ibm db2
程序代码:
oledbconnection1.open()
//打开数据库连接
oledbdataadapter1.fill(dataset1,"address")
//将得来的数据填入dataset
datagrid1.databind()
//绑定数据
oledbconnection1.close()
//关闭连接
//增加数据库数据
在web form上新增对应字段数量个数的textbox,及一个button,为该按键增加click响应事件代码如下:
this.oledbinsertcommand1.commandtext = "insertsintosaddress(name,
email, age, address) values
(’"+textbox1.text+"’,’"+textbox2.text+"’,’"+textbox3.text+"’,’"+textbox4.text+"’)"
oledbinsertcommand1.connection.open()
//打开连接
oledbinsertcommand1.executenonquery()
//执行该sql语句
oledbinsertcommand1.connection.close()
//关闭连接
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)