SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


varchar(n) in C# (and C/C++)


varchar(n) in C# (and C/C++)

Author
Message
ghelyar
ghelyar
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 24
I have an extended stored procedure in C++ that I am converting to CLR so that it will also work on 64 bit (do not have the original source to just recompile it)

The procedure takes a varchar(n) as one of its inputs. The size of n makes a difference to the output even if the actual data is the same. For example
DECLARE @i VARCHAR(10)
SET @i 'test'
exec xp_test @i, @o OUTPUT

produces a different result to
DECLARE @i VARCHAR(4)
SET @i 'test'
exec xp_test @i, @o OUTPUT



I want to test my functions that are normally called by sql server, in both native C++ and C# with some sample values directly in the code but I can't work out how to pass them varchars of a specific length without the actual data always being that length. For example a varchar(10) with value "test".

How can I mimic a varchar(n) in native C++ and also in C# without always using a string that is actually n characters long?

Thanks
dmbaker
dmbaker
Right there with Babe
Right there with Babe (741 reputation)Right there with Babe (741 reputation)Right there with Babe (741 reputation)Right there with Babe (741 reputation)Right there with Babe (741 reputation)Right there with Babe (741 reputation)Right there with Babe (741 reputation)Right there with Babe (741 reputation)

Group: General Forum Members
Points: 741 Visits: 3524
In C#, would something like this work? Key is setting the length of the input parameter when you create it. You didn't indicate what type @o is so I assumed it was int.

private int CallProc(int inputParmLength, string inputParmValue)
{
int outputValue = -1;

using (SqlConnection conn = new SqlConnection("[your connection string here]"))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "xp_test";

SqlParameter parm1 = new SqlParameter("@i", SqlDbType.VarChar, inputParmLength);
SqlParameter parm2 = new SqlParameter("@o", SqlDbType.Int);
parm1.Value = inputParmValue;
parm2.Direction = ParameterDirection.Output;

cmd.Parameters.Add(parm1);
cmd.Parameters.Add(parm2);

conn.Open();

int affectedRows = cmd.ExecuteNonQuery();
outputValue = (int)cmd.Parameters["@o"].Value;

conn.Close();
}

return outputValue;
}





ghelyar
ghelyar
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 24
Isn't this only used to get the DBMS to execute the procedure and return the result back to the application?

I was hoping to do something more like just call the function as a normal C# method but pass in parameters that would be the same as SQL Server 2005 passes in when it calls the procedure.

What I was looking for was more along the lines of (from memory)
[SqlProcedure]
public void foo(...)
{
...
}

public void testfoo()
{
foo(...); // send a varchar(n) to foo
}



I was also hoping to do this in C++ as well, where I have even less of a clue how to do it.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search