我的代码中有以下UPDATE命令(用VB编写)。
Dim currentUser As String = User.Identity.Name
Dim myConnectionString As String = ConfigurationManager.ConnectionStrings("DBConnection").ConnectionString
Dim myCommand As New SqlCommand("UPDATE tblProfile SET Title= @Title, FirstName= @FirstName, LastName= @LastName, MiddleName= @MiddleName, HomePhoneNumber= @HomePhoneNumber, MobilePhoneNumber= @MobilePhoneNumber, Address= @Address, StreetName= @StreetName, StreetType= @StreetType, Suburb= @Suburb, PostCode= @PostCode, State= @State WHERE UserName = '" & currentUser & "'", New SqlConnection(myConnectionString))
myCommand.Connection.Open()
myCommand.Parameters.AddWithValue("@Title", Title.SelectedItem.Text)
myCommand.Parameters.AddWithValue("@FirstName", FirstName.Text)
myCommand.Parameters.AddWithValue("@LastName", LastName.Text)
If MiddleNames.Text = String.Empty Then
myCommand.Parameters.AddWithValue("@MiddleName", DBNull.Value)
Else
myCommand.Parameters.AddWithValue("@MiddleName", MiddleNames.Text)
End If
If HomePhoneNumber.Text = String.Empty Then
myCommand.Parameters.AddWithValue("@HomePhoneNumber", DBNull.Value)
Else
myCommand.Parameters.AddWithValue("@HomePhoneNumber", HomePhoneNumber.Text)
End If
If MobilePhoneNumber.Text = String.Empty Then
myCommand.Parameters.AddWithValue("@MobilePhoneNumber", DBNull.Value)
Else
myCommand.Parameters.AddWithValue("@MobilePhoneNumber", MobilePhoneNumber.Text)
End If
myCommand.Parameters.AddWithValue("@Address", AddressNumber.Text)
myCommand.Parameters.AddWithValue("@StreetName", StreetName.Text)
myCommand.Parameters.AddWithValue("@StreetType", StreetType.SelectedItem.Text)
myCommand.Parameters.AddWithValue("@Suburb", Suburb.Text)
myCommand.Parameters.AddWithValue("@PostCode", Postcode.Text)
myCommand.Parameters.AddWithValue("@State", State.SelectedItem.Text)
myCommand.ExecuteNonQuery()
myCommand.Connection.Close()
Dim myCommandPref As New SqlCommand("UPDATE tblPreferences SET Classical = @Classical, Comedy = @Comedy, Concerts = @Concerts, Dance = @Dance, DiningOut = @DiningOut, Exhibitions = @Exhibitions, Family = @Family, Festivals = @Festivals, Lifestyle = @Lifestyle, Musicals = @Musicals, Opera = @Opera, Rock = @Rock, Sports = @Sports, Theatre = @Theatre WHERE UserName = '" & currentUser & "'", New SqlConnection(myConnectionString))
myCommandPref.Connection.Open()
Dim boolClassical As Boolean = Preferences.Items(0).Selected
myCommandPref.Parameters.AddWithValue("@Classical", boolClassical.ToString)
Dim boolComedy As Boolean = Preferences1.Items(0).Selected
myCommandPref.Parameters.AddWithValue("@Comedy", boolComedy.ToString)
Dim boolConcerts As Boolean = Preferences.Items(1).Selected
myCommandPref.Parameters.AddWithValue("@Concerts", boolConcerts.ToString)
Dim boolDance As Boolean = Preferences1.Items(1).Selected
myCommandPref.Parameters.AddWithValue("@Dance", boolDance.ToString)
Dim boolDiningOut As Boolean = Preferences.Items(2).Selected
myCommandPref.Parameters.AddWithValue("@DiningOut", boolDiningOut.ToString)
Dim boolExhibitions As Boolean = Preferences1.Items(2).Selected
myCommandPref.Parameters.AddWithValue("@Exhibitions", boolExhibitions.ToString)
Dim boolFamily As Boolean = Preferences.Items(3).Selected
myCommandPref.Parameters.AddWithValue("@Family", boolFamily.ToString)
Dim boolFestivals As Boolean = Preferences1.Items(3).Selected
myCommandPref.Parameters.AddWithValue("@Festivals", boolFestivals.ToString)
Dim boolLifestyle As Boolean = Preferences.Items(4).Selected
myCommandPref.Parameters.AddWithValue("@Lifestyle", boolLifestyle.ToString)
Dim boolMusicals As Boolean = Preferences1.Items(4).Selected
myCommandPref.Parameters.AddWithValue("@Musicals", boolMusicals.ToString)
Dim boolOpera As Boolean = Preferences.Items(5).Selected
myCommandPref.Parameters.AddWithValue("@Opera", boolOpera.ToString)
Dim boolRock As Boolean = Preferences1.Items(5).Selected
myCommandPref.Parameters.AddWithValue("@Rock", boolRock.ToString)
Dim boolSports As Boolean = Preferences.Items(6).Selected
myCommandPref.Parameters.AddWithValue("@Sports", boolSports.ToString)
Dim boolTheatre As Boolean = Preferences1.Items(6).Selected
myCommandPref.Parameters.AddWithValue("@Theatre", boolTheatre.ToString)
myCommandPref.ExecuteNonQuery()
myCommandPref.Connection.Close()
当用户按下触发该代码的按钮时,我的页面只会刷新,但不会更新数据库中的信息。我环顾四周,有些人说你需要把主键作为'where'语句,所以我把'UserName'作为两个表中的主键。
有人可以帮我解决这个问题。
答案 0 :(得分:1)
我今天几个月后又回到了我的代码,经过短暂的搜索后发现了问题......我需要在我的Page_Load中包含'if not IsPostBack then ...'。每次按下按钮时我都会重置页面,这会重置页面上的字段,从而将相同的信息发送回我的服务器 - 我正在用相同的信息更新我的服务器。
对于有同样问题的人,这有助于我: http://www.java-samples.com/showtutorial.php?tutorialid=1083