使用VB更新Dataadapter

时间:2017-10-06 08:55:35

标签: vb.net sql-update

我希望有些vb专家可以告诉我我做错了什么。 我正在尝试打开,修改和更新Northwind MDF中的记录。 DA.Update(DS)语句始终生成错误。 我在网上找到了几个例子,但只有在C中,我才明白,只能在VB中工作。我发现自己很难理解更新机制。

我的来源:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Call GETNWDS()
        ContactNamebox.Text = ContactNamebox.Text + "   " + Now
        Call UpdateNWDS()
    End If
End Sub


Public Sub GETNWDS()
    '' open custumor  DataAdapter daaset DS

    Dim connectionString = ConfigurationManager.ConnectionStrings("NW testConnectionString").ConnectionString
    Dim queryString As String = "SELECT CustomerID, CompanyName, ContactName FROM Customers where CustomerID='ALFKI'"

    Dim CN As New SqlConnection(connectionString)
    Dim DA As SqlDataAdapter = New SqlDataAdapter(queryString, CN)
    Dim DS As DataSet = New DataSet
    DA.Fill(DS)
    Dim DT As DataTable = DS.Tables(0)
    Dim row As DataRow = DT(0)

    CustomerIDbox.Text = row("CustomerID").ToString
    CompanyNamebox.Text = row("CompanyName").ToString
    ContactNamebox.Text = row("ContactName").ToString

    CN.Close()
End Sub

Public Sub UpdateNWDS()

    Dim connectionString = ConfigurationManager.ConnectionStrings("NW testConnectionString").ConnectionString
    Dim queryString As String = "SELECT CustomerID, CompanyName, ContactName FROM Customers where CustomerID='ALFKI'"

    Dim CN As New SqlConnection(connectionString)
    Dim DA As SqlDataAdapter = New SqlDataAdapter(queryString, CN)
    Dim DS As DataSet = New DataSet
    DA.Fill(DS)
    Dim DT As DataTable = DS.Tables(0)
    Dim row As DataRow = DT(0)

    row("CustomerID") = CustomerIDbox.Text
    row("CompanyName") = CompanyNamebox.Text
    row("ContactName") = ContactNamebox.Text

    Textbox.Text = row("ContactName").ToString

    ' DA.Update(DS)  ' skip to avoud error
    CN.Close()

End Sub

webform正在填充3个DB项目。 然后更改1个框并调用更新toutineis,但更新语句中存在错误。 Skippimg此声明显示了此webform。 请注意,新行值在文本框中显示。

0 个答案:

没有答案