vb.net datagridview数据修改同步到数据库
Textbox1输入查询命令(select语句后查询结果在datagridview中显示,在datagridview中修改数据按保存按钮数据更新到数据库)查询没有错误,但...
Textbox1输入查询命令(select语句后查询结果在datagridview中显示,在datagridview中修改数据按保存按钮数据更新到数据库)
查询没有错误,但修改数据后保存出现错误 ,错误行在下面代码红色标出了,错误提示未将对象引用设置到对象的实例,我查询的表设置了主键
Imports System.Data.SqlClient
Public Class Form1
Dim con As New SqlConnection
Dim Com As SqlCommand
Dim da As SqlDataAdapter
Dim ds As DataSet
'查询按钮代码
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
con = New SqlConnection("data source=mypc;initial catalog=xiaoshouxt;" & "integrated security=false;user=sa;password=8348538620")
con.Open()
Com = New SqlCommand(TextBox1.Text, con)
Dim da As SqlDataAdapter
Dim ds As DataSet
da = New SqlDataAdapter(com)
ds = New DataSet
da.Fill(ds, "ch")
DataGridView1.DataSource = ds.Tables("ch")
End Sub
'修改datagridview1中数据保存按钮
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
con = New SqlConnection("data source=mypc;initial catalog=xiaoshouxt;" & "integrated security=false;user=sa;password=8348538620")
con.Open()
'保存表身修改后的数据
Dim A As New SqlCommandBuilder(da)
da.Update(ds, "ch")
con.close()
End Sub
End Class
更多 0 展开
查询没有错误,但修改数据后保存出现错误 ,错误行在下面代码红色标出了,错误提示未将对象引用设置到对象的实例,我查询的表设置了主键
Imports System.Data.SqlClient
Public Class Form1
Dim con As New SqlConnection
Dim Com As SqlCommand
Dim da As SqlDataAdapter
Dim ds As DataSet
'查询按钮代码
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
con = New SqlConnection("data source=mypc;initial catalog=xiaoshouxt;" & "integrated security=false;user=sa;password=8348538620")
con.Open()
Com = New SqlCommand(TextBox1.Text, con)
Dim da As SqlDataAdapter
Dim ds As DataSet
da = New SqlDataAdapter(com)
ds = New DataSet
da.Fill(ds, "ch")
DataGridView1.DataSource = ds.Tables("ch")
End Sub
'修改datagridview1中数据保存按钮
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
con = New SqlConnection("data source=mypc;initial catalog=xiaoshouxt;" & "integrated security=false;user=sa;password=8348538620")
con.Open()
'保存表身修改后的数据
Dim A As New SqlCommandBuilder(da)
da.Update(ds, "ch")
con.close()
End Sub
End Class
更多 0 展开
展开全部
变量的作用域的概念错误:
改用下面代码:
Imports System.Data.SqlClient
Public Class Form1
Dim con As New SqlConnection
Dim Com As SqlCommand
Dim da As SqlDataAdapter
Dim ds As DataSet
'查询按钮代码
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
con = New SqlConnection("data source=mypc;initial catalog=xiaoshouxt;" & "integrated security=false;user=sa;password=8348538620")
con.Open()
Com = New SqlCommand(TextBox1.Text, con)
'Dim da As SqlDataAdapter '这行不要
'Dim ds As DataSet '这行不要
da = New SqlDataAdapter(com)
ds = New DataSet
da.Fill(ds, "ch")
DataGridView1.DataSource = ds.Tables("ch")
End Sub
'修改datagridview1中数据保存按钮
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'下面两行也不要
'con = New SqlConnection("data source=mypc;initial catalog=xiaoshouxt;" & "integrated security=false;user=sa;password=8348538620")
'con.Open()
'保存表身修改后的数据
Dim A As New SqlCommandBuilder(da)
da.Update(ds, "ch")
con.close()
End Sub
End Class
改用下面代码:
Imports System.Data.SqlClient
Public Class Form1
Dim con As New SqlConnection
Dim Com As SqlCommand
Dim da As SqlDataAdapter
Dim ds As DataSet
'查询按钮代码
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
con = New SqlConnection("data source=mypc;initial catalog=xiaoshouxt;" & "integrated security=false;user=sa;password=8348538620")
con.Open()
Com = New SqlCommand(TextBox1.Text, con)
'Dim da As SqlDataAdapter '这行不要
'Dim ds As DataSet '这行不要
da = New SqlDataAdapter(com)
ds = New DataSet
da.Fill(ds, "ch")
DataGridView1.DataSource = ds.Tables("ch")
End Sub
'修改datagridview1中数据保存按钮
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'下面两行也不要
'con = New SqlConnection("data source=mypc;initial catalog=xiaoshouxt;" & "integrated security=false;user=sa;password=8348538620")
'con.Open()
'保存表身修改后的数据
Dim A As New SqlCommandBuilder(da)
da.Update(ds, "ch")
con.close()
End Sub
End Class
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询