我在用VB6.0向sqlserver数据库Pub_Exam的Pub_Exam表写入数据,其中一字段Exam_GUID不能为空,且是自动生成的GUID(类似16进制的代码,中间有分隔,无大括号),我在使用newid()的方式写入数据,但是使用数据库连接的.execute不能使用(Selectsqlbd2.Execute这一段),编译时提示必选参数,麻烦帮看看。
以下是vb6.0代码:
Private Function Selectsqlbd2(SQL As String) As ADODB.Recordset
Dim ConnStr As String
Set rrsbd2 = New ADODB.Recordset
Set connbd2 = New ADODB.Connection
On Error GoTo MyErr:
ConnStr = "Provider=SQLOLEDB.1;Persist Security Info=false;User ID=" + Trim(Text36.Text) + ";PWD=" + Trim(Text71.Text) + ";Initial Catalog=" + Trim(Text194.Text) + ";Data Source=" + Trim(Text58.Text) + ""
connbd2.open ConnStr
rrsbd2.CursorLocation = adUseClient
rrsbd2.open Trim$(SQL), connbd2, adOpenDynamic, adLockOptimistic
Set Selectsqlbd2 = rrsbd2
Exit Function
MyErr:
rrsbd2.Close
connbd2.Close
Set rrsbd2 = Nothing
Set connbd2 = Nothing
MsgBox "无法连接机构服务器,请检数据库或服务器!", vbInformation, "系统提示"
End Function
Dim SQL As String
SQL = "SELECT * FROM Pub_Exam WHERE Exam_HisCode='" & Exam_HisCode & "'"
Set rrsbd2 = Selectsqlbd2.Execute(SQL)
If rrsbd2.EOF Then
rrsbd2.AddNew
rrsbd2("Exam_HisCode") = Exam_HisCode
rrsbd2("Pat_Name") = Pat_Name
rrsbd2("Pat_Sex") = Pat_Sex
' 使用SQL Server的NEWID()函数生成新的GUID
rrsbd2("Exam_GUID") = Selectsqlbd2.Execute("SELECT NEWID() AS NewGuid").Fields("NewGuid").Value
Else
' 如果找到记录,更新现有记录
rrsbd2.Edit
rrsbd2("Pat_Name") = Pat_Name
rrsbd2("Pat_Sex") = Pat_Sex
rrsbd2.Update
End If