| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 885 人关注过本帖
标题:VB连接几种数据库的代码
只看楼主 加入收藏
海凌
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2006-8-1
收藏
 问题点数:0 回复次数:0 
VB连接几种数据库的代码

1用函数连接数据库
Public Const DataBaseName = "CC_BOOK" '数据库名称
Systitle = "书店管理系统"
If ConnSQL("CCTSOFT\SQLSEVER", DataBaseName, "sa", "") = False Then
MsgBox "连接数据库不成功!", vbExclamation, Systitle
Exit Sub
End If
‘连接数据库涵数
Public Function ConnSQL(ByVal cNtServerName As String, _
ByVal cDatabaseName As String, ByVal cSQLUserName As String, _
ByVal cSQLPassword As String) As Boolean '打开数据库
On Error GoTo SQLConErr
With DB
.CursorLocation = adUseClient
.Provider = "sqloledb"
.Properties("Data Source").Value = cNtServerName
.Properties("Initial Catalog").Value = cDatabaseName
.Properties("User ID") = cSQLUserName
.Properties("Password") = cSQLPassword
.Properties("prompt") = adPromptNever
'尝试打开时间,可以改这个时间
.ConnectionTimeout = 10
.Open
If .State = adStateOpen Then
ConnSQL = True
Exit Function
Else
ConnSQL = False
Set DB = Nothing
Exit Function
End If
End With
Exit Function
SQLConErr:
ConnSQL = False
Set DB = Nothing
End Function

1 ADO字符串连接数据库
Dim cn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim strcnn As String
Dim strsql As String
strcnn = "driver={sql server};server=CC_COFT;uid=sa;pwd=;database=Student"
Set cn = New ADODB.Connection
cn.Open strcnn '打开学生表

2 种方法
strconn = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=student"
If db.State = 1 Then db.Close
db.Open strconn


VB使用ADO数据库可以分为有源数据库和无源数据库,即是否使用了DSN数据源,如下例:
1、在连接数据库前首先要在VB菜单中“工程”-“引用”从中选择 microsoft activeX Data objects 2.6 library和 microsoft activeX Data objects recordset 2.6两个组件,这是在连接数据前所必做的工作。
2、接下来定义ADO的连接对象,如下所示:
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
第一种方法采用的是无源的数据库连接方法
conn.ConnectionString = "Driver={sql server};server=JSZX3-11;uid=sa;pwd=;database=wzc"
conn.ConnectionTimeout = 30
conn.Open
rs.Open "select 用户名,密码 from login where 用户名='" & Trim(Combo1.Text) & "' And 密码='" & Trim(Text1.Text) & "'", conn, adOpenStatic, adLockReadOnly, adCmdText
If rs.EOF = True And rs.BOF Then
m = MsgBox("请重新登录", vbExclamation)
Text1.Text = ""
Text1.SetFocus
Else
Form1.Hide
End If
rs.Close
这是第二种方法连接数据库的方法,这一种方法是采用有源的方法与数据库连接的方法,代码如下所示:
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.ConnectionString = "DSN=login;uid=sa;pwd=;"
conn.ConnectionTimeout = 30
conn.Open
rs.Open "select 用户名 from login", conn, adOpenStatic, adLockReadOnly, adCmdText
Dim i As String
For t = 0 To Val(rs.RecordCount) - 1
i = Trim(rs.Fields("用户名").Value)
rs.MoveNext
Combo1.AddItem i
Next t
rs.Close
连接数据库的方法很多很多,只要各位去勇敢的偿试一下,就可以感觉数据库的方法并不是很难,本人以前也很想做这方法的工作,只是学识不深,未能完成胸中大志,如今对这些方法的多少有一些了解,特意为那些初学者以供学习之用。

MS Access数据库连接

用DSN连接并且没有用户名和密码:
<%
set conn = Server.CreateObject("ADODB.Connection")
conn.open "YourDSNName"
%>

用DSN连接并且有用户名和密码:
<%
set conn = Server.CreateObject("ADODB.Connection")
conn.open "YourDSNName","username","password"
%>

用实际的数据库绝对路径连接:
<%
Set conn = Server.CreateObject("ADODB.Connection")
Strconn="DRIVER={Microsoft Access Driver (*.mdb)}; "
Strconn=Strconn & "DBQ=e:\yanhang\database.mdb"
conn.Open Strconn
%>

用实际的数据库相对路径连接:
<%
Set conn = Server.CreateObject("ADODB.Connection")
Strconn="DRIVER={Microsoft Access Driver (*.mdb)}; "
Strconn=Strconn & "DBQ=" & Server.MapPath("/database/yanhang.mdb")
conn.Open Strconn
%>

MS SQL Server数据库连接
用DSN连接:
<%
set conn = Server.CreateObject("ADODB.Connection")
conn.open "DSN=MyDSN;UID=user;PWD=password;DATABASE=databasename"
%>

不用DSN连接:
<%
Set conn = Server.CreateObject("ADODB.Connection")
DSNtemp="DRIVER={SQL Server};SERVER=ServerName;UID=USER;PWD=password;DATABASE=databasename"
conn.open DSNtemp
%>

FoxPro数据库连接
<%
Set Conn = Server.CreateObject("ADODB.connection")
ConnStr= "Driver=Microsoft Visual Foxpro Driver; UID=userID;SourceType=DBC;SourceDB=C:\yanhang\database.dbc"
Conn.Open ConnStr
%>

Oracle数据库连接:
<%
set conn=server.createobject("adodb.connection")
conn.cursorlocation=adUseClient
DSNTemp="Provider=MSDAORA.1;Password=xxxxx;User ID=yanhang;Data Source=xxx.world"
conn.open DSNtemp
%>
dim SQL,proname,manname
manname=request("manname")
proname=request("proname")
SQL="select * from product where true "
if trim(manname)<>"" then
SQL=SQL & " and menname like '%" & manname & "%'"
end if
if trim(proname)<>"" then
SQL=SQL & "and proname like '%" & proname & "%'"
end if
Set conn = Server.CreateObject("ADODB.Connection")
DSNtemp="DRIVER={SQL Server};SERVER=chen;UID=sa;PWD=;DATABASE=xxinfo"
conn.open DSNtemp
Set rs = Server.CreateObject("ADODB.Recordset")
rs.cursorLocation=3
rs.open sql,conn,1,1

搜索更多相关主题的帖子: 数据库 代码 
2006-08-06 09:27
快速回复:VB连接几种数据库的代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016355 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved