2

データセットを使用せずに、2 つの変数を使用して winform から更新クエリを実行しようとしています。両方の変数を割り当ててからクエリを実行しましたが、zcomp が有効な列名ではないというエラーが表示され続けます。もちろんこれは正しいですが、= zcomp と言う前にどの列かを伝えます。以下は、クエリを実行している私のコードです。

Dim zamnt As Integer = WopartsDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
Dim zcomp As Integer = gridRow.Cells(0).Value

Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
  con.ConnectionString = "Data Source=MNT-MGR-2\SQLEX;Initial Catalog=MT;Integrated Security=True"
  con.Open()
  cmd.Connection = con
  cmd.CommandText = "UPDATE dbo.sparts SET [dbo.sparts.QtyonHand] = [dbo.sparts.QtyonHand] - zamnt WHERE [ComponentID] = zcomp"
  cmd.ExecuteNonQuery()
Catch ex As Exception
  MessageBox.Show("Error while updating record on table..." & ex.Message, "Update Records")
Finally
  con.Close()
  gridRow.Cells(4).Value = "Yes"
End Try

私はそれをいくつかの異なる方法で試しました。zamnt と zcomp を取り出して、変数にある実際の数値を入れれば、問題なく動作します。この更新クエリで変数を使用する方法を一日中探していました。ありがとう、ステイシー

4

3 に答える 3

0

これを試しましたか?

Dim zamnt As Integer = WopartsDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
Dim zcomp As Integer = gridRow.Cells(0).Value

Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
  con.ConnectionString = "Data Source=MNT-MGR-2\SQLEX;Initial Catalog=MT;Integrated Security=True"
  con.Open()
  cmd.Connection = con
  cmd.CommandText = "UPDATE dbo.sparts SET [dbo.sparts.QtyonHand] = [dbo.sparts.QtyonHand] -" + zamnt + " WHERE [ComponentID] =" + zcomp
  cmd.ExecuteNonQuery()
Catch ex As Exception
  MessageBox.Show("Error while updating record on table..." & ex.Message, "Update Records")
Finally
  con.Close()
  gridRow.Cells(4).Value = "Yes"
End Try
于 2013-08-08T04:00:22.853 に答える
0

パラメータの使用に加えて、"Using" ステートメントは接続を閉じ、リソースを破棄します。

    Dim zamnt As Integer = WopartsDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
    Dim zcomp As Integer = gridRow.Cells(0).Value

    Try
        Using con As New SqlConnection("Data Source=MNT-MGR-2\SQLEX;Initial Catalog=MT;Integrated Security=True")
            con.Open()
            Using cmd As New SqlCommand
                cmd.CommandText = "UPDATE dbo.sparts SET [dbo.sparts.QtyonHand] = [dbo.sparts.QtyonHand] - @zamnt WHERE [ComponentID] = @zcomp"
                cmd.Parameters.AddWithValue("@zamt", zamnt)
                cmd.Parameters.AddWithValue("@zcomp", zcomp)
                cmd.ExecuteNonQuery()
            End Using
        End Using
    Catch ex As Exception
        MessageBox.Show("Error while updating record on table..." & ex.Message, "Update Records")
    Finally
        con.Close()
        gridRow.Cells(4).Value = "Yes"
    End Try
于 2012-11-06T21:23:16.987 に答える
0

おそらく、ADO.NET でパラメーターを使用する方法を探しているでしょう。あなたの例では、次のようになります。

cmd.Parameters.Add("@zamnt", zamnt);
cmd.Parameters.Add("@zcomp", zcomp);

この 2 行を の前の任意の場所に置きますExecuteNonQuery

パラメータには@プレフィックスが必要なため、クエリを単に,@zamntの代わりに次のように変更する必要があります。zamntzcomp

cmd.CommandText = "UPDATE dbo.sparts SET [dbo.sparts.QtyonHand] = [dbo.sparts.QtyonHand] - @zamnt WHERE [ComponentID] = @zcomp"
于 2012-11-06T21:11:23.350 に答える