Dim r As New ADODB.Recordset
Dim a As Long
Dim i As Long
On Error Resume Next
a = 0
c.Open C_Str
c.BeginTrans
If c.State = 1 Then
r.Open "select top 0 * from sfk", c, adOpenStatic, adLockOptimistic
If r.State = 1 Then
......
If c.State = 1 Then 这一句就是判断数据库是否成功连接
If r.State = 1 Then 这一句就是判断数据表sfk是否成功打开
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条)