📄 frmconn.frm
字号:
VERSION 5.00
Begin VB.Form frmConn
BorderStyle = 1 'Fixed Single
Caption = "设置数据库服务器"
ClientHeight = 3225
ClientLeft = 45
ClientTop = 435
ClientWidth = 5685
BeginProperty Font
Name = "宋体"
Size = 10.5
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Icon = "frmConn.frx":0000
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 3225
ScaleWidth = 5685
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton cmdCancel
Caption = "取 消"
Height = 435
Left = 3780
TabIndex = 7
Top = 2640
Width = 1335
End
Begin VB.CommandButton cmdSave
Caption = "保 存"
Height = 435
Left = 2070
TabIndex = 6
Top = 2640
Width = 1335
End
Begin VB.TextBox txtPassWord
Height = 315
IMEMode = 3 'DISABLE
Left = 1530
PasswordChar = "*"
TabIndex = 5
Top = 1410
Width = 3555
End
Begin VB.TextBox txtUserName
Height = 315
Left = 1530
TabIndex = 4
Top = 960
Width = 3555
End
Begin VB.TextBox txtBase
Height = 315
Left = 2310
TabIndex = 1
Top = 450
Width = 2745
End
Begin VB.Line Line2
BorderColor = &H00C0C0C0&
X1 = -30
X2 = 5670
Y1 = 2460
Y2 = 2460
End
Begin VB.Line Line1
BorderColor = &H00FFFFFF&
X1 = 30
X2 = 5700
Y1 = 180
Y2 = 180
End
Begin VB.Label lblCheck
Caption = "(√) 加载数据库并测试连接"
BeginProperty Font
Name = "宋体"
Size = 10.5
Charset = 134
Weight = 400
Underline = -1 'True
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00C00000&
Height = 210
Left = 210
MouseIcon = "frmConn.frx":08CA
MousePointer = 99 'Custom
TabIndex = 8
Top = 2040
Width = 2625
End
Begin VB.Label Label3
Caption = "帐号密码:"
Height = 210
Left = 360
TabIndex = 3
Top = 1470
Width = 1050
End
Begin VB.Label Label2
Caption = "帐号名称:"
Height = 210
Left = 360
TabIndex = 2
Top = 990
Width = 1050
End
Begin VB.Label Label1
Caption = "MS SQL服务器名称:"
Height = 210
Left = 360
TabIndex = 0
Top = 510
Width = 1890
End
End
Attribute VB_Name = "frmConn"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim Base As String
Dim UID As String
Dim PWD As String
Private Sub cmdCancel_Click()
Unload Me
End Sub
Private Sub cmdSave_Click()
Open App.Path & "\connaction" For Output As #1
Print #1, txtBase.Text & "/" & txtUserName.Text & "/" & txtPassWord.Text
Close #1
Unload Me
End Sub
Private Sub Form_Load()
Dim SysBase As String
If Dir(App.Path & "\connaction") = "" Then
Open App.Path & "\connaction" For Output As #1
Print #1, "sysbase/sa/sa"
Close #1
End If
Open App.Path & "\connaction" For Input Shared As #1
Input #1, SysBase
Close #1
Base = Mid(SysBase, 1, InStr(SysBase, "/") - 1)
SysBase = Mid(SysBase, InStr(SysBase, "/") + 1)
UID = Mid(SysBase, 1, InStr(SysBase, "/") - 1)
PWD = Mid(SysBase, InStr(SysBase, "/") + 1)
txtBase.Text = Base
txtUserName.Text = UID
txtPassWord.Text = PWD
txtBase.SelStart = 0
txtBase.SelLength = Len(txtBase.Text)
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
lblCheck.ForeColor = &HC00000
End Sub
Private Sub lblCheck_Click()
Dim Conn As ADODB.Connection
Dim Cmd As ADODB.Command
Dim Rst As ADODB.Recordset
Dim Rst As ADODB.Recordset
Dim SQL As String
On Error GoTo can
SQL = "Provider=SQLOLEDB.1;Password=" & txtPassWord.Text & ";Persist Security Info=True;" & _
"User ID=" & txtUserName.Text & ";Initial Catalog=master;Data Source=" & txtBase.Text
Set Conn = New ADODB.Connection
Conn.ConnectionString = SQL
Conn.Open
On Error Resume Next
Set Cmd = New ADODB.Command
SQL = "EXEC sp_attach_db @dbname = N'pos', " & _
"@filename1 = N'" & App.Path & "\pos_data.mdf'," & _
"@filename2 = N'" & App.Path & "\pos_Log.LDF'"
Cmd.ActiveConnection = Conn
Cmd.CommandText = SQL
Cmd.CommandType = adCmdText
Cmd.Execute
Set Cmd = Nothing
Set Rst = New ADODB.Recordset
Rst.CursorLocation = adUseClient
SQL = "select * from sysdatabases where name='pos'"
Rst.Open SQL, Conn, adOpenDynamic, adLockReadOnly, adCmdText
If Not Rst.EOF Then
MsgBox "测试数据库成功!", 64
Conn.Close
Set Conn = New ADODB.Connection
SQL = "Provider=SQLOLEDB.1;Password=" & txtPassWord.Text & ";Persist Security Info=True;" & _
"User ID=" & txtUserName.Text & ";Initial Catalog=pos;Data Source=" & txtBase.Text
Conn.ConnectionString = SQL
Conn.Open
Set Rst = New ADODB.Recordset
Rst.CursorLocation = adUseClient
SQL = "select * from sysseting"
Rst.Open SQL, Conn, adOpenStatic, adLockReadOnly, adCmdText
If Rst.EOF Then
Set Cmd = New ADODB.Command
Cmd.ActiveConnection = Conn
Cmd.CommandText = "insert into sysseting values ('" & Format(Date$, "yyyy-mm-dd") & "',15)"
Cmd.CommandType = adCmdText
Cmd.Execute
Set Cmd = Nothing
End If
Else
MsgBox "连接失败!", 16
End If
Rst.Close
Conn.Close
Set Rst = Nothing
Set Conn = Nothing
Exit Sub
can:
MsgBox "连接失败!", 16
End Sub
Private Sub lblCheck_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
lblCheck.ForeColor = &HFF0000
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -