如果之前有人问过,我会道歉,但我甚至不确定如何搜索。
我正在使用VB代码&希望我试图找出这个;
我有一个已定义的SqlCommand
:
Dim cmd As New SqlCommand("Select [Field1], [Field2], [Field3] From [Table] Where [FieldX] = @parameter ")
如何使用代码添加其他字段而不更改基础SqlCommand
?
所以临时SqlCommand
如下所示:
Dim cmd As New SqlCommand("Select [Field1], [Field2], [Field3] [Field4 Added Dynamically] From [Table] Where [FieldX] = @parameter ")
如果我对任何人或每个人感到困惑,请告诉我,我会尽力让解释清楚。
MC
答案 0 :(得分:1)
这是你在找什么?
Dim DynamicField = "FieldToAdd"
Dim cmd As New SqlCommand("Select [Field1], [Field2], [Field3], " + DynamicField + " From [Table] Where [FieldX] = @parameter ")
我不记得确切的VB语法,但你可以使用replace:
Dim DynamicField = "FieldToAdd"
Dim Sql = "Select [Field1], [Field2], [Field3], ##DynamicField## From [Table] Where [FieldX] = @parameter "
Sql = Sql.Replace("##DynamicField##", DynamicField)
Dim cmd As New SqlCommand(Sql)