0

A small question

I am trying to run a db query through c#. I am trying to print something on DB which I want to return as a string from C#

string altertable1 = "print 'scfs'";
SqlCommand altertable = new SqlCommand(createtablecommand, connection);

string x = altertable.(function which return the printed query result);

SO what i want is that x value will be scfs;

4
  • stackoverflow.com/questions/10541991/… Commented Aug 28, 2014 at 9:52
  • If it has to specifically be PRINT, I'm not sure that there's a way. Commented Aug 28, 2014 at 9:53
  • Could you use SELECT rather then PRINT? Commented Aug 28, 2014 at 10:16
  • It is not specifically select actually i have declared a variable which gets data, which i need in c# Commented Aug 28, 2014 at 10:52

2 Answers 2

1

I don't think there is an easy way to capture the output of PRINT command, since it doesn't return any row readable through a SqlDataReader object.

I would consider a solution that stores the output messages of a query into a temporary table (for example #outputMessages) with just one varchar column. When the execution of your query is completed, I would capture its output messages stored into #outputMessages table with just a SELECT * FROM #outputMessages. When the output messages capturing process is completed, just drop the temporary table.

Please also read the question linked by @Corak.

Sign up to request clarification or add additional context in comments.

Comments

0

So I did it in a different manner , I declared a variable , and get my print statement data in that variable and rather than printing the variable , i select that variable in the last part.

Now for C# , i use altertable.ExecuteScalar which return the first row as string, so for me it was what I needed.

string altertable1 = "print 'scfs'"; SqlCommand altertable = new SqlCommand(createtablecommand, connection);

string x = altertable.(function which return the printed query result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.