;===============================================================================
;
; 函数名称.........: _MSSQL_AddRecord
; 描述.............: 在数据库表中插入一个或多个新的数值
; 语法.............: _MSSQL_AddRecord($oConnectionObj, $sTable, $Values, $UNIQUE, $condition)
; 参数(s)..........: $oConnectionObj = Object, returned by _MSSQL_Con
; $sTable = 表名
; $Values = 要插入到表中的值
; $UNIQUE = [可选参数] 设置为 True 使用条件模式, 避免重复数据
; $condition = [可选参数] 条件模式, 如果避免重复数据的条件.
; 要求.............: You need to add a Value for each Column in the Table
; If $Values is an Array , it has to be indexed 1,
; If $Values is a String , it has to be formated like this:
; - 'Value1', 'Value2', 'Value3', 'Value4', 'Value5', 'Value n'
; 返回值(s)........: 成功 - 1
; 失败 - 0, sets @error
; |1 - $oConnectionObj is not an object
; |2 - $condition was not set
; |3 - Only returned if $UNIQUE = True
; - All Values already in Database
; 作者.............: TheLuBu <LuBu@veytal.com>
;
;===============================================================================
Func _MSSQL_AddRecord($oConnectionObj, $sTable, $Values, $UNIQUE = False, $condition = "")
Local $str, $check
If IsObj($oConnectionObj) And Not @error Then
If IsArray($Values) Then
If UBound($Values, 2) = 0 Then
$str = "INSERT INTO " & $sTable & " VALUES('"
For $grades = 1 To UBound($Values) - 1
If $UNIQUE = False Then
$str &= $Values[$grades] & "', '"
Else
If $condition = "" Then Return SetError(2, 0, 0)
$check = _MSSQL_GetRecord($oConnectionObj, $sTable, "*", $condition)
If @error = 4 Then
$str &= $Values[$grades] & "', '"
EndIf
EndIf
Next
$str = StringTrimRight($str, 3) & ");"
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $str = ' & $str & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
If StringRight($str, 7) = "VALUE);" Then Return SetError(3, 0, 0)
$oConnectionObj.execute($str)
Return 1
Else
For $rows = 1 To UBound($Values) - 1
$str = "INSERT INTO " & $sTable & " VALUES('"
For $grades = 1 To UBound($Values, 2) - 1
If $UNIQUE = False Then
$str &= $Values[$rows][$grades] & "', '"
Else
If $condition = "" Then Return SetError(2, 0, 0)
$check = _MSSQL_GetRecord($oConnectionObj, $sTable, "*", $condition)
If @error = 4 Then
$str &= $Values[$rows][$grades] & "', '"
EndIf
EndIf
Next
$str = StringTrimRight($str, 3) & ");"
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $str = ' & $str & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
If StringRight($str, 7) = "VALUE);" Then Return SetError(3, 0, 0)
$oConnectionObj.execute($str)
Next
Return 1
EndIf
Else
If $UNIQUE = False Then
$str = "INSERT INTO " & $sTable & " VALUES(" & $Values & ");"
Else
If $condition = "" Then Return SetError(2, 0, 0)
$check = _MSSQL_GetRecord($oConnectionObj, $sTable, "*", $condition)
If @error = 4 Then
$str = "INSERT INTO " & $sTable & " VALUES(" & $Values & ");"
Else
Return SetError(3, 0, 0)
EndIf
EndIf
$oConnectionObj.execute($str)
Return 1
EndIf
Else
Return SetError(1, 0, 0)
EndIf
EndFunc ;==>_MSSQL_AddRecord