English 中文(简体)
替换函数在 C# 不在 C# 中工作, 而是在 SQL Server 2008 中工作
原标题:Replace function not working in C#, but working in SQL Server 2008

使用命令时替换工作函数

exec MyStoredProcedure param1, param2, 

等... 但当它通过 C# 代码运行时, 它没有正确替换文本, 所以我最后会使用电子邮件, 上面仍然写着“ Comments ”, 而不是实际的评论。 我试图在 SQL Server 2008 中检查它, 但是它很好, 电子邮件通过正常。 需要检查什么吗?

这是我的C#代码:

public static void SetRequestStatusChange(int reqID, int newStatus, string assignTo)
{
    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand("spUpdateRequest", con);
    cmd.CommandType = CommandType.StoredProcedure;

    // Status
    cmd.Parameters.Add(new SqlParameter("@RequestNo", SqlDbType.Int, 4));
    cmd.Parameters["@RequestNo"].Value = 276;

    // Queue
    cmd.Parameters.Add(new SqlParameter("@userID", SqlDbType.NVarChar, 12));
    cmd.Parameters["@userID"].Value = "1091912";

    // State
    cmd.Parameters.Add(new SqlParameter("@RequestStatus", SqlDbType.Int, 4));
    cmd.Parameters["@RequestStatus"].Value = 2;

    // Buyer Emp ID
    cmd.Parameters.Add(new SqlParameter("@assignedTo", SqlDbType.NVarChar, 12));
    cmd.Parameters["@assignedTo"].Value = "1091912";

    try
    {
        con.Open();
        cmd.ExecuteNonQuery();
    }
    catch (SqlException err)
    {
        throw new ApplicationException("Data Error." + err.ToString());
    }
    finally
    {
        //close the connection 
        con.Close();
    }
}

以下是存储程序中实际进行替换的部分。 实际存储程序非常长, 并呼叫许多其他存储程序( 我没有写), 读起来会非常麻烦 。

select @html = replace(replace(replace(@html,  @@COMMENTS , case when @activityID <> 20 then   The following comments were noted when the Request  else    end +

   case @ActivityID when  2 then   was submitted:   +  case when requestorComments >    then requestorComments  else  None  end
   when 0 then    
   when  4 then   was edited:   + isnull(L.Comments,   None ) 
   when  15 then   was edited:   + isnull(LL.Comments,   None ) 
   when  20 then   
   when  21 then    material was returned:   + case when dbo.fnLastComment(lamSpecRecallComments) >    then replace(dbo.fnLastComment(lamSpecRecallComments), : , :   ) else  None  end
   when  -21 then    material was returned:   + case when dbo.fnLastComment(lamSpecRecallComments) >    then replace(dbo.fnLastComment(lamSpecRecallComments), : , :   ) else  None  end
   when  8 then   was approved:   + case when ccMgrCOmments >    then ccMgrCOmments  else  None  end
   when  9 then   was approved:   + case when lamAprCOmments >    then lamAprCOmments  else  None  end
   when 13 then   was stored:   + case when dbo.fnLastComment(lamSpecCOmments) >    then dbo.fnLastComment(lamSpecCOmments)  else  None  end
   when -8 then   was closed:   + case when ccMgrCOmments >    then ccMgrCOmments  else  None  end
   when -9 then   was closed:   + case when lamAprCOmments >    then lamAprCOmments  else  None  end
   else    end), 

    @@PLEASE ,
   case when @activityID =  8 or @activityID = 9 or @activityID = 20 or @activityID = 2  then  Please click the link below to view the Request and ensure it is worked expeditiously. 
   else    end), @@RECALL ,@list)  

   from (select * from strgRequests where requestNumber = @requestID) r
   left join (select * from  ActivityLog  where TransactionID = @transactionID) L on 1 = 1
   left join (select * from  ActivityLineItemLog where TransactionID = @transactionID) LL on 1 = 1

Comments, Recall, and {请来自一个表格。 这样电子邮件就可以定制 。

问题回答

"@@" is normally used for global/default variables. "@" is normally used for parameters.

我的直觉告诉我,切换你的@@@是您在解决这个问题时应该采取的下一个方向。

另外,如果一个字段有"\\\ Comments"的价值, 你可能有一个代码, 看起来像:

设置@cmd安插到表格( 字段、 字段、 字段) 值( 有用, @ Comments, 更多东西 )

exec (@ cmd)

这有两个问题。

  1. You didn t translate @@Comments
  2. You are injection prone.

使用类似 :

execute sp_executesql N insert into Table (field,field,field) values (@var1,@var2,@var3) ,N @var1 varchar(256) , @var1=@Comments ...





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签