关闭

几个模块常用的自定义变量和函数

标签: integerfunctionstringsql数据库api
843人阅读 评论(0) 收藏 举报

const模块:

'数据库访问常量

Public Const SQL_C_CHAR As Long = 1

Public Const SQL_COLUMN_LABEL As Long = 18

Public Const SQL_DROP As Long = 1

Public Const SQL_ERROR As Long = -1

Public Const SQL_NO_DATA_FOUND As Long = 100

Public Const SQL_SUCCESS As Long = 0

Public Const SQL_ATTR_MAX_LENGTH As Long = 100

Public Const SQL_SUCCESS_WITH_INFO As Long = 1

Public Const VerNum As String = "版本 1.0.01"

Public Const GenDate As String = "日期 2006-6-25"

'数据源信息常量

Public Const DATABASE As String = "newdb"

Public Const DSN As String = "newdb"

Public Const DB_USER_NAME As String = "sa"

Public Const DB_PASSWORD As String = "123"

Public Const CONNECT_LOOP_MAX = 10

dbfunction模块:

Public Declare Function SQLAllocEnv Lib "odbc32.dll" (phenv&) As Integer

Public Declare Function SQLAllocConnect Lib "odbc32.dll" (ByVal Henv&, phdbc&) As Integer

Public Declare Function SQLAllocStmt Lib "odbc32.dll" (ByVal Hdbc&, phstmt&) As Integer

Public Declare Function SQLConnect Lib "odbc32.dll" (ByVal Hdbc&, ByVal szDSN$, ByVal cbDSN%, ByVal szUID$, ByVal cbUID%, ByVal szAuthStr$, ByVal cbAuthStr%) As Integer

Public Declare Function SQLColAttributesString Lib "odbc32.dll" Alias "SQLColAttributes" (ByVal Hstmt&, ByVal icol%, ByVal fDescType%, ByVal rgbDesc As String, ByVal cbDescMax%, pcbDesc%, pfDesc&) As Integer

Public Declare Function SQLDisconnect Lib "odbc32.dll" (ByVal Hdbc&) As Integer

Public Declare Function SQLExecDirect Lib "odbc32.dll" (ByVal Hstmt&, ByVal szSqlStr$, ByVal cbSqlStr&) As Integer

Public Declare Function SQLFetch Lib "odbc32.dll" (ByVal Hstmt&) As Integer

Public Declare Function SQLFreeConnect Lib "odbc32.dll" (ByVal Hdbc&) As Integer

Public Declare Function SQLFreeEnv Lib "odbc32.dll" (ByVal Henv&) As Integer

Public Declare Function SQLFreeStmt Lib "odbc32.dll" (ByVal Hstmt&, ByVal fOption%) As Integer

Public Declare Function SQLGetData Lib "odbc32.dll" (ByVal Hstmt&, ByVal icol%, ByVal fCType%, ByVal rgbValue As String, ByVal cbValueMax&, pcbValue&) As Integer

Public Declare Function SQLNumResultCols Lib "odbc32.dll" (ByVal Hstmt&, pccol%) As Integer

'数据库访问常量

Public Const SQL_C_CHAR As Long = 1

Public Const SQL_COLUMN_LABEL As Long = 18

Public Const SQL_DROP As Long = 1

Public Const SQL_ERROR As Long = -1

Public Const SQL_NO_DATA_FOUND As Long = 100

Public Const SQL_SUCCESS As Long = 0

Public Const SQL_ATTR_MAX_LENGTH As Long = 100

'声明变量
Private Isconnect As Boolean
Private Connect_Num As Integer
Private Henv As Long
Private Hdbc As Long
Private Rc As Long
Private Hstmt As Long

'自定义的数据库操作函数
Private Sub connect()
Dim TmpStat As String 'SQLConnect()函数的返回值
'如果连接标记为真,则返回,否则会出错
If Isconnect = True Then
Exit Sub
End If
'分配环境句柄,保存在变量Henv中
If SQLAllocEnv(Henv) Then
MsgBox "无法初始化ODBC环境!", , "ODBC API 执行错误"
End
'根据连接句柄、数据源、用户名和密码连接指定的数据库
TmpStat = SQLConnect(Hdbc, DSN, Len(DSN), DB_USER_NAME, lench(DB_USER_NAME), DB_PASSWORD, Len(DB_PASSWORD))
'如果连接不成功则退出程序
If TmpStat <> SQL_SUCCESS And TmpStat <> SQL_SUCCESS_WITH_INFO Then
MsgBox "无法获得句柄!", , "ODBC API执行错误"
Isconnect = True
disconnect
End
End If
Isconnect = True
End Sub

