什么';我的计数查询asp.net有问题吗

什么';我的计数查询asp.net有问题吗,asp.net,sql-server,vb.net,visual-studio,visual-studio-2010,Asp.net,Sql Server,Vb.net,Visual Studio,Visual Studio 2010,代码显示了一个错误 列名“California”无效 但是加州已经存在于我的州表中,我想统计我在州表中输入的州

代码显示了一个错误

列名“California”无效

但是
加州
已经存在于我的
表中,我想统计我在
表中输入的
下的所有城市

我希望输出为

Public state_name as String
state_name = Textbox1.Text

Dim constr As String = ConfigurationManager.ConnectionStrings("ApplicationServices").ConnectionString
Dim query As String = "SELECT Count(cities) FROM state_table WHERE state_name=" & state_name
Using conn As New SqlConnection(constr)
    Using comm As New SqlCommand()
        conn.Open()
        With comm
            .Connection = conn
            .CommandText = query
            .CommandType = CommandType.Text
        End With

        Dim count As Int16 = Convert.ToInt16(comm.ExecuteScalar())
        Label1.Text = count
    End Using
End Using

因为变量周围没有引号<代码>“州名=”+州名+“”


但是,您应该改为使用参数。

因为您没有用引号括住变量<代码>“州名=”+州名+“”


但是,您应该改为使用参数。

因为您没有用引号括住变量<代码>“州名=”+州名+“”


但是,您应该改为使用参数。

因为您没有用引号括住变量<代码>“州名=”+州名+“”


但是,您应该改用参数。

您希望使用参数化查询来避免


您希望使用参数化查询来避免


您希望使用参数化查询来避免


您希望使用参数化查询来避免


使用参数化查询,而不是将参数连接到SQL字符串中。添加
…state\u name='“&state\u name&'”
使用引号或仅将其添加到变量state\u name中使用参数化查询,而不是将参数连接到SQL字符串中。添加
…state\u name='“&state\u name&'”
使用引号或仅将其添加到变量状态名称中使用参数化查询,而不是将参数连接到SQL字符串中。添加
…状态名称='“&状态名称&”“
使用引号或仅在变量状态名称中添加引号之间的任何内容使用参数化查询,而不是将参数连接到SQL字符串中。添加
…state\u name='”&state\u name&“””
使用引号之间的任何内容或仅在变量状态名称中添加引号
California (3)
Dim constr As String = ConfigurationManager.ConnectionStrings("ApplicationServices").ConnectionString
Dim query As String = "SELECT Count(cities) FROM state_table WHERE state_name=@State_Name"
Using conn As New SqlConnection(constr)
   Using comm As New SqlCommand()
      conn.Open()
      With comm
         .Connection = conn
         .CommandText = query
         .CommandType = CommandType.Text
         .Parameters.AddWithValue("@State_Name", state_name)
      End With
      Dim count As Int16 = Convert.ToInt16(comm.ExecuteScalar())
      Label1.Text = count
   End Using
End Using