vb连接SQL数据库,要代码

初学者,在用VB6连接SQL数据库,放个代码出来。加100分1楼的看不懂,做个说明出来,你这个好象是NET啊,我要VB6的... 初学者,在用VB6连接SQL数据库,放个代码出来。加100分
1楼的看不懂,做个说明出来,你这个好象是NET啊,我要VB6的
展开
 我来答
lzh741206
2008-06-14 · TA获得超过1192个赞
知道大有可为答主
回答量:1929
采纳率:100%
帮助的人:2776万
展开全部
给你一个示例代码吧
Private Sub cmdOK_Click()
Static nErr As Integer
Dim bErr As Boolean
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strSql As String

If txtUser.Text = "" Or txtPwd.Text = "" Then
MsgBox "用户或密码不能为空!", vbCritical, "错误"
bErr = True
Else
Set conn = OpenConnForSqlServer("127.0.0.1\SQLEXPRESS", "xsfh", "")
strSql = "select * from users where name = '" & txtUser.Text & "' and password='" & txtPwd.Text & "'"
Set rs = OpenRecordset(strSql, conn)
If rs.EOF Then
MsgBox "错误的用户名或密码!", vbCritical, "错误"
bErr = True
Else
Unload Me
frmMain.Show
End If
End If

If bErr Then
nErr = nErr + 1
If nErr = 3 Then
Unload Me
Else
txtUser.Text = ""
txtPwd.Text = ""
txtUser.SetFocus
End If
End If
End Sub

Public Function OpenConnForSqlServer(ByVal ServerName As String, ByVal UserId As String, ByVal PassWord As String, Optional ByVal DateBaseName As String = "") As ADODB.Connection
Dim AdoConn As New ADODB.Connection
Dim strConn As String
With AdoConn
strConn = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=" & UserId & ";Password=" & PassWord & ";Data Source=" & ServerName
If DateBaseName <> "" Then strConn = strConn & ";database=" & DateBaseName
.ConnectionString = strConn
.Open
End With
Set OpenConnForSqlServer = AdoConn
End Function

Public Function OpenConnForSqlServerByWin(ByVal ServerName As String, Optional ByVal DateBaseName As String = "") As ADODB.Connection
Dim AdoConn As New ADODB.Connection
Dim strConn As String
With AdoConn
strSql = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Data Source=" & ServerName
If DateBaseName <> "" Then strConn = strConn & ";Initial Catalog=" & DateBaseName
.ConnectionString = strConn
.Open
End With
Set OpenConnForSqlServerByWin = AdoConn
End Function

Public Function OpenRecordset(ByVal strSql As String, ByVal AdoConn As ADODB.Connection) As ADODB.Recordset
Dim rs As New ADODB.Recordset
With rs
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
.Open strSql, AdoConn, , , adCmdText
End With
Set OpenRecordset = rs
End Function

Public Function RunTrans(ByVal tranSql As String, ByVal AdoConn As ADODB.Connection)
With AdoConn
.BeginTrans
.Execute tranSql
.CommitTrans
End With
End Function
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式