简体   繁体   English

执行 SQL 存储过程

[英]Executing SQL Stored Procedure

ALTER PROCEDURE dbo.SP_InsertTicket
    /*
    (
    @parameter1 int = 5,
    @parameter2 datatype OUTPUT
    )
    declare @i as numeric
    exec SP_InsertTicket 'asd','cd@y.com','232323','dasasd','sdasdas','01-jan-2010',@i output,'sdas','sdasd','02-jan-2010'
    select @i*/
    @Client_FullName varchar(30),
    @Client_EmailAdd varchar(50),
    @Client_Telephn varchar(15),
    @Ticket_Subject varchar(50),
    @Ticket_Source varchar(15),
    @Ticket_CreateDate Datetime,

    @Ticket_Id integer output,
    @Que_Message varchar(100),
    @Que_Attachment varchar(max),
    @Que_UpdateDate Datetime

AS
    declare @TickID integer;
    /* SET NOCOUNT ON */
    BEGIN

       INSERT INTO tbl_Ticket (Client_FullName,Client_EmailAdd,Client_Telephn,Ticket_Subject,Ticket_Source,Ticket_CreateDate)

                     VALUES (@Client_FullName,  @Client_EmailAdd ,@Client_Telephn,@Ticket_Subject,@Ticket_Source,@Ticket_CreateDate)


        Select @TickID = MAX(Ticket_Id) from tbl_Ticket


        set @Ticket_Id=@TickID

        INSERT INTO tbl_TicketQuestion (Ticket_Id,Que_Message,Que_Attachment,Que_UpdateDate)

                     VALUES (@TickID,@Que_Message,@Que_Attachment,@Que_UpdateDate)

     END

    RETURN

This is my store procedure in which i need to return Ticket_Id to send it via email app It insert records well bt not able to retirn value in DAL Below is the code for executing stored procedure which returns value这是我的存储过程,我需要返回 Ticket_Id 以通过 email 应用程序发送它它插入记录很好但无法在 DAL 中返回值下面是执行返回值的存储过程的代码

public class cls_DAL
{
    public cls_DAL()
    {
        //
        // TODO: Add constructor logic here
        //
    }

   static string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString.ToString();

  SqlConnection con = new SqlConnection(strConn);
  SqlCommand cmd = new SqlCommand();
  SqlDataAdapter da = new SqlDataAdapter();
  DataSet ds = new DataSet();
  DataTable dt = new DataTable();



  public int insert_NewTicket(string fullname, string emailadd, string telephone, string subject, string source, DateTime date,string Message, string attachment, DateTime updatedate)
  {
      try
      {  
            con.Open();
            cmd = new SqlCommand("SP_InsertTicket", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Client_FullName", fullname);
            cmd.Parameters.AddWithValue("@Client_EmailAdd", emailadd);
            cmd.Parameters.AddWithValue("@Client_Telephn",telephone);
            cmd.Parameters.AddWithValue("@Ticket_Subject", subject);
            cmd.Parameters.AddWithValue("@Ticket_Source",source);
            cmd.Parameters.AddWithValue("@Ticket_CreateDate",date);
            cmd.Parameters.AddWithValue("@Ticket_Id",0);
            cmd.Parameters.AddWithValue("@Que_Message", Message);
            cmd.Parameters.AddWithValue("@Que_Attachment", attachment);
            cmd.Parameters.AddWithValue("@Que_UpdateDate",updatedate);

            cmd.Parameters["@Ticket_Id"].Direction = ParameterDirection.InputOutput;

            return cmd.ExecuteNonQuery();
            int i = (int)cmd.Parameters["@Ticket_Id"].Value;

        }
     catch
        {
            throw;
        }
        finally
        {
            cmd.Dispose();
            con.Close();
            con.Dispose();
        }
    }
}

Its just a guess, not sure.这只是一个猜测,不确定。 You can give a try the following:您可以尝试以下操作:

cmd.Parameters["@Ticket_Id"].Direction = ParameterDirection.InputOutput;

TO

cmd.Parameters["@Ticket_Id"].Direction = ParameterDirection.Output;

That won't compile you'll get unreachable code那不会编译你会得到无法访问的代码

cmd.Parameters["@Ticket_Id"].Direction = ParameterDirection.InputOutput;              cmd.ExecuteNonQuery();
return (int)cmd.Parameters["@Ticket_Id"].Value; 

or @Matt's solution below...或下面@Matt 的解决方案...

That cast is iffy as well...那个演员也很不稳定......

And in a multi user scenario, ticketid will race.并且在多用户场景中,ticketid 会竞争。 Think about what could (will!!!) happen if you run two of these at the same time想想如果你同时运行其中两个会发生什么(将会!!!)

Should be wrapped in a transaction.应该包裹在交易中。

And you don't need Max, either, Use Scope_Identity而且您也不需要 Max,使用 Scope_Identity

You could run Select Scope_Identity() after the Insert statement.您可以在 Insert 语句之后运行Select Scope_Identity() Then in your DAL Method return Convert.ToInt32(cmd.ExecuteScalar())然后在你的 DAL 方法中return Convert.ToInt32(cmd.ExecuteScalar())

Change this:改变这个:

return cmd.ExecuteNonQuery(); 

to

Int i = cmd.ExecuteScalar();

If you are only returning one integer from that procedure.如果您只从该过程中返回一个 integer。

ExecuteNonQuery() isnt the method you want to be using here ExecuteNonQuery()不是您要在此处使用的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM