1.数据库连接问题
期初客户端登陆不成功
思考:
经同学介绍,不是用户或密码问题,是由于SQL Server(MSSQLSERVER)已停止运行的缘故.
SQL Server(MSSQLSERVER)的作用
连接符号“.”或者“IP地址”或者“localhost”或者“127.0.01”或者“一个具体的实例名”表示MSSQLServer
MSSQLServer是microsoft sql server的缩写,开启的配置包含Shared memory,Name Pipes和TCP/IP等,是正式版的。
VB连接数据SQL Server
Public FunctionConnectString() As String
ConnectString ="FileDSN=charge_sys.dsn;UID=sa;PWD=123456" SQL Server的登陆
End Function
PublicFunction ExecuteSQL(ByVal SQL As String, MsgString As String) AsADODB.Recordset
Dim cnn AsADODB.Connection
Dim rst AsADODB.Recordset
Dim sTokens() AsString
On Error GoToExecuteSQL_Error
sTokens =Split(SQL)
Set cnn = NewADODB.Connection
cnn.OpenConnectString
If InStr("insert,delete,update", UCase$(sTokens(0))) Then
cnn.ExecuteSQL
MsgString =sTokens(0) & "query successful"
Else
Set rst = NewADODB.Recordset
rst.OpenTrim$(SQL), cnn, adOpenKeyset, adLockOptimistic
Set ExecuteSQL= rst '?
MsgString = "查询到" &rst.RecordCount & _
"条记录"
End If
ExecuteSQL_exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function
ExecuteSQL_Error:
MsgString = "查询错误:" &Err.Description '?
ResumeExecuteSQL_exit '?
End Function '(这段内容至软件的登陆 )
2.连接数据库以后,打开具体表(例:)
Dim mrc As ADODB.Recordset
Dim MsgText As String
Dim txtSQL As String
txtSQL = "select *fromBasicData_Info"
Set mrc = ExecuteSQL(txtSQL, MsgText)
3.查询数据(例:)
txtSQL1 = "select * from Cardstudent_info where cardno='" &Trim(txtCard.Text) & "'"
Set mrc1 = ExecuteSQL(txtSQL1, MsgText1)
If mrc1.EOF Then '表示没有此卡号
MsgBox "数据库中无此卡号,请注册以后再充值!", vbOKOnly + vbExclamation, "警告"
Exit Sub
endif
If not mrc1.EOF Then '表示存在此卡号
endif
小提示:mrc.EOF的意思是超出最后一条语句
4.删除数据(例:)
PrivateSub Command1_Click()
Dim txtSQLday As String, MsgTextday AsString
Dim mrcday As ADODB.Recordset
txtSQLday = "delete * from CheckDay_info" 'where date='" &Format(Now, "yyyy-mm-dd") & "'" & "order bydate"
Set mrcday = ExecuteSQL(txtSQLday,MsgTextday)
mrcday.Update
mrcday.Close
EndSub
这样删除数据是要报错的,删除数据不需要数据更新和关闭
就算注释了mrcday.Update、mrcday.Close这两句,虽然不会报错,但是删除是不成功的,因为删除整个表数据时是不需要加“*”的
如果是要删除某条数据记录,最好是将这条语句查询出来,这样利用
mrcday.Delete
mrcday.Update
mrcday.Close
语句就可以了