Windows窗体:具有两个操作的一个按钮 [英] Windows Forms: One button with Two Operations

查看:97
本文介绍了Windows窗体:具有两个操作的一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个带有一个SAVE按钮的表单,并且已经将它用于一个button_click事件,在该事件中我正在使用存储过程将数据添加到数据库表中.

现在,我要编辑表单中的数据,然后单击相同的保存"按钮.这应该运行与编辑相关的代码,而不是与添加相关的代码.

如何为save button_click事件编写两个存储过程函数?

请使用示例进行答复.

Hi all,

I have a form with one SAVE button and I already used it for a button_click event inside which I am using a stored procedure to add data to a database table.

Now I want to go to edit data in the form and click that same SAVE button. This should run the edit related code not the add related code.

How do I write two stored procedure functions for the save button_click Event?

Please reply with an example.

推荐答案

编辑和添加非常相似,它们都需要将数据保存到服务器.两者之间的主要区别在于,对于添加",您还没有数据的主键.

因此,您可以有一个名为保存"的存储过程,可以在编辑或添加时进行调用,但是该过程执行的操作取决于是否将主键传递给它.

Edit and Add are very similar, they both need to Save data to the server. The major difference between the two is that for Add you won''t have a primary key for your data yet.

So you could have a single stored procedure called ''save'' that you call on either edit or add, but the operation the procedure takes depends on whether or not you pass a primary key to it.

CREATE PROCEDURE  myarea_SaveData

	(
		@PrimaryKey			INT, 
		@FirstName			VARCHAR(50), 
		@LastName			VARCHAR(50)
	)

AS

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SET NOCOUNT ON

/*
__________________________________________________________________________________________
Author:			Dylan Morley 
Description:		Either adds a new record or updates an existing one

__________________________________________________________________________________________
*/

IF EXISTS(SELECT FirstName FROM MyTable (NOLOCK) WHERE PrimaryKeyID = PrimaryKey)
BEGIN
	UPDATE
		MyTable
	SET
		FirstName = @FirstName, 
		LastName = @LastName, 
	WHERE
		PrimaryKeyID = @PrimaryKey
END
ELSE
BEGIN
	INSERT INTO
		MyTable (PrimaryKey, FirstName, LastName)
	VALUES		
		(@PrimaryKey, @FirstName, @LastName)

END


GO


好吧,我误解了这个问题,让我相应地修改了答案.

您可以保留一个全局变量.



Ok I misunderstood the question and let me modify my answer accordingly.

for accomplishing you can keep one global variable.

Like

String Operation = "Insert";



相应地,在按钮单击事件中,您可以进行相应的修改,例如



And accordingly in button click event you can modify accordingly like

Public void Button_click(object sender,EventArgs e)
{
if(Operation == "Insert")
{
InsertData();
Operation = "Edit";
}
else
{
EditData();
Operation="Insert";
}
}

InsertData()
{
//Perform insert stored procedure here
}
EditData()
{
//Perform edit stored procedure here
}


您可以有一个布尔标志,或一些逻辑来决定要做什么.

例如
您可以有一个保存记录ID的标签.如果为空,则为新记录.否则,这是更新操作.
You can have a boolean flag, or some logic to decide what to do.

For example,
You can have a label which holds ID of the record. If it is blank, it is a new record. Otherwise it is an update operation.
If Label1.Text = "" Then
  doAdd()
Else
  doEdit()
End If


这篇关于Windows窗体:具有两个操作的一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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