您好,我正在从 sql 数据库创建一个搜索功能和更新按钮,我需要能够在其中搜索某些名称等,但还需要能够在数据集中进行更改并保存它们。到目前为止,搜索功能按照我想要的方式工作,但是,更新按钮并没有真正保存更改,因为当我停止并重新启动代码时,即使在我尝试更新它以更改某些值之后,它也会恢复为默认数据集。关于我做错了什么有什么想法吗?您可能建议编辑数据集中的插入和删除值的其他方法?任何事情都有帮助!谢谢你!
Imports System.Data.SqlClient
Imports System.Data.Common
Public 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
为了从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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)