作者:咸菜地瓜的丽质人生知_467 | 来源:互联网 | 2023-05-17 12:31
需要在C#中获取SQLServer“PRINT”值我有一个SP将结果打印到SQLServer,但我需要在C#中使用该值。现在无法将PRINT更改为SELECT。我试过SqlComm
需要在C#中获取SQL Server“PRINT”值
我有一个SP将结果打印到SQL Server ,但我需要在C#中使用该值。
现在无法将PRINT
更改为SELECT
。 我试过SqlCommand.ExecuteScalar()
但是没有用。
有人知道是否有可能在SP中将PRINT
命令的值重定向到C# ?
例:
CREATE PROCEDURE doXYZ AS BEGIN PRINT 'XYZ' END
现在在C#中我需要获得值’XYZ’….任何想法?
您可以使用SqlConnection.InfoMessage
事件。
您可以像这样使用SqlConnection.InfoMessage
事件:
上述就是C#学习教程:需要在C#中获取SQL Server“PRINT”值分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—编程笔记
using System.Data; using System.Data.SqlClient; namespace foo { class bar { static public void ExecuteStoredProc() { var cOnnectionString= "Data Source=.;Integrated Security=True;Pooling=False;Initial Catalog=YourDatabaseName"; using (var cOnnection= new SqlConnection(connectionString)) using (var command = new SqlCommand("dbo.YourStoredProcedure", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@YourParameterName", "YourParameterValue"); connection.Open(); // wire up an event handler to the connection.InfoMessage event connection.InfoMessage += connection_InfoMessage; var result = command.ExecuteNonQuery(); connection.Close(); } } static void connection_InfoMessage(object sender, SqlInfoMessageEventArgs e) { // this gets the print statements (maybe the error statements?) var outputFromStoredProcedure = e.Message; } } }