Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to SQL Server messages via ADO.NET

Tags:

Is it possible to access the SQL Server "by-product messages" via ADO.NET? Due to the lack of words, by "by-product messages" I mean the output which appears in the Messages tab in Microsoft SQL Server Management Studio. What I particularly have it mind is to read the output of SET STATISTICS TIME ON. It appears that SqlDataReader does not offer anything in this matter.

like image 262
Jan Zich Avatar asked Apr 19 '09 09:04

Jan Zich


People also ask

How does ADO.NET connect to database?

Select tab from top menu-bar TOOLS, then Connect to Database… Browse your database file and click the OK button. After connecting to the new database file create an object of OleDBConnection class in case of a database like Oracle or MS-Access and create an object of SqlConnection class in case of MS-SQL database.

Can ADO.NET use in console application?

In this article, I'll create a console application, use ADO.NET SQL data provider classes to connect to a SQL Server database using C#, and access, update and execute SQL commands using ADO.NET. You can use the same code in your Windows Forms or WPF application.

What is SQL ADO?

ADO.NET is the data access component for the . NET Framework. ADO.NET is made of a set of classes that are used for connecting to a database and providing access to relational, XML or application data.

Which namespace is used when we connect our ASP NET page to SQL Server?

SqlClient (or the newer Microsoft. Data. SqlClient) is the provider or namespace you typically use to connect to an SQL Server.


1 Answers

Yes, there's an event on the SqlConnection class called SqlInfoMessage, which you can hook into:

SqlConnection _con =     new SqlConnection("server=.;database=Northwind;integrated Security=SSPI;");  _con.InfoMessage += new SqlInfoMessageEventHandler(InfoMessageHandler); 

The event handler will look like this:

static void InfoMessageHandler(object sender, SqlInfoMessageEventArgs e) {     string myMsg = e.Message;             } 

The e.Message is the message printed out to the message window in SQL Server Management Studio.

like image 100
marc_s Avatar answered Oct 19 '22 11:10

marc_s