2012-07-27 108 views
0

我是VB.NET和SQL中的新成员。我想更新我的数据库中的记录。我在我的数据库中做了一个假人。更新vb.net中的SQL语句

实施例中的值是:ID = 1,姓名=蛋白酶,年龄= 21

以我接口在VB.NET制成,我更新的值例如:名称= txtName.Text和年龄= txtAge.Text其中ID = 1。这是我的主要形式。在我的主窗体中,我有“查看”按钮,通知单击该按钮,将弹出新窗体,并查看用户更新的值。该程序没有任何错误,只是当我想再次更新SQL中的值时,它会记录BUT,当我再次单击“查看”按钮时,它会显示以前由用户输入的内容(运行界面时的第一次更新)。应该是什么解决方案?

这是我的代码:

的MainForm:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

    SQLConnection.ConnectionString = ServerString 
    Try 
     If SQLConnection.State = ConnectionState.Closed Then 
      SQLConnection.Open() 
      MessageBox.Show("Successful connection") 
     Else 
      'SQLConnection.Close() 
      MessageBox.Show("Connection is closed") 
     End If 
    Catch ex As Exception 
     MsgBox(ex.ToString) 

    End Try 

End Sub 

Public Sub SaveNames(ByRef SQLStatement As String) 
    Dim cmd As MySqlCommand = New MySqlCommand 
With cmd 
     .CommandText = SQLStatement 
     .CommandType = CommandType.Text 
     .Connection = SQLConnection 
     .ExecuteNonQuery() 
    End With 

    MsgBox("Successfully Added!") 

End Sub 

Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click 
    Dim date_now As String 

    date_now = Format(dtpDate.Value, "yyyy-MM-dd") 

    Dim SQLStatement As String = "UPDATE people SET name='" & txtName.Text & "', date ='" & date_now & "' WHERE 1" 
    SaveNames(SQLStatement) 
End Sub 

表2:(其中更新后的数据会被视为)

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

    SQLConnection.ConnectionString = ServerString 
    Try 
     If SQLConnection.State = ConnectionState.Closed Then 
      SQLConnection.Open() 
      '====retrieve/update values in database============= 
      Dim SQLStatement As String = "SELECT name, date FROM people" 
      ViewInfos(SQLStatement) 
     Else 
      'SQLConnection.Close() 
      MessageBox.Show("Connection is closed") 
     End If 
    Catch ex As Exception 
     MsgBox(ex.ToString) 

    End Try 
End Sub 

Public Sub ViewInfos(ByRef SQLStatement As String) 
    Dim cmd As MySqlCommand = New MySqlCommand 

    With cmd 
     .CommandText = SQLStatement 
     .CommandType = CommandType.Text 
     .Connection = SQLConnection 
     .ExecuteNonQuery() 

    End With 
    '--read the records in database in phpmyadmin gui--- 
    Dim myReader As MySqlDataReader = cmd.ExecuteReader 

    If myReader.Read Then 
     lblName.Text = myReader.GetString(0) 
     lblDate.Text = myReader.GetString(1) 

    End If 

    myReader.Close() 
    MsgBox("Records Successfully Retrieved") 

End Sub 

任何帮助,将不胜感激。谢谢!

+0

只是一个旁注:总是紧密联系立即(最好使用语句)。连接池实际上不会关闭它,但将其标记为_usable_。 – 2012-07-27 16:42:14

+0

我认为它应该是Dim SQLStatement As String =“UPDATE people SET name ='”&txtName.Text&“',date ='”&date_now&''WHERE ID = 1“ – Mithrandir 2012-07-27 16:44:33

+1

Noooo,不要连接字符串做sql语句! – King 2012-07-27 16:54:13

回答

0

当您更新sql时,您将保持连接字符串处于打开状态,因此当您尝试检索数据时,您的条件将关闭连接而不读取数据或更新文本框。

0

从ViewInfos()方法中取出.ExecuteNonQuery()。

通过表单名称不是通过变量引用您的形式:

Dim myForm as New Form2() 
myForm.Show()