- Attribute VB_Name = "main1"
- Public POSID As Integer 'POS机编号
- Public strSaleID As String '单号
- Public strSMName As String '超市名称
- Public strAddress As String '超市地址
- Public strTele As String '超市电话
- Public strPOST As String '邮编
- Public UserName As String '当前用户名
- Public UserStyle As Long '当前用户类型
- Dim cnn As ADODB.Connection
- Public Sub Main() '程序入口
- App.Title = "超市POS系统"
- ReadInf
- frmLogin.Show
- End Sub
- Public Sub ReadInf() '获取POS机配信息
- Dim strTmp As String, str1() As String
- On Error GoTo err1 '错误处理
- Open App.Path & "DATAbase.inf" For Input As #1 '打开文件
- If Not EOF(1) Then '若文件未结束
- Line Input #1, strTmp '读入一行内容
- End If
- Close #1 '关闭打开的文件
- strTmp = Trim(strTmp) '去掉尾部空格
- If strTmp <> "" Then '若字符串不为空
- str1 = Split(strTmp, ",") '将字符串分解为数组
- POSID = str1(0) '将POS机序号保存到全局变量
- strSaleID = POSID & Format(str1(1), "0000000") '由POS机号作第1位生成销售单编号
- End If
- Exit Sub
- err1:
- POSID = 1 '设置为1号POS机
- strSaleID = POSID & Format(1, "0000000") ' '由POS机号作第1位
- End Sub
- Private Sub Connect() '连接数据库
- If IsConnect = True Then '如果连接标记为真,则返回。否则会出错
- Exit Sub
- End If
- On Error GoTo DbOpenErr
- Set cnn = New ADODB.Connection '关键New用于创建新对象cnn
- With cnn
- .Provider = "sqloledb"
- .ConnectionString = "data source=.;initial catalog=SuperMarket;user id=sa;password=;"
- .ConnectionTimeout = 10
- .Open
- End With
- IsConnect = True '设置连接标记,表示已经连接到数据库
- Exit Sub
- DbOpenErr:
- If err = -2147467259 Then
- Set cnn = Nothing
- MsgBox "连接MS SQL Server数据库失败!" & vbCrLf & vbCrLf & "请检查配置是否完好,数据库SuperMarket是否存在?", vbOKOnly + vbInformation, "学籍管理系统"
- End
- End If
- End Sub
- Public Sub Disconnect() '断开与数据库的连接
- Dim rc As Long
- If IsConnect = False Then Exit Sub '如果连接标记为假,标明已经断开连接,则直接返回
- cnn.Close '关闭连接
- Set cnn = Nothing
- IsConnect = False
- End Sub
- Public Sub DB_Connect() '使用Connect_Num控制数据库连接
- Connect_Num = Connect_Num + 1
- Connect
- End Sub
- Public Sub DB_Disconnect()
- If Connect_Num >= CONNECT_LOOP_MAX Then
- Connect_Num = 0
- Disconnect
- End If
- End Sub
- Public Sub DBapi_Disconnect() '强制关闭api方式访问的数据库,计数器复位
- Connect_Num = 0
- Disconnect
- End Sub
- Public Sub SQLExt(ByVal TmpSQLstmt As String) '执行数据库操作语句
- Dim cmd As New ADODB.Command '创建Command对象cmd
- DB_Connect '连接到数据库
- Set cmd.ActiveConnection = cnn '设置cmd的ActiveConnection属性,指定与其关联的数据库连接
- cmd.CommandText = TmpSQLstmt '设置要执行的命令文本
- cmd.Execute
- Set cmd = Nothing
- DB_Disconnect
- End Sub
- Public Function QueryExt(ByVal TmpSQLstmt As String) As ADODB.Recordset '执行数据库查询语句
- Dim rst As New ADODB.Recordset
- DB_Connect '连接到数据库
- Set rst.ActiveConnection = cnn '设置rst的ActiveConnection属性,指定与其关联的数据库连接
- rst.CursorType = adOpenKeyset
- rst.LockType = adLockOptimistic '设置锁定类型
- rst.Open TmpSQLstmt '打开记录集
- Set QueryExt = rst '返回记录集
- End Function