从数据库中选择和更新数据

时间:2014-04-07 14:33:44

标签: vb.net

我在数据库中有ID,FullName,Age,Sex作为文本框和类似列。 下面是我通过ID从数据库中搜索的代码。 但我不知道如何在搜索这些数据后进行更新。

Imports System.Data.OleDb
Public Class Form1

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

    End Sub

    Private Sub Src_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Src.Click
        Form2.Show()
        Dim Connection As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Satyam\Documents\Satyam.accdb;" & "Persist Security Info=False;" & "Jet OLEDB:Database Password=" & "your pass" & ";")

        Dim cmd As OleDbCommand
        Dim dr As OleDbDataReader
        Try
        Connection.Open()
        cmd = New OleDbCommand("SELECT *  from DailyWorkLoadRegister WHERE ID=" & txtID.Text & "", Connection)

        dr = cmd.ExecuteReader
        If dr.Read Then
            Form2.txtID.Text = dr("ID")
            Form2.txtAge.Text = dr("Age")
            Form2.txtSex.Text = dr("Sex")
            Form2.txtFullName.Text = dr("FullName")

            dr.Close()
        Else
            MsgBox("No Record")
        End If
        Catch
        End Try
        Connection.Close()


    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

您需要执行一个返回的非查询方法:

  

对于UPDATE,INSERT和DELETE语句,返回值为   受命令影响的行数。对于所有其他类型的   语句,返回值为-1。如果发生回滚,则返回   值也是-1。

Microsoft文档 - > Here

这很简单:

Dim ID : ID = Form2.txtID.Text
Dim Age : Age = Form2.txtAge.Text
Dim Sex : Sex = Form2.txtSex.Text
Dim FN : FN = Form2.txtFullName.Text


Dim command As New OleDbCommand("UPDATE YOUR_TABLE_NAME SET _
                                 FullName = '" & FN & "',_
                                 Age = '" & Age & "',_
                                 Sex = '" & Sex & "'_
                                 WHERE ID = '" & ID & "'", Connection)
command.ExecuteNonQuery()