0

変数 Itemtext は既に宣言されています。なぜこのエラー??

Try
        conn = New SqlConnection(connString)
        conn.Open()
        Dim i As Integer = 0
        Using cmd As New SqlCommand()
            For Each c In item
                cmd.Connection = conn
                cmd.CommandType = CommandType.Text
                cmd.CommandText = "INSERT INTO tblItems(Item,Numberof) Values (@Itemtext,@Numberof)"
                cmd.Parameters.AddWithValue("@Itemtext", c)
                cmd.Parameters.AddWithValue("@Numberof", num(i))
                cmd.ExecuteNonQuery()
                i = i + 1
            Next
        End Using
4

1 に答える 1

0

ループのたびにパラメーターを追加しているため

   Using cmd As New SqlCommand() 
      cmd.Connection = conn 
      cmd.CommandType = CommandType.Text 
      cmd.CommandText = "INSERT INTO tblItems(Item,Numberof) Values (@Itemtext,@Numberof)" 
      Dim ItemParameter As SqlParameter = cmd.Parameters.Add("@Itemtext",SqlDbType.VarChar)
      Dim NumberParameter As SqlParameter  = cmd.Parameters.Add("@Numberof", SqlDbType.Int) 
      For Each c In item 
            ItemParameter.Value = c
            NumberParameter.Value = num(i)
            cmd.ExecuteNonQuery() 
            i = i + 1 
      Next 
    End Using 
于 2012-09-08T07:27:27.400 に答える