0

我有以下代码,其中表名是学生,列名sem_of_study来自INT

Dim userquery As String = "select sem_of_study from student where username='" + Session("user") + "'"
Dim usercom As New SqlCommand(userquery, conn)
Dim a = usercom.ExecuteReader

我想知道是否可以将 SQL 命令转换为整数,以便在IF语句中使用它!

所以变量 a 会

if a=1 then 

else if a=2 then
4

1 回答 1

1

除了您应该使用的事实parameterized queries(搜索 SO - 大量示例)之外,您还需要DataReader在行中执行以下操作:

Dim a as Integer
Dim myReader As SqlDataReader = usercom.ExecuteReader()

If myReader.HasRows Then
  myReader.Read()
  a = myReader.GetInt32(0)
End If

对于参数:

Dim userquery As String = "select sem_of_study from student where username=@username"
Dim usercom as SqlCommand = new SqlCommand()
usercom.Connection = conn
usercom.CommandType = CommandType.Text
usercom.CommendText = userquery 
usercom.Parameters.AddWithValue("username", Session("user"))
于 2013-07-15T00:25:39.527 回答