0

vb.net内では、以下のコードが機能します

cmd.CommandText = "CREATE TABLE Notes ( num INTEGER, name TEXT)"
Try
      cmd.ExecuteNonQuery()
Catch ex As Exception
      MsgBox("Error during save operation: " & ex.Message)
End Try

cmd.CommandText = "insert into Notes(num , name ) VALUES( 3, 'Bob') "
Try
    Console.WriteLine(cmd.ExecuteNonQuery())
Catch ex As Exception
    MsgBox("Error during save operation: " & ex.Message)
End Try

しかし、数字3を挿入する代わりに整数変数を入れたいと思います。私はこれを試しました:

Dim count As Integer = 1
cmd.CommandText = "insert into Notes(num , name ) VALUES(" + count " + , 'Bob') "

Try
     Console.WriteLine(cmd.ExecuteNonQuery())
Catch ex As Exception
      MsgBox("Error during save operation: " & ex.Message)
End Try

しかし、私には例外があります:

Conversion from string "insert into Notes(isFirst" to type 'Double' is not valid.

私は次の行を知っています:

"insert into Notes(num , name ) VALUES(" + count + ", 'Bob') "

完全に間違っているようですが、すべて試したので、他の解決策は見つかりません。

カウント変数をテーブルに挿入するにはどうすればよいですか?

前もって感謝します。

4

2 に答える 2

1

クエリをパラメータ化する必要があります。私は Oracle を使用しているため、SQLite の正確な構文についてはわかりませんが、これで間違いなく正しい方向に進むはずです。

Dim count As Integer
'Whatever code you need to get the count from the user.

cmd.CommandText = "insert into Notes(num , name ) VALUES( @count, 'Bob')"

Dim myParam As New SQLiteParameter("@count")
cmd.Parameters.Add(myParam)

'Continue with executing the query, storing the result, etc.
于 2012-08-15T18:44:12.627 に答える
1

二重引用符の反対側に「+」記号があります。

cmd.CommandText = "insert into Notes(num , name ) VALUES(" + count " + , 'Bob') "

する必要があります

cmd.CommandText = "insert into Notes(num , name ) VALUES(" & count & ", 'Bob') "

ChrisStauffer が言及しているように、パラメーターを使用します。いつも。

于 2012-08-15T18:48:22.690 に答える