将值传递给存储过程

时间:2014-05-20 09:57:46

标签: c# sql asp.net sql-server ado.net

我在SQL Server中有一个存储过程,它有许多字段,其中一些在参数列表中初始化。那么我需要从asp.net传递这些参数的值还是绕过它们?请参阅下面的代码以了解我的意思。

存储过程:

ALTER PROCEDURE [dbo].[ADD_SMS_InQueue]
(
   @ToMobNo varchar(15),
   @Message nvarchar(2000),
   @encoding_Type int = 0,
   @IsAlertSMS bit = 0,
   @priority int = 2,
   @SendByUser int = 1,
   @MessageType tinyint = 0
)
AS
   BEGIN TRY
   BEGIN TRANSACTION
       INSERT INTO MessageQueue ([QueueDateTime], Client_ID, ToMobileNo, 
            [Message], LongMessageType_ID, EncodingType_ID, AlertMessage, LocalSMS, Priority_ID, Attempts, TotalAttempts, Status_ID, SendByUser, MessageType)
       VALUES (GETDATE(), 1, @ToMobNo, @Message, 3, @encoding_Type, @IsAlertSMS, 1, @priority, 0, 0, 1, @SendByUser, @MessageType)

       COMMIT TRANSACTION
   END TRY
   BEGIN CATCH
        ROLLBACK TRANSACTION
   END CATCH

发送电子邮件的C#代码:

public class MessageQueueBizz
{
        public string ToMobileNo { get; set; }
        public string Message { get; set; }

        public MessageQueueBizz(string ToMobileNo, string Message) 
        {
            this.ToMobileNo = ToMobileNo;
            this.Message = Message;
        }
    }
    public class ManageQueueBizz 
    {
        public int Insert(MessageQueueBizz MessageQueueBizz)
        {
            SqlCommand cmd = new SqlCommand("ADD_SMS_InQueue", DataBaseConnectionSMSDB.OpenConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter pToMobileNo = new SqlParameter("@ToMobNo", MessageQueueBizz.ToMobileNo);
            cmd.Parameters.Add(pToMobileNo);
            SqlParameter pMessage = new SqlParameter("@Message", MessageQueueBizz.Message);
            cmd.Parameters.Add(pMessage);

            int result = Convert.ToInt32(cmd.ExecuteNonQuery());

            DataBaseConnectionSMSDB.CloseConnection();

            return result;
        }

1 个答案:

答案 0 :(得分:1)

您不需要包含已在存储过程

中初始化的参数