我做了這個程序,以幫助我重新使用選擇查詢:如果參數的數據類型是一個Object,我該如何將我查詢的值賦給一個String,它是一個過程中的參數?
Private Sub selectQry(ByVal myColumn As String, ByVal myTable As String, ByVal myFilter As String, ByVal myObjectOne As Object, ByVal myObj As Object)
Dim qrySlctCaldate As String = "SELECT " + myColumn + " FROM " + myTable + " WHERE " + myFilter + " = '" & Replace(myObjectOne, "'", "''") & "'"
Dim cmdSlct As New SqlCommand(qrySlctCaldate, transConn.Connection)
Dim readSCalDate As SqlDataReader
readSCalDate = cmdSlct.ExecuteReader
While readSCalDate.Read
If TypeOf (myObj) Is TextBox Or TypeOf (myObj) Is ComboBox Then
myObj.Text = readSCalDate.Item(myColumn).ToString
Else
myObj = readSCalDate.Item(myColumn).ToString
End If
End While
readSCalDate.Close()
我這樣使用它,如果我想放置在文本框中選擇的值,它工作正常
selectQry("ProcConvDescription", "Line", "LineCode", nameValue.Value, txtProcess)
但是,如果我想在一個字符串傳遞的價值,像這樣:
selectQry("LastCalibrationDate", "EquipmentItem", "ControlNo", txtControlNo.Text, strCalDate)
字符串結束有一個空字符串值。如何將我查詢的值分配給該字符串?
爲什麼我不認爲這是超出我的。謝謝!但是,我怎麼不能將值分配給字符串的obj – Kurusu
@Kurusu您正在執行的任務僅適用於本地變量。爲了影響調用方法的變量,您需要按照Bryce的建議傳遞ByRef。 –