Vb.net Visual Studio 使用更新命令时出现问题
Problem in Vb.net Visual Studio Using Update Command
您好,我正在从 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
您好,我正在从 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