Losbear
Losbear

Reputation: 3315

c# parameter values to string?

ok, I'm sure i'm not the only one that has wondered this =)

In a nutshell, is it possible using VisualStudio to convert:

string a1, string a2, string a3, string a4, string a5, string  a6

into a string sorta like:

"'value1', 'value2', 'value3', 'value4', 'value5', value6'"

(don't worry about the exact formatting, i can handle that myself)?

Background: I often have functions that calls stored procedures that accept a lot of params, and sometimes (debugging) i need to build the "exec dbo.storedprocname param1, param2...." string and run it directly against the server. Currently i have to manually build the string and i'm hoping there's an easy way to print it out in the immediate window in VisualStudio

EDIT (By popular demand): I'm using Linq2SQL (yes, i knwo it's obsolete) and have 100+ SP's in it. When I need to debug a function, i put a break inside one of these functions to see what it's about to pass to the database. So this would be RUNTIME. When it hits the breakpoint:

public static void MethodCreatedByLINQ2Sql(string param1, string param2, string param3, ...)
{
    context.mystoredprocedure(param1, param2, param3...); //<---breakpoint
}

I want to somehow (without having to specify each param), print out a delimited string that i can cut & paste into SSMS2008 (trying to save time) to build a string like this

exec dbo.mystoredprocedure [['value1', 'value2', 'value3'......]] <--- pasted value from VisualStudio

No, i'm not trying to do SQLInjections or anything like that. I'm just trying to save time - some of these SP's have 20+ params.

Upvotes: 2

Views: 1200

Answers (6)

Blachshma
Blachshma

Reputation: 17385

Is this what you're after?

String paramStr = String.Format("'{0}', '{1}', '{2}', '{3}', '{4}'", a1, a2, a3,a4,a5);

I should point out that sending strings like this to the DB can result in SQL Injection

Option 2:

To print the to the intermediate window you can use this trick:

  1. Create a breakpoint
  2. Right-Click the red dot -> "When Hit".
  3. In the new screen select the "Print a message" checkbox
  4. In the textbox type the String.Format from above but with a curly brace on each side:

{ String.Format(.....) }

More about the params you can print in that message: http://msdn.microsoft.com/en-us/library/232dxah7.aspx

Upvotes: 3

Omar
Omar

Reputation: 16623

I think that the best method is use params in your method to make it more simple:

public static void MethodCreatedByLINQ2Sql(params string[] list)
{
    //And then very simply use String.Join()
    string s = String.Join(",", list);
}

Upvotes: 2

Gene S
Gene S

Reputation: 2773

If you run the SQL Profiler it will show you the EXEC statements with all the information you need. You will have to play around with which options in the Profiler work best for you but generally if you use the TSQL_SPs template it should give you the information you are looking for.

Of course, the limitation to this is that you won't get the string until after the stored procedure call is made so if you put a breakpoint in your code it will have to be after the call to SQL Server.

Upvotes: 0

Larry
Larry

Reputation: 18031

Have a1, a2 in a string array :

var a = new string[] { a1, a2, a3... };

Then convert their values to a string this way :

var v = string.Join("','", a);

Anyway, I would suggest to use Parameter against your stored procedure. The trick above is not clean to generate sql queries because it can lead to SQL injections.

Upvotes: 0

Alex
Alex

Reputation: 23300

String.Join() allows you to concatenate any number of strings, like this:

string completeString = String.Join(", ", a1, a2, a3, ... ,aN);

this overload (it has 5 different ones) is .Join(string separator, params object[] values)

Of course, as already pointed out, this should be avoided, especially when you have to use EXEC. A parameterized query is highly advisable (but another topic entirely, plus you can easily find many examples around SO itself).

Upvotes: 0

MichaelT
MichaelT

Reputation: 7934

You can run code in immediate window of visual studio while you debugging.

Upvotes: 0

Related Questions