'断开与数据库的连接
Private Sub disconnect()
Dim Rc As Long
'如果连接标记为假,标明已经断开连接,则直接返回
If Isconnect = False Then
Exit Sub
End If
'断开连接
Rc = SQLDisconnect(Hdbc)
'释放连接句柄
Rc = SQLFreeConnect(Hdbc)
'释放环境句柄
Rc = SQLFreeEnv(Henv)
Isconnect = False
End Sub

'使用Connect_Num控制数据库连接
Public Sub DB_Connect()
Connect_Num = Connect_Num + 1
connect
End Sub

'断开数据库连接函数DB_Disconnect()
Public Sub DB_Disconnect()
If Connect_Num >= CONNECT_LOOP_MAX Then
Connect_Num = 0
disconnect
End If
End Sub

'强制关闭API方式访问的数据库,计数器复位
Public Sub DBapi_Disconnect()
Connect_Num = 0
disconnect
End Sub
'执行ODBC数据库操作语句
Public Sub ODBCExt(ByVal TmpSQLstmt As String)
'根据连接句柄,分配语句句柄
If SQLAllocStmt(Hdbc, Hstmt) Then
MsgBox "句柄分配失败", , "ODBC API 执行错误"
DBapi_Disconnect
End
End If
'执行SQL语句,lench是用户自定义的函数,计算包含汉字的字符串长度
If SQLExecDirect(Hstmt, TmpSQLstmt, lench(TmpSQLstmt)) Then
MsgBox "数据库访问语句失败", , "ODBC API执行错误"
MsgBox TmpSQLstmt
DBapi_Disconnect
End
End If

End Sub

GeneralFunc模块(用户自定义函数):

 Public Function In_Int(KeyAscii As Integer) As Boolean
Dim Ch_Accept_Int(20) As String
'可以接受的字符数组
Ch_Accept_Int(0) = "0"
Ch_Accept_Int(1) = "1"
Ch_Accept_Int(2) = "2"
Ch_Accept_Int(3) = "3"
Ch_Accept_Int(4) = "4"
Ch_Accept_Int(5) = "5"
Ch_Accept_Int(6) = "6"
Ch_Accept_Int(7) = "7"
Ch_Accept_Int(8) = "8"
Ch_Accept_Int(9) = "9"
Ch_Accept_Int(10) = Chr(8)
'检查输入字符是否在数组中
In_Int = False
For i = 0 To 10
If Chr(KeyAscii) = Ch_Accept_Int(i) Then
In_Int = True
End If
Next
End Function
'实现对文本框输入内容的限制
Private Sub text1_KeyPress(KeyAscii As Integer)
If In_Int(KeyAscii) = False Then
KeyAscii = 0
End If
End Sub
'判断指定的字符串是否在列表框中的函数
Public Function InCombo(ByVal Str As String, ByVal combol1 As ComboBox) As Boolean
i = 0
Do While i < combol1.List
If combom1.List(i) = Trim(Str) Then
InCombo = True
Exit Function
End If
i = i + 1
Loop
MsgBox Str + "不在列表中,请重新设置"
InCombo = False
End Function
'计算字符串长度的函数
Public Function lench(ByVal TmpStr As String) As String
lench = LenB(StrConv(TmpStr, vbFromUnicode))
End Function
'替换非法的字符的函数
Public Function TrimStr(ByVal Str As String) As String
If InStr(Str, Chr(0)) <= 0 Then
   TrimStr = Trim(Str)
Else
  TrimStr = Trim(Left(Str, InStr(Str, Chr(0) - 1)))
End If
End Function

 

 

 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:22718次
    • 积分:312
    • 等级:
    • 排名:千里之外
    • 原创:9篇
    • 转载:6篇
    • 译文:0篇
    • 评论:3条