Posts

Showing posts with the label sqldatareader

ExecuteReader, Make field variable

Image
Clash Royale CLAN TAG #URR8PPP ExecuteReader, Make field variable I want to make the Data Field a variable I wrote the code as follows. The SQL works but when I try to get the returned value it returns +StrVariable+ if I remove the + then it returns Strvariable literally. Private Function FUNCTStrSN(StrVariable As String, StrSN As String) As String Dim sqlConn As SqlConnection Dim sqlComm As SqlCommand Dim r As SqlDataReader Dim sqlstring As String sqlstring = "Select " + StrVariable + " FROM HistorySNUnit WHERE SN='" + StrSN + "'" sqlConn = New SqlConnection(DBConnection) : sqlConn.Open() : sqlComm = New SqlCommand(sqlstring, sqlConn) : r = sqlComm.ExecuteReader() While r.Read() Dim DBSN As String = CStr(r("StrVariable")) StrSN = DBSN End While : r.Close() : sqlConn.Close() FUNCTStrSN = StrSN End Function How do I retrieve the value correctly? Thank you! ...