2
我有一個CLR觸發器,它調用存儲過程與BeginInvoke
。在存儲過程中,我嘗試調用SqlComman,但得到:請求的操作需要Sql Server執行線程。當前線程是由用戶代碼或其他非Sql Server引擎代碼啓動的。CLR存儲過程需要一個SQL Server執行線程
我必須在SP中做這個事情,因爲等待,我必須給BeginInvoke
打電話。
代碼:
[SqlProcedure()]
public static void MySendData(String crudType, String sourceTable, ...) {
SqlCommand sqlDeleteComm;
String temp = String.Empty;
using(SqlConnection conn = new SqlConnection("context connection=true")) {
sqlDeleteComm = new SqlCommand("DELETE FROM #TEMP_TABLE");
sqlDeleteComm.Connection = conn;
try {
conn.Open();
sqlDeleteComm.ExecuteNonQuery();
conn.Close();
} catch(Exception ex) {
// --- here ---
// The requested operation requires a Sql Server execution thread. The current thread was started by user code or other non-Sql Server engine code.
}
}
...
}
[Microsoft.SqlServer.Server.SqlTrigger(Name = "MyTrigger", Target = "MyTable", Event = "FOR UPDATE, INSERT, DELETE")]
public static void MyTrigger() {
SqlTriggerContext myContext = SqlContext.TriggerContext;
MyDelagate d;
SqlCommand sqlComm;
SqlDataReader reader;
if(connection.State != ConnectionState.Open) {
connection.Open();
}
switch(myContext.TriggerAction) {
case TriggerAction.Update:
sqlComm = new SqlCommand("SELECT X, Y FROM Inserted");
sqlComm.Connection = connection;
reader = sqlComm.ExecuteReader();
try {
reader.Read();
d = new MyDelagate(MySendData);
d.BeginInvoke("Update", "MyTable", (Int32)reader[ 0 ], (Int32)reader[ 1 ], null, null);
} catch(Exception ex) {
} finally {
reader.Close();
}
break;
...
}
}
我怎麼可能,所有調用SQL查詢的SP?