数据表中的数据不会更新 [英] Data din't Update in Database Table

查看:84
本文介绍了数据表中的数据不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,



我在vb.net 08中创建了一个应用程序,数据库是基于SQL服务的数据库,



现在我有代码:

 公共  Form1 
Dim upd As New Boolean
Private Sub Form1_Load( ByVal sender As System。对象 ByVal e 作为 System.EventArgs)句柄 MyBase .Load
' TODO:这行代码将数据加载到'MyDataBaseDataSet.tbl中用户表。您可以根据需要移动或删除它。
.TblUsersTableAdapter.Fill( .MyDataBaseDataSet.tblUsers)
结束 Sub

私有 Sub TblUsersBindingSource_ListChanged( ByVal sender < span class =code-keyword>作为 对象 ByVal e As System.ComponentModel.ListChangedEventArgs)句柄 TblUsersBindingSource.ListChanged
如果 MyDataBaseDataSet.HasChanges 然后
upd = True
结束 如果
结束 Sub

私有 Sub DataGridView1_RowValidated( ByVal sender As 对象 ByVal e 作为系统。 Windows.Forms.DataGridViewCellEventArgs)句柄 DataGridView1.RowValidated
如果 upd = True 然后
TblUsersTableAdapter.Update(MyDataBaseDataSet.tblUsers)
结束 如果
结束 Sub
结束





现在这是我的项目的代码,当我在datagridview中进行编辑时,按下当时的更新成功,然后我关闭那个调试并再次运行该应用程序然后它一段时间显示更新的数据或一段时间没有,但只在datagridview中,在数据库中它永远不会更新,

plz帮我更新datagridview和数据库表,



谢谢,,,

@Nilesh ,,,

解决方案





在调用数据适配器的更新方法之前,需要设置InsertCommand,UpdateCommand,DeleteCommand属性。

 SqlCommandBuilder cb; 
cb = new SqlCommandBuilder(TblUsersTableAdapter);


TblUsersTableAdapter.DeleteCommand = cb.GetDeleteCommand( true );
TblUsersTableAdapter.UpdateCommand = cb.GetUpdateCommand( true );
TblUsersTableAdapter.InsertCommand = cb.GetInsertCommand( true );





call update()

 TblUsersTableAdapter.Update(MyDataBaseDataSet.tblUsers)







示例程序

-------------



  Imports  System.Data.SqlClient 

Public Form1

