如何仅使用access和vb.net在特定列中插入数据

时间:2018-03-23 04:54:22

标签: vb.net ms-access

我在msaccess中有3列。 NAME | AGE | STATUSnameagerequired key value pair that you want在数据库中有数据,Status列为空。如何使用vb.net和文本框将数据仅插入“状态”列。

1 个答案:

答案 0 :(得分:0)

<强>第一

您需要设置连接。

e.g。

Imports System.Data.SqlClient
Imports System.Data

Public Class SQLTools
     Inherits System.Windows.Forms.Form
     Private Const SqlString As String = "Server= ;Database= ;User ID= ;Password= ;Integrated Security=SSPI;"
     Private myConn As SqlConnection
     Private myCmd As SqlCommand
     Private myReader As SqlDataReader
     Private Sub Conn()
         myConn = New SqlConnection(SqlString)
     End Sub

<强> SECOND

现在您可以编写更新查询代码

Sub UpdateQuery(ByVal PK As Long, ByVal Val as String)        
    Dim Sql As String= "update TABLE_NAME set Status = @Val Where TABLE_PK = @PK"
    myCmd = New SqlCommand(Sql, myConn)
    myCmd.Parameters.Add("@PK", SqlDbType.Int).Value = PK
    myCmd.Parameters.Add("@Val", SqlDbType.VarChar).Value = Val
    myCmd.ExecuteNonQuery()
    MsgBox("Update done!")
    myConn.Close()
End Sub

<强> FINAL

现在你必须叫你Sub传递你的Textbox值ByVal。

e.g。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    UpdateQuery(tablePk, TextBox1.Text)
End Sub