简体   繁体   English

Vb.net Visual Studio 中使用更新命令的问题

[英]Problem in Vb.net Visual Studio Using Update Command

Hello I am creating a Search function and Update button from a sql database in which I need to be able to search for certain names and such but also need to be able to make changes within the dataset and save them.您好,我正在从 sql 数据库创建搜索 function 和更新按钮,我需要能够在其中搜索某些名称等,但还需要能够在数据集中进行更改并保存它们。 As of now the search function works as I want it to, however, the update button does not truly save the changes as when I stop and restart the code it reverts back to the default dataset even after I attempted to update it to change certain values.截至目前,搜索 function 可以按我的意愿工作,但是,更新按钮并没有真正保存更改,因为当我停止并重新启动代码时,即使我尝试更新它以更改某些值,它也会恢复为默认数据集. Any Ideas as to what I am doing wrong?关于我做错了什么的任何想法? Other ways you may suggest to edit insert and delete values in the dataset?您可能建议以其他方式编辑数据集中的插入和删除值? Anything helps!什么都有帮助! Thank You!谢谢你!

Imports System.Data.SqlClient导入 System.Data.SqlClient

Imports System.Data.Common导入 System.Data.Common

Public Class Form1公共 Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    Dim connection As New SqlConnection("Connection String PlaceHolder")
    Dim Table As New DataTable()
    
    Dim Adapter As New SqlDataAdapter("SELECT * FROM TrueTrack1", connection)
    
    Adapter.Fill(Table)
    
    DataGridView1.DataSource = Table
    load_data()



End Sub



Private ReadOnly queryTimer As New System.Threading.Timer(AddressOf runQuery, Nothing, -1, -1)
Private searchName As String
Private searchType As String
Private searchIP As String
Private textChangeQueryDelay As Integer = 1000

Private Sub TextBoxes_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
    searchName = TextBox1.Text
    searchType = TextBox2.Text
    searchIP = TextBox3.Text
    queryTimer.Change(textChangeQueryDelay, -1)
End Sub

Private Sub runQuery(state As Object)
    Dim table = New DataTable()
    Using connection = New SqlConnection("Connection String Placeholder")
        Using command = New SqlCommand()
            command.Connection = connection
            Dim commandText = "SELECT * FROM TrueTrack1 WHERE 1=1 "
            If Not String.IsNullOrEmpty(searchName) Then
                commandText &= " AND UserName like @name "
                command.Parameters.Add("@name", SqlDbType.VarChar, 100).Value = "%" & searchName & "%"
            End If
            If Not String.IsNullOrEmpty(searchType) Then
                commandText &= " AND DeviceType like @type "
                command.Parameters.Add("@type", SqlDbType.VarChar, 100).Value = "%" & searchType & "%"
            End If
            If Not String.IsNullOrEmpty(searchIP) Then
                commandText &= " AND IPAddress like @ip "
                command.Parameters.Add("@ip", SqlDbType.VarChar, 100).Value = "%" & searchIP & "%"
            End If
            command.CommandText = commandText
            Using adapter = New SqlDataAdapter(command)
                adapter.Fill(table)
            End Using
        End Using
    End Using
    DataGridView1.Invoke(Sub() DataGridView1.DataSource = table)
End Sub





Private Sub Button1_Click(sender As Object, e As EventArgs) 

End Sub

Dim da As New SqlDataAdapter
Dim dt As New DataSet

Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
    Dim cmd As New SqlCommandBuilder(da)
    Dim changes As New DataSet
    Dim table As New DataTable()




    changes = dt.GetChanges
    If changes IsNot Nothing Then
        da.Update(changes)
        da.Fill(dt)
        DataGridView1.DataSource = dt.Tables(0)
        load_data()
    End If
End Sub

Private Sub load_data()
    Dim conn As New SqlConnection("Connection String Placeholder")
    conn.Open()
    da = New SqlDataAdapter("Select * From TrueTrack", conn)
    dt.Clear()
    da.Fill(dt)
    DataGridView1.DataSource = dt.Tables(0)

    conn.Close()
End Sub

End Class结束 Class

In order to update(including edit, insert and delete) database and DataTable from DataGridView, you can refer to the following code.为了从 DataGridView 更新(包括编辑、插入和删除)数据库和 DataTable,您可以参考以下代码。

Private dt As DataTable = New DataTable
Private da As SqlDataAdapter
Private connection As SqlConnection = New SqlConnection("your connection string")
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    DataGridView1.EndEdit()
    da.Update(dt)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    bind_data()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    connection.Close()
End Sub
Private Sub bind_data()
    connection.Open()
    Dim cmdTxt As String = "SELECT * FROM TrueTrack"
    da = New SqlDataAdapter(New SqlCommand(cmdTxt, connection))
    Dim builder As SqlCommandBuilder = New SqlCommandBuilder(da)
    da.Fill(dt)
    Dim source As BindingSource = New BindingSource With {
                                    .DataSource = dt
                                  }
    DataGridView1.DataSource = source
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM