SQL触发器使用ASP.Net CLR

时间:2012-06-20 14:09:48

标签: asp.net sql-server-2008 clr

所以我正在尝试编写一个将调用ASP.Net DLL的SQL Server Trigger。

我已经把所有事情都说了,但我完全不知道当触发器触发时它无法看到“插入”表?!?!

Msg 6522, Level 16, State 1, Procedure etrition_train_trigger_clr35, Line 1
A .NET Framework error occurred during execution of user-defined routine or aggregate "etrition_train_trigger_clr35": 
System.Data.SqlClient.SqlException: Invalid object name 'inserted'.
System.Data.SqlClient.SqlException: 
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader()
   at CLRTrigger.CLRTrigger.SQLTriggerCall()

这是我的.Net代码。就像我说的那样,当触发条件满足时会触发,但由于某种原因它无法看到插入的表格。

public static void SQLTriggerCall()
{
    SqlTriggerContext triggContext = SqlContext.TriggerContext;
    SqlConnection cn = new SqlConnection("Server=vm260101;Database=RT_Training;uid=XXXXXX;pwd=XXXXXXX;Enlist=False;");
    cn.Open();
    SqlCommand sqlComm = cn.CreateCommand();
    SqlPipe sqlP = SqlContext.Pipe;
    SqlDataReader dr;
    sqlComm.CommandText = "SELECT stu_fee_gu, stu_fee_pay_trans_gu, amount from inserted";
    dr = sqlComm.ExecuteReader();
    String stu_fee_gu = "";
    String stu_fee_pay_trans_gu = "";
    float amount = 0;
    while (dr.Read())
    {
        stu_fee_gu = Convert.ToString(dr["stu_fee_gu"]);
        stu_fee_pay_trans_gu = Convert.ToString(dr["stu_fee_pay_trans_gu"]);
        amount = (float)Convert.ToDecimal(dr["amount"]);
    }
    cn.Close();
}

1 个答案:

答案 0 :(得分:3)

您打开的连接cn与触发器不共享相同的上下文。尝试使用context connection

请参阅SqlTriggerContext下的示例,了解触发器中的用法,例如:

// Retrieve the connection that the trigger is using.
  using (SqlConnection connection
     = new SqlConnection(@"context connection=true"))
  {
     connection.Open();

     // Get the inserted row.
     command = new SqlCommand(@"SELECT * FROM INSERTED;",
                              connection);

     // Get the user name and real name of the inserted user.
     reader = command.ExecuteReader();
     reader.Read();
     //More...

当然,该示例的其余部分非常差,因为它假设inserted中只有一行。不要在自己的代码中犯这个错误。