Dim upd 正如 布尔
Dim cb As SqlCommandBuilder
Dim cn As SqlConnection( server =。\sqlexpress; database = testdb; uid = sa; pwd = sa123
Dim dt 作为 DataTable
Dim da As SqlDataAdapter

私有 Sub Button1_Click( ByVal sender 作为系统。对象 ByVal e 作为 System.EventArgs)句柄 Button1.Click
Dim squery < span class =code-keyword> As
String
squery = 从* dtl中选择*
da.SelectCommand = SqlCommand(squery,cn)
da.Fill(dt)
MessageBox.Show (dt.Rows.Count.ToString())
结束 Sub

私有 Sub Button2_Click( ByVal sender As System。 Object ByVal e As System.EventArgs)句柄 Button2.Click
Dim dr As DataRow = dt.NewRow
dr( 0 )= 4
dr( 1 )= Shasini
dr( 2 )= 5

dt.Rows.Add(dr)

MessageBox.Show(dt.Rows.Count.ToString())

Dim cb As SqlCommandBuilder(da)

da.DeleteCommand = cb.GetDeleteCommand( True
da.UpdateCommand = cb.GetUpdateCommand( True
da.InsertCommand = cb。 GetInsertCommand( True

da.Update(dt)
结束 Sub
结束





上面的示例工作正常,它正在数据库中插入记录。请确保您在表格中有一个主键字段。



最好的问候

Muthuraja


< blockquote>感谢很多老兄,

最后我得到了完美答案,

运行时更新DataGridView代码,,,



  Imports  System.Data.SqlClient 
Public Form1
Dim con As SqlConnection( 数据源=。 \ SQLEXPRESS; AttachDbFilename = D:\ My Project\Practice Projects\DataGridViewFinal\DataGridViewFinal\MyDataBase.mdf; Integrated Security = True; Connect Timeout = 30; User Instance = True
Dim upd As New Boolean
Dim ada 作为 SqlClient.SqlDataAdapter( 选择*来自tblusers,con)
Dim 标签 As < span class =code-keyword> New DataSet

Private Sub Form1_Load( ByVal sender As System。 Object ByVal e As System.EventArgs) Handles MyBase .Load
' TODO:这行代码将数据加载到'MyDataBaseD中ataSet.tblUsers'表。您可以根据需要移动或删除它。
.TblUsersTableAdapter.Fill( .MyDataBaseDataSet.tblUsers)
结束 Sub

私有 Sub TblUsersBindingSource_ListChanged( ByVal sender < span class =code-keyword>作为 对象 ByVal e As System.ComponentModel.ListChangedEventArgs)句柄 TblUsersBindingSource.ListChanged
如果 MyDataBaseDataSet.HasChanges 然后
upd = True
结束 如果
结束 Sub

私有 Sub DataGridView1_RowValidated( ByVal sender As 对象 ByVal e 作为系统。 Windows.Forms.DataGridViewCellEventArgs)句柄 DataGridView1.RowValidated
如果 upd = True 然后
Dim cb As SqlCommandBuilder(ada)
ada.DeleteCommand = cb.GetDeleteCommand( True
ada.UpdateCommand = cb.GetUpdateCommand( True
ada.InsertCommand = cb.GetInsertCommand( True
ada.Update(MyDataBaseDataSet.tblUsers)
< span class =code-keyword>结束 如果
结束 Sub
结束





再次感谢,,,

@Nilesh ,,,


Hey,

I have created an application in vb.net 08, and database is sql services based database,

Now i have Code:

Public Class Form1
    Dim upd As New Boolean
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'MyDataBaseDataSet.tblUsers' table. You can move, or remove it, as needed.
        Me.TblUsersTableAdapter.Fill(Me.MyDataBaseDataSet.tblUsers)
    End Sub

    Private Sub TblUsersBindingSource_ListChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs) Handles TblUsersBindingSource.ListChanged
        If MyDataBaseDataSet.HasChanges Then
            upd = True
        End If
    End Sub

    Private Sub DataGridView1_RowValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowValidated
        If upd = True Then
            TblUsersTableAdapter.Update(MyDataBaseDataSet.tblUsers)
        End If
    End Sub
End Class



Now this is the code of my project, when im editing in the datagridview, and press enter at that time its update successful, and then im closing that debugging and again run that application then its some time show the updated data or some time not, but only in datagridview, in the database its never update,
plz help me to update both datagridview and database table,

Thanks,,,
@Nilesh,,,

解决方案

Hi,

Before calling the update method of data adapter, you need to set the InsertCommand, UpdateCommand, DeleteCommand properties.

SqlCommandBuilder cb;
 cb = new SqlCommandBuilder(TblUsersTableAdapter);


TblUsersTableAdapter.DeleteCommand = cb.GetDeleteCommand(true);
TblUsersTableAdapter.UpdateCommand = cb.GetUpdateCommand(true);
TblUsersTableAdapter.InsertCommand = cb.GetInsertCommand(true);



call update()

TblUsersTableAdapter.Update(MyDataBaseDataSet.tblUsers)




Sample program
-------------

Imports System.Data.SqlClient

Public Class Form1

    Dim upd As New Boolean
    Dim cb As New SqlCommandBuilder
    Dim cn As New SqlConnection("server=.\sqlexpress;database=testdb;uid=sa;pwd=sa123")
    Dim dt As New DataTable
    Dim da As New SqlDataAdapter

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim squery As String
        squery = "Select * from dtl"
        da.SelectCommand = New SqlCommand(squery, cn)
        da.Fill(dt)
        MessageBox.Show(dt.Rows.Count.ToString())
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim dr As DataRow = dt.NewRow
        dr(0) = 4
        dr(1) = "Shasini"
        dr(2) = 5

        dt.Rows.Add(dr)

        MessageBox.Show(dt.Rows.Count.ToString())

        Dim cb As New SqlCommandBuilder(da)

        da.DeleteCommand = cb.GetDeleteCommand(True)
        da.UpdateCommand = cb.GetUpdateCommand(True)
        da.InsertCommand = cb.GetInsertCommand(True)

        da.Update(dt)
    End Sub
End Class



The above sample works fine and it is inserting the record in the db. Please make sure you are having a Primary Key field in the table.

Best Regards
Muthuraja


Thanking a lot dude,
Finally i got the Perfect Answer,
Code For Update DataGridView at Run Time,,,

Imports System.Data.SqlClient
Public Class Form1
    Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\My Project\Practice Projects\DataGridViewFinal\DataGridViewFinal\MyDataBase.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
    Dim upd As New Boolean
    Dim ada As New SqlClient.SqlDataAdapter("Select * from tblusers", con)
    Dim tab As New DataSet
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'MyDataBaseDataSet.tblUsers' table. You can move, or remove it, as needed.
        Me.TblUsersTableAdapter.Fill(Me.MyDataBaseDataSet.tblUsers)
    End Sub
 
    Private Sub TblUsersBindingSource_ListChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs) Handles TblUsersBindingSource.ListChanged
        If MyDataBaseDataSet.HasChanges Then
            upd = True
        End If
    End Sub
 
    Private Sub DataGridView1_RowValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowValidated
        If upd = True Then
            Dim cb As New SqlCommandBuilder(ada)
            ada.DeleteCommand = cb.GetDeleteCommand(True)
            ada.UpdateCommand = cb.GetUpdateCommand(True)
            ada.InsertCommand = cb.GetInsertCommand(True)
            ada.Update(MyDataBaseDataSet.tblUsers)
        End If
    End Sub
End Class



Thanks Again,,,
@Nilesh,,,


这篇关于数据表中的数据不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