{
try
{
con.Open()
label1.Text = "连接成功"
}
catch (OleDbException err)
{
label1.Text = "连接不成功"
}
finally
{
con.Close()
}
建议用ado连接,网上的ado封装好的库有很多,随便下一个就行。包含了头文件和cpp文件后,可以这样
CADORecordset* pRs = new CADORecordset((static_cast<CFrenchApp *>(AfxGetApp()))->g_pDb)
Sql1="select word,wordtype,meaning,tag,id from word "+Where
int i=0
if(pRs->Open((LPCTSTR)Sql1))
{
while(!pRs->IsEof())
{
pRs->GetFieldValue(0,word[i].word)
pRs->GetFieldValue(3,word[i].tag)
pRs->GetFieldValue(1,word[i].wordtype)
pRs->GetFieldValue(2,word[i].meaning)
pRs->GetFieldValue(4,word[i].id)
pRs->MoveNext()
i++
}
pRs->Close()
}
m_max=i
m_cur=0
delete pRs
这样就可以得到数据库里的东西
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条)