Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# multi line strings allowing variable calling

Tags:

c#

I have googled but cannot find an answer for this question:

I know about multi-line strings in c#. But how can I create a string like:

string temp = @"DECLARE @rolename varchar(max)
SET @rolename ='***' 
EXEC sp_addrolemember N'db_execute',@rolename"

* represents there I have to delcare a variable having some value like (object.variable).

Is this possible??

like image 362
vysakh Avatar asked Dec 01 '22 20:12

vysakh


1 Answers

.NET supports multi-line strings, sure. The @"..." syntax is just a shortcut to make it easier in the language. However, in your specific example you should not try to concatenate the value in: that entire example should be done via parameters:

cmd.CommandText = "EXEC sp_addrolemember N'db_execute',@rolename";
cmd.Parameters.AddWithValue("rolename", yourRoleName);

Update: checking msdn, the second parameter is actually the member-name, but you might also be able to use:

cmd.CommandText = "sp_addrolemember";
cmd.CommantType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("rolename", "db_execute");
cmd.Parameters.AddWithValue("membername", yourMemberName);
like image 104
Marc Gravell Avatar answered Dec 03 '22 10:12

Marc Gravell