如何从文本框和绑定源中所做的更改更新SQL SERVER

时间:2016-04-28 17:42:04

标签: vb.net datagridview datatable

使用:SQL Server 2014,VS2015,VB

  1. 我的 Datagridview 填充了名为 DataTable
  2. 我有几个 Textboxes 与来自名为 bsAllData 的绑定源的相应值绑定。

    这很完美。它填充 Datagridview ,当我点击一行时,它会填充相应的文本框

    我需要的是,当我在文本框中进行更改时,我想按下命令按钮以使用新值更新SQL Server。这就是我被困住的地方。

  3. 以下代码正在填充Datagridview和Textboxes:

    ‘Button to display all records in the datagridview
     Private Sub btnViewAll_Click(sender As Object, e As EventArgs) Handles btnViewAll.Click
    
    ‘Connection to the Server
     Using connection3 As New SqlConnection("SERVER=x.x.x.x;database=ITDatabase;trusted_connection=yes;")
    
    ‘Variables declarations            
     Dim cmd As New SqlCommand("SELECT AssetNo [ASSET NO], t2.TYPE
     From ASSETINFO t1 
     INNER Join TypeInfo t2 on t1.TYPEID = t2.TYPEID 
      Order By AssetNo ASC;", connection3)
    
     Dim Adpt As New SqlDataAdapter(cmd)
     Dim table As New DataTable
    
            Adpt.Fill(table)
            bsAllData.DataSource = table
            dataGridAssetEdit.DataSource = bsAllData
    
         txtAssetNo2.DataBindings.Clear()
            txtAssetNo2.DataBindings.Add("Text", Me.bsAllData, "ASSET NO")
    
            cboAssetType2.DataBindings.Clear()
            cboAssetType2.DataBindings.Add("Text", Me.bsAllData, "Type")
      End Sub
    

    '要更新的按钮

      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
    
     ‘After searching for days I tried the following but doesn’t do anything.
      bsAllData.EndEdit()
      Adpt.Update(table)
    
      End Sub
    

    谢谢你,问候,

0 个答案:

没有答案