vb连接SQL数据库,要代码
初学者,在用VB6连接SQL数据库,放个代码出来。加100分1楼的看不懂,做个说明出来,你这个好象是NET啊,我要VB6的...
初学者,在用VB6连接SQL数据库,放个代码出来。加100分
1楼的看不懂,做个说明出来,你这个好象是NET啊,我要VB6的 展开
1楼的看不懂,做个说明出来,你这个好象是NET啊,我要VB6的 展开
1个回答
展开全部
给你一个示例代码吧
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
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |