Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture Stored Procedure print output in .NET (Different model!)

Basically, this question with a difference...

Is it possible to capture print output from a TSQL stored procedure in .NET, using the Entity Framework?

The solution in the other question doesn't work for me. It works with the connection type from System.Data.SqlClient but I'm using the one from System.Data.EntityClient which does not have an InfoMessage event. (Of course, I could just create an SQL connection based on the Entity connection settings, but prefer to do it directly.)

like image 967
Wim ten Brink Avatar asked Mar 08 '10 14:03

Wim ten Brink


Video Answer


3 Answers

Just to show people a complete working example from Craig's answer and Wim's response:

var entityConnection = (EntityConnection)Context.Connection;
var sqlConnection = (SqlConnection)entityConnection.StoreConnection;
sqlConnection.InfoMessage += (s,a) => Debug.WriteLine(a.Message);

​S​t​e​ve​​​​​​

like image 149
samneric Avatar answered Sep 20 '22 23:09

samneric


This is what worked for me, samneric's example was helpful but not the exact syntax.

string message = Empty.string;

using (var context1 = new DatabaseEntities())
            {
                var conn =
                ((EntityConnection)
                    ((IObjectContextAdapter)context1).ObjectContext.Connection);

                var sqlConnection = (SqlConnection)conn.StoreConnection;

                sqlConnection.InfoMessage += (s, a) => message = a.Message;

              data = context1.storedProc("Parameter");


            }
like image 38
Ryan Dooley Avatar answered Sep 20 '22 23:09

Ryan Dooley


Actually, it does, but since the EF is not SQL Server specific, you have to cast it:

var sqlConn = (SqlConnection)Context.Connection.StoreConnection;
like image 34
Craig Stuntz Avatar answered Sep 17 '22 23:09

Craig Stuntz