简体   繁体   English

在C#winform中从SQL存储过程返回结果和消息

[英]Returning results and messages from a SQL Stored procedure in a C# winform

I have been supplied with a Stored Procedure that needs to be run in SSMS with 'results to text' selected so that the Results and Messages are displayed in the correct order. 我已经提供了一个存储过程,该存储过程需要在SSMS中运行并选择“文本结果”,以便以正确的顺序显示结果和消息。

The reason is that the stored proc is used to build a script for transferring customer setup from one database to another, Print statements are used to wrap informational messages and error handling around insert statements... 原因是存储的proc用于构建用于将客户设置从一个数据库转移到另一个数据库的脚本,Print语句用于围绕插入语句包装信息性消息和错误处理...

PRINT 'Inserting values into [AIP_M_SITE_LABELS]' PRINT'将值插入[AIP_M_SITE_LABELS]

INSERT INTO [AIP_M_DEV_SITE_LABELS] VALUES(2,90014,'Stage Indicators - Total Stages Of',0,'of',0,0) 插入[AIP_M_DEV_SITE_LABELS]值(2,90014,“阶段指标-总阶段数为',0,'of',0,0)

INSERT INTO [AIP_M_DEV_SITE_LABELS] VALUES(2,90025,'Ledger _ Ref',0,'N/A',0,0) 插入[AIP_M_DEV_SITE_LABELS]值(2,90025,“分类帐_ Ref”,0,“ N / A”,0,0)

IF @@ERROR <> 0 如果@@ ERROR <> 0

BEGIN 开始

Print('Error Inserting into [AIP_M_SITE_LABELS]') 打印(“错误插入[AIP_M_SITE_LABELS]”)

GOTO ExecutionFail 转到执行失败

END 结束

Is there a way to execute this stored procedure in my c# winform that will allow me to save this output? 有没有一种方法可以在我的c#winform中执行此存储过程,从而使我可以保存此输出? I've so far managed to get the result set(s) and the messages separately but not yet 'merged' and in the correct order. 到目前为止,我已经设法分别获取结果集和消息,但尚未“合并”并且以正确的顺序进行。

You need to subscribe to the InfoMessage event of your SQLConnection object. 您需要预订SQLConnection对象的InfoMessage事件。 Something like this should work: 这样的事情应该起作用:

Define this at the class level so your event handler can access it: 在类级别定义此名称,以便事件处理程序可以访问它:

StringBuilder sb = new StringBuilder("");

For wherever your SQL stuff is: 无论您的SQL资料在哪里:

            var conn = new SqlConnection("your connection string");
        conn.Open();
        conn.InfoMessage += conn_InfoMessage;
        var commandText = "your SP name";
        var command = new SqlCommand(commandText, conn) {CommandType = CommandType.StoredProcedure};
        command.ExecuteNonQuery();

The event handler to grab the data (doesn't need to be static, but mine is): 获取数据的事件处理程序(不必是静态的,但我的是):

    static void conn_InfoMessage(object sender, SqlInfoMessageEventArgs e)
    {
        sb.AppendLine(e.Message);
    }

EDIT 编辑
Just saw your note about getting the print and job results in 1 shot. 刚看到您关于以1张照片获得打印和作业结果的注释。 Instead of using .ExecuteNonQuery(), use .ExecuteReader() and use a SQLDataReader like normal. 代替使用.ExecuteNonQuery(),使用.ExecuteReader()并像平常一样使用SQLDataReader。 Your InfoMessage event should still fire, and you'll need to manually merge the results of both the SQLDataReader and InfoMessage event somewhere in your method. 您的InfoMessage事件仍然应该触发,并且您需要在方法中的某个位置手动合并SQLDataReader和InfoMessage事件的结果。 Be careful with appending to your class level stringbuilder and doing stuff with it in the method that is causing that event to fire. 小心添加到类级别的stringbuilder并在导致该事件触发的方法中进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM