Visual Basic编辑表单有帮助吗? [英] Visual Basic edit form help?

查看:70
本文介绍了Visual Basic编辑表单有帮助吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够在visual basic中编写添加表单。 
$


我正在尝试使用类似的方法/逻辑来编辑项目并将其保存回Microsoft访问数据库。 
$


请在此处查找我尝试编辑的子程序: 

I was able to program an add form in visual basic. 

I'm trying to use a similar method/logic to edit items and save it back to a Microsoft access database. 

Please find my subroutine that tries to edit here: 

#Region " Edit to DB "
    Public Sub EditSelected(ByVal row As DataRow)
        Using cn As New OleDb.OleDbConnection With {.ConnectionString = Builder.ConnectionString}
            Using cmd As New OleDb.OleDbCommand With {.Connection = cn}
                cmd.CommandText =
                    <SQL>
                      UPDATE Table1
                          (PRODID ,
                            TS,
                           Material,
                           Directory 
                          ) 

                        SET
                           
                        (   @PRODID ,
                            @TS,
                            @Material,
                            @Directory
                        )
                    WHERE PRODID = @PRODID
                    </SQL>.Value
                cmd.Parameters.AddWithValue("@PRODID", row.Field(Of Integer)("PRODID"))
                cmd.Parameters.AddWithValue("@Material", row.Field(Of String)("Material"))
                cmd.Parameters.AddWithValue("@TS", row.Field(Of String)("TS"))
                cmd.Parameters.AddWithValue("@Directory", row.Field(Of String)("Directory"))

                cn.Open()
                cmd.ExecuteNonQuery()
                '
                ' Get new identifier which is an auto-incrementing field/primary key
                '
                cmd.CommandText = "Select @@Identity"
                row.SetField(Of Integer)("ID", CInt(cmd.ExecuteScalar))
            End Using
        End Using


    End Sub

#End Region

我调用编辑表单的方式是用户在数据网格中选择一行显示编辑按钮的视图。

the way I invoke the edit form has the user select a row in data grid view which shows the edit button.

用户点击编辑按钮以显示编辑表单。 

User clicks on the edit button to show the edit form. 

我已经能够从主表单传递数据进行编辑表格成功。 



问题:

I've been able to pass the data from the main form to edit form successfully. 

THE ISSUE:

如果我使用INSERT,这很明显INTO而不是UPDATE此代码创建一个带有PRODID 1的新条目(假设用户在主窗体数据网格视图中选择了id 1),而不是覆盖旧的产品ID。



我知道我必须使用UPDATE命令,但我不确定语法。  我想要一些帮助。



如果有什么不清楚请告诉我

It's pretty obvious that if I use INSERT INTO instead of UPDATE this code creates a new entry with PRODID 1 (assuming the user selected id 1 on main form data grid view) instead of overwriting the old product id.

I know I have to use the UPDATE command but I'm not sure of the syntax.  I'd like some help with that.

If anything isn't clear please let me know

CP

推荐答案

您展示的是创建ORM的原因(& ;对象相关映射")。我们可以讨论什么是ORM,什么不是。但是使用CRUD(创建,读取,更新和删除),您确定您的解决方案很弱。在多用户环境中使用真正的
数据库几乎不可能做到这一点。 

What you show is why there is created ORM ("Object Related Mapping"). We can discuss what is ORM and what not. But using CRUD (Create, Read, Update and Delete) you are sure your solution is weak. What you try to do is almost impossible with a real database in a multi user environment. 

首先使用简单的ORM来了解这一点。一个很好的例子是TableAdapterManager(虽然它也有问题)。 

Start with using a simple ORM to learn that. A good sample about that is the TableAdapterManager (although it has issues as well). 

https://msdn.microsoft.com/en-us/library/bb384426.aspx

与此相关的是绑定演示文稿。 

Something related to that is bound presentation. 


这篇关于Visual Basic编辑表单有帮助吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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