在我的項目,我需要對登錄我的數據庫執行的所有查詢。作爲一個例子,我們可以在這裏使用員工用戶數據。在I類有一個函數生成與參數的命令如下:參數化查詢作爲字符串
Public Function SQLUpdate(ByVal conn As SqlClient.SqlConnection) As SqlClient.SqlCommand Implements IDbConnected.SQLUpdate
Dim sqlstatement As String = "UPDATE Persons SET [email protected], [email protected], [email protected], [email protected], " & _
"[email protected], [email protected],[email protected], [email protected], [email protected], [email protected]"
sqlstatement &= " WHERE ID=" & Me.ID
Dim comm As New SqlClient.SqlCommand(sqlstatement, conn)
With comm.Parameters
.Add("@act", SqlDbType.Bit).Value = Me.Active
.Add("@abbr", SqlDbType.VarChar).Value = Me.Abbreviation
.Add("@first", SqlDbType.VarChar).Value = Me.FirstName
.Add("@last", SqlDbType.VarChar).Value = Me.LastName
.Add("@bday", SqlDbType.SmallDateTime).Value = Me.Birthday
.Add("@email", SqlDbType.VarChar).Value = Me.Email
.Add("@tel", SqlDbType.VarChar).Value = Me.Telephone
.Add("@fax", SqlDbType.VarChar).Value = Me.Fax
.Add("@reg", SqlDbType.Bit).Value = Me.Registered
.Add("@adm", SqlDbType.Bit).Value = Me.Administrator
End With
Return comm
End Function
當我請求命令文本
comm.CommandText
然後我得到仍然是parametrizid查詢
UPDATE人員SET Active = @ act,縮寫= @ abbr,FirstName = @ first,LastName = @ last,Birthday = @ bday,Email = @ email,Tel = @ tel,Fax = @ fax,Registered = @ reg,Admin = @ adm WHERE ID = 2
off course我需要查詢的參數被替換的值。有沒有一個簡單的方法來做到這一點,或者我需要編程一個函數來替代它自己?
+1肯定保持參數化。只需添加一下,可以方便地將輸出以SSMP的讀取運行形式輸出 - 因此,我之前剛剛編寫了一個方法,可以將每個參數(正確類型)的DECLARE語句與適當的命令串行化,並且然後在實際的命令文本之前設置這些變量。意思是你可以直接複製n粘貼到日誌文件中直接輸入SSMS – AdaTheDev 2010-11-15 15:17:36
,因爲這個問題純粹是爲了日誌記錄的目的,所以我會考慮將它們附加到字符串中。我想我正在讓事情比他們應該做的更困難。謝謝! – Geoffrey 2010-11-15 15:27:19