I've got a sproc and some C# calling it. My problem is that both the (N)VARCHAR values only have the first character returned. Everything else is fine. Why would only the 1st character be returned?
So
Content = (string)cmd.Parameters["@SmsContent"].Value,
ToNumber = (string) cmd.Parameters["@ToNumber"].Value,
both only get the 1st character returned.
Here's my sproc:
ALTER PROCEDURE [dbo].[GetNextSms]
(
@SmsId UNIQUEIDENTIFIER OUTPUT,
@SmsContent NVARCHAR OUTPUT,
@ToNumber VARCHAR OUTPUT,
@TimeAccepted DATETIME OUTPUT
)
AS
BEGIN TRANSACTION
-- 1. Get 1 row
SET ROWCOUNT 1
SELECT @SmsId = SmsId FROM SendQueue
WHERE ProcessingStarted = 0
SET ROWCOUNT 0
-- 2. Set as processing
UPDATE SendQueue
SET ProcessingStarted = 1
WHERE SmsId = @SmsId
-- 3. Return data
SELECT @SmsId = SmsId,
@SmsContent = SmsContent,
@ToNumber = ToNumber,
@TimeAccepted = TimeAccepted
FROM SendQueue
WHERE SmsId = @SmsId;
COMMIT
And here is my C#
connection.Open();
var cmd = new SqlCommand(@"GetNextSms", connection) {CommandType = CommandType.StoredProcedure};
SqlParameter param = cmd.Parameters.Add("@SmsId", SqlDbType.UniqueIdentifier);
param.Direction = ParameterDirection.Output;
param = cmd.Parameters.Add("@ToNumber", SqlDbType.VarChar,50);
param.Direction = ParameterDirection.Output;
param = cmd.Parameters.Add("@SmsContent", SqlDbType.NVarChar,1024);
param.Direction = ParameterDirection.Output;
param = cmd.Parameters.Add("@TimeAccepted", SqlDbType.DateTime);
param.Direction = ParameterDirection.Output;
if (cmd.ExecuteNonQuery() > 0)
{
sms = new Sms
{
SmsId = ((Guid) cmd.Parameters["@SmsId"].Value),
Content = (string)cmd.Parameters["@SmsContent"].Value,
ToNumber = (string) cmd.Parameters["@ToNumber"].Value,
TimeAccepted = ((DateTime) cmd.Parameters["@TimeAccepted"].Value)
};
}