-1

我有一个存储过程,可以同时将行插入多个表。存储过程中存在未触发的问题。你能看看这个语法是否正确且有效。

    set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[InquiryPostReply]
(
    @Inquiry_id VARCHAR(50),
    @User_id VARCHAR(50),
    @Priority_type VARCHAR(25),
    @Status_name VARCHAR(50),
    @Inquiry_Content VARCHAR(1024),
    @Question_id NUMERIC(18,0),
    @user_name VARCHAR(50) OUT,
    @user_email VARCHAR(50) OUT,
    @NewId VARCHAR(50) OUT
)
AS
BEGIN
SET NOCOUNT ON;
declare @var1 int
declare @var2 int
declare @uniqueRef char(14)
set @uniqueRef = dbo.UniqueRefNum(rand(), rand(), rand(), rand())
set @var1= (SELECT [Id] FROM [OmStocks].[dbo].[tbl_Status_master] WHERE (Status_name=@Status_name))
set @var2= (SELECT [Id] FROM [OmStocks].[dbo].[tbl_Priority_master] WHERE (Priority_name=@Priority_type))
set @user_email=(SELECT [Email_Address] FROM [OmStocks].[dbo].[tbl_Inquiry_History] WHERE (Inquiry_id=@Inquiry_id))
set @user_name= (SELECT tbl_User_master.Full_Name FROM tbl_Inquiry_History INNER JOIN tbl_User_master ON tbl_Inquiry_History.User_id = tbl_User_master.Id WHERE(tbl_Inquiry_History.Inquiry_id=@Inquiry_id))
IF @var1 = '1'
BEGIN
  INSERT INTO [OmStocks].[dbo].[tbl_Inquiry_master]
           ([Id],[Inquiry_id],[User_id],[Priority_id],[Status_id],[body])
  VALUES
          (@uniqueRef,@Inquiry_id,@User_id,@var2,@var1,@Inquiry_Content)
  INSERT INTO [OmStocks].[dbo].[tbl_Answer]
           ([Question_id],[body],[Created_date])
  VALUES
           (@Question_id,@Inquiry_Content,CONVERT(DATETIME,GETDATE(), 101))
  UPDATE [OmStocks].[dbo].[tbl_Inquiry_History] 
  SET [Priority_id] = @var2,[Status_id] = @var1,[IsDisplay] = 1,[IsReplied] = 1,[TimeStamp] = CONVERT(DATETIME,GETDATE(), 101)
  WHERE (Inquiry_id=@Inquiry_id)
END
ELSE IF @var1 = '2'
BEGIN
  INSERT INTO [OmStocks].[dbo].[tbl_Inquiry_master]
           ([Id],[Inquiry_id],[User_id],[Priority_id],[Status_id],[body])
  VALUES
          (@uniqueRef,@Inquiry_id,@User_id,@var2,@var1,@Inquiry_Content)
  INSERT INTO [OmStocks].[dbo].[tbl_Answer]
           ([Question_id],[body],[Created_date])
  VALUES
           (@Question_id,@Inquiry_Content,CONVERT(DATETIME,GETDATE(), 101))
  UPDATE [OmStocks].[dbo].[tbl_Inquiry_History] 
  SET [Priority_id] = @var2,[Status_id] = @var1,[IsDisplay] = 1,[IsReplied] = 1,[TimeStamp] = CONVERT(DATETIME,GETDATE(), 101)
  WHERE (Inquiry_id=@Inquiry_id)
END
ELSE IF @var1 = '3'
BEGIN
  INSERT INTO [OmStocks].[dbo].[tbl_Inquiry_master]
    ([Id],[Inquiry_id],[User_id],[Priority_id],[Status_id],[body])
  VALUES
    (@uniqueRef,@Inquiry_id,@User_id,@var2,@var1,@Inquiry_Content)
  INSERT INTO [OmStocks].[dbo].[tbl_Answer]
           ([Question_id],[body],[Created_date])
  VALUES
           (@Question_id,@Inquiry_Content,CONVERT(DATETIME,GETDATE(), 101))
  UPDATE [OmStocks].[dbo].[tbl_Inquiry_History] 
  SET [Priority_id] = @var2,[Status_id] = @var1,[IsDisplay] = 0,[IsReplied] = 1,[TimeStamp] = CONVERT(DATETIME,GETDATE(), 101),[Activity_expire_time] = CONVERT(DATETIME,GETDATE(), 101)
  WHERE (Inquiry_id=@Inquiry_id)
END
SET @NewId = @uniqueRef
END

当我从管理工作室执行它背后的代码时,这个存储过程如何正常工作,但没有将记录插入表。我在那里包括这个 databasehelper.cs 代码:

/// <summary>
    /// Adds answer in the [tbl_Inquiry_master] table.
    /// </summary>
    public static void AddPostReply(string inquiry_id,string user_id,string priority_type, string status_name, string inquiry_content,int question_id,out string reply_id,out string user_name,out string user_email)
    {
        using (SqlConnection con = Util.GetConnection())
        {
            con.Open();
            AddPostReply(con,inquiry_id,user_id,priority_type, status_name, inquiry_content, question_id,out reply_id,out user_name,out user_email);
        }
    }
    /// <summary>
    /// Adds answer in the [tbl_Inquiry_master] table.
    /// </summary>
    public static void AddPostReply(SqlConnection con, string inquiry_id, string user_id, string priority_type, string status_name, string inquiry_content, int question_id, out string reply_id, out string user_name, out string user_email)
    {
        AddPostReply(con, null, inquiry_id, user_id, priority_type, status_name, inquiry_content, question_id, out reply_id, out user_name, out user_email);
    }
    /// <summary>
    /// Adds answer in the [tbl_Inquiry_master] table.
    /// </summary>
    public static void AddPostReply(SqlConnection con, SqlTransaction trans, string inquiry_id, string user_id, string priority_type, string status_name, string inquiry_content, int question_id, out string reply_id, out string user_name, out string user_email)
    {
        using (SqlCommand cmd = new SqlCommand("[dbo].[InquiryPostReply]", con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Inquiry_id", SqlDbType.VarChar).Value = inquiry_id;
            cmd.Parameters.Add("@User_id", SqlDbType.VarChar).Value = user_id;
            cmd.Parameters.Add("@Priority_type", SqlDbType.VarChar).Value = priority_type;
            cmd.Parameters.Add("@Status_name", SqlDbType.VarChar).Value = status_name;
            cmd.Parameters.Add("@Inquiry_Content", SqlDbType.VarChar).Value = inquiry_content;
            cmd.Parameters.Add("@Question_id", SqlDbType.Int).Value = question_id;
            cmd.Parameters.Add("@user_name", SqlDbType.VarChar, 50).Direction = ParameterDirection.Output;
            cmd.Parameters.Add("@user_email", SqlDbType.VarChar, 50).Direction = ParameterDirection.Output;
            cmd.Parameters.Add("@NewId", SqlDbType.VarChar, 50).Direction = ParameterDirection.Output;
           if (trans != null)
            trans = con.BeginTransaction();
        cmd.ExecuteNonQuery();
        trans.Commit();
            user_name = Convert.ToString(cmd.Parameters["@user_name"].Value);
            user_email = Convert.ToString(cmd.Parameters["@user_email"].Value);
            reply_id = Convert.ToString(cmd.Parameters["@NewId"].Value);
        }
    }

- - - - - - - - - - - - - - - - - 更新 - - - - - - - - --------------------------

错误发生如下:

    Server Error in '/OmInvestmentStockMarketing_new' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 140:                trans = con.BeginTransaction();
Line 141:            cmd.ExecuteNonQuery();
Line 142:            trans.Commit();
Line 143:            user_name = Convert.ToString(cmd.Parameters["@user_name"].Value);
Line 144:            user_email = Convert.ToString(cmd.Parameters["@user_email"].Value);

Source File: c:\Documents and Settings\Vishal\My Documents\Visual Studio 2005\WebSites\OmInvestmentStockMarketing_new\App_Code\DatabaseHelper.cs    Line: 142 

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   DatabaseHelper.AddPostReply(SqlConnection con, SqlTransaction trans, String inquiry_id, String user_id, String priority_type, String status_name, String inquiry_content, Int32 question_id, String& reply_id, String& user_name, String& user_email) in c:\Documents and Settings\Vishal\My Documents\Visual Studio 2005\WebSites\OmInvestmentStockMarketing_new\App_Code\DatabaseHelper.cs:142
   DatabaseHelper.AddPostReply(SqlConnection con, String inquiry_id, String user_id, String priority_type, String status_name, String inquiry_content, Int32 question_id, String& reply_id, String& user_name, String& user_email) in c:\Documents and Settings\Vishal\My Documents\Visual Studio 2005\WebSites\OmInvestmentStockMarketing_new\App_Code\DatabaseHelper.cs:120
   DatabaseHelper.AddPostReply(String inquiry_id, String user_id, String priority_type, String status_name, String inquiry_content, Int32 question_id, String& reply_id, String& user_name, String& user_email) in c:\Documents and Settings\Vishal\My Documents\Visual Studio 2005\WebSites\OmInvestmentStockMarketing_new\App_Code\DatabaseHelper.cs:112
   Admin_OWM_Inquiry_Post_Reply.btnsubmit_Click(Object sender, EventArgs e) in c:\Documents and Settings\Vishal\My Documents\Visual Studio 2005\WebSites\OmInvestmentStockMarketing_new\Admin\OWM_Inquiry_Post_Reply.aspx.cs:51
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

请帮我..

4

2 回答 2

1

您需要在您的连接上打开事务,执行您的语句,然后提交这些更改。

trans = con.BeginTransaction();
cmd.ExecuteNonQuery();
trans.Commit();
于 2013-10-31T13:15:09.897 回答
1

这对我来说似乎是个问题

IF @var1 = '1' AND  @var1 = '2'

那将始终返回 false

您需要按照 dgarbacz 的指示提交交易

于 2013-10-31T13:23:33.217 回答