如果c#中不存在则调用delete# [英] call delete if not exists condition in c#

查看:84
本文介绍了如果c#中不存在则调用delete#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个类似的存储过程,

Hi all,
I have a stored procedure which is like this,

Alter proc usp_DelTechnology
(@TechnologyID int)
 as
Begin
 -- make sure we don't have reference descriptions
if(NOT EXISTS(select TechnologyID from tblTechnologySubTypes where TechnologyID = @TechnologyID) and
   NOT EXISTS(select TechnologyID from tblApplicationVsTechnologies where TechnologyID = @TechnologyID) and
   NOT EXISTS(select TechnologyID from tblEffortHours where TechnologyID = @TechnologyID))
BEGIN
 DELETE from tblTechnologies where TechnologyID = @TechnologyID
END
else
 begin
 Print 'False'
 end
 END



我这样称呼sp,


I have called the sp like this,

public int DeleteTechnology()
       {
           SqlParameter[] prms = new SqlParameter[1];
           prms[0] = new SqlParameter("@TechnologyID", TechnologyID);

           Data.ExecuteNonQuery("usp_DelTechnology", prms);
           return 0;
       }



任何人都可以帮我解决如何在c#中执行此操作。我想删除该技术,如果它没有在另一个表中的引用..如果它有它然后它应该显示一个消息..我必须在网格视图图像按钮中执行此操作..单击图像按钮,删除应该发生..

请帮忙。

感谢。 :)


Can anyone help me on how to do this in c#. I want to delete the technology if it doesnt have the reference in another table.. if it has then it should show a msg.. I have to do this in grid view image button.. On click of image button, the delete should happen..
Please help.
Thanku. :)

推荐答案

你可以在gridview中使用你的图像按钮......

you can have your image button inside the gridview like this ...
asp:ImageButton runat="server" ID="ibtnDelete"
        Text="delete"
        CommandName="delete"
        CommandArgument='<%#Eval("technologyID")%>
'





你可以添加一个RowCommand



and you can add a RowCommand

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "delete")
    {
        DeleteTechnology(convert.toInt32(e.CommandArgument));
    }
}







public int DeleteTechnology(int TechnologyID)
       {
           SqlParameter[] prms = new SqlParameter[1];
           prms[0] = new SqlParameter("@TechnologyID", TechnologyID);
 
           Data.ExecuteNonQuery("usp_DelTechnology", prms);
           return 0;
       }


如下所示更改存储过程,以便返回如下值:

Alter your stored procedure as below, so that it returns some value like below:
Alter proc usp_DelTechnology
(
   @TechnologyID int
)

AS

BEGIN
 -- make sure we don't have reference descriptions
if(NOT EXISTS(select TechnologyID from tblTechnologySubTypes where TechnologyID = @TechnologyID) and
   NOT EXISTS(select TechnologyID from tblApplicationVsTechnologies where TechnologyID = @TechnologyID) and
   NOT EXISTS(select TechnologyID from tblEffortHours where TechnologyID = @TechnologyID))
BEGIN
 DELETE from tblTechnologies where TechnologyID = @TechnologyID
 RETURN 1
END
   ELSE
     BEGIN
       Return 0
     END
END





现在在c#代码中,为图像按钮创建onClick事件



Now in c# code, create onClick event for image button

private void ImageButton_Click(object sender, EventArgs e)
{
    using (SqlConnection con = new SqlConnection("Your database connection string here."))
    {
        using (SqlCommand cmd = new SqlCommand("usp_DelTechnology", con))
        {
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.Add("@TechnologyID", SqlDbType.int).Value = GridView1.Cells[0].Text; //access technology Id from Gridview
           con.Open();
           int returnValue = cmd.ExecuteNonQuery();
           if (returnvalue == 0)
           {
               //Display message - There are references
           }
           else
           {
               //Deleted Successfully message.
           }

        }
    }
}


这篇关于如果c#中不存在则调用delete#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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