更新MS SQL

时间:2017-10-03 13:42:02

标签: sql vb.net boolean bit

我很难用这个。我似乎无法确定什么是错的。它没有更新我的投票统计数据,但它更新了投票数。

Dim myreader As SqlDataReader

'  Try

Dim president As New SqlClient.SqlCommand("SELECT * FROM PRESIDENT WHERE name=@name ", con) ' + ComboBox1.Text + "';", con)
president.Parameters.AddWithValue("@name", ComboBox1.Text)
myreader = president.ExecuteReader()

While myreader.Read()
    prevote = myreader.Item("VoteCount")
    prevote = prevote + 1
End While

myreader.Close()

president.CommandText = "Update President set [VoteCount]= " & prevote & " where [Name] ='" & ComboBox1.Text & "'"
president.Connection = con
president.ExecuteNonQuery()
president.Dispose()

Dim LRN As Int64
LRN = TextBox3.Text

Using Stud_Vote_Stat As New SqlClient.SqlCommand("UPDATE Voters SET [VoteStat] = 1  WHERE [VOTERSNAME] = @VOTERSNAME", con)
    Stud_Vote_Stat.Parameters.Add("@VOTERSNAME", SqlDbType.TEXT).Value = TextBox2.R
    Stud_Vote_Stat.ExecuteNonQuery()
End Using

1 个答案:

答案 0 :(得分:2)

以下内容应该有效:

'*** This part is presumably not needed
'*** if the "prevote"-variable is needed after the UPDATE,
'*** then use this part
'Dim prevote As Integer
'
'Using president As New SqlClient.SqlCommand("SELECT COUNT(*) FROM PRESIDENT WHERE name=@name", con)
'    president.Parameters.Add("@name", SqlDbType.VarChar).Value = ComboBox1.Text
'    prevote = CInt(president.ExecuteScalar) + 1
'End Using
'
'Using president As New SqlClient.SqlCommand("UPDATE President SET [VoteCount]=@prevote where [Name]=@name", con)
'   president.Parameters.Add("@prevote", SqlDbType.Int).Value = prevote
'   president.Parameters.Add("@name", SqlDbType.Int).Value = Name
'   president.ExecuteNonQuery()
'End Using
'******************************************

'*** Replaces upper part
'*** If VoteCount just should be incremented by 1 everytime,
'*** then use this part
'*** assumes that only one row exists per "name" in table "PRESIDENT"!
Using president As New SqlClient.SqlCommand("UPDATE President SET [VoteCount]=(CASE WHEN [VoteCount] IS NULL THEN 1 ELSE [VoteCount]+1 END) WHERE [Name]=@name", con)
    president.Parameters.Add("@name", SqlDbType.VarChar).Value = Name
    president.ExecuteNonQuery()
End Using
'****************************

Dim LRN As Int64
LRN = CLng(TextBox3.Text)

Using Stud_Vote_Stat As New SqlClient.SqlCommand("UPDATE Voters SET [VoteStat]=1 WHERE [LRN]=@LRN", con)
    Stud_Vote_Stat.Parameters.Add("@LRN", SqlDbType.BigInt).Value = LRN
    Stud_Vote_Stat.ExecuteNonQuery()
End Using