如何从其他类订阅SQL InfoMessage?

时间:2016-11-15 12:50:53

标签: c# sql

我有两个班级:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <div ng-app="myApp" ng-controller="myCtrl"> <div class="col-md-12"> <div class="col-md-6"> <div ng-repeat="check in checks"> <div class="form-group"> <label>check Amount:</label> <div class="col-md-5"> <input type="text" class="form-control" id="{{'id'+($index+1)}}" ng-model="check['checkAmt']" maxlength="6" /> </div> </div> </div> <button type="button" ng-click="add()">Add More</button> </div> </div> <div class="col-md-6"> <!--<p ng-repeat="check in checks">{{check['checkAmt']}}</p>--> </div> <label>{{tot()}}</label> <label>{{total()}}</label> </div>这是我的表单,并有一个列表框,等待Class Home发送的所有InfoMessage。

我想知道如何将Class SQL Class SQL传递给InfoMessages

Class Home我的代码如下:

Class SQL

代码有:

public void runSqlCmd(string sqlCmd, Boolean dsRequired)
{
    try
    {
        using (SqlConnection con = new SqlConnection(conString))
        {
            try
            {
                con.Open();
                con.InfoMessage += delegate (object sender, SqlInfoMessageEventArgs e)
                {
                     sqlMsg += "\n" + e.Message;
                };

                if (dsRequired)
                {
                    ds.Clear();
                    SqlDataAdapter da = new SqlDataAdapter(sqlCmd, con);
                    da.Fill(ds);
                }
                else if (!dsRequired)
                {
                    using (SqlCommand cmd = new SqlCommand(sqlCmd, con))
                    {
                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                throw sqlEx;
            }
            finally
            {
                con.Close();
            }
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

是我想要的

我的con.InfoMessage += delegate (object sender, SqlInfoMessageEventArgs e) { sqlMsg += "\n" + e.Message; }; 代码是:

ClassHome

1 个答案:

答案 0 :(得分:1)

您可以在本文ADO-NET-Asynchronous-SQL-Calls

中找到示例

你需要写这样的东西

public class ClassHome
{
    public static void ConnectionInfoMessage(object sender, SqlInfoMessageEventArgs e)
    {
        if (e.Errors.Count > 0)
        {
            // Check to make sure we are information only messages
            Console.WriteLine("Received {0} messages", e.Errors.Count);
            foreach (SqlError info in e.Errors)
            {
                if (info.Class > 9) // Severity
                {
                    Console.WriteLine("Error Message : {0} :   State : {1}", info.Message, info.State);
                }
                else
                {
                    Console.WriteLine("Info Message : {0} :   State : {1}", info.Message, info.State);
                }
            }
        }
        else
        {
            Console.WriteLine("Received Connection Info Message : {0}", e.Message);
        }
    }
}

然后从SQL Class订阅

command.Connection.InfoMessage += ClassHome.ConnectionInfoMessage;