Reputation: 927
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;
Upvotes: 0
Views: 596
Reputation: 927
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);
Upvotes: 0
Reputation: 8227
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.
Upvotes: 1