SQL Server CLR Stored Procedure example

Source: Internet
Author: User
Tags stored procedure example
Recently, a system was developed to work with Mo lazy units. When discussing interfaces, considering that the packets defined by them are used for communication, it will inevitably lead to a long joint debugging time, and many projects have been plagued recently, there is not enough time to play with this type of lazy unit, so I am impulsive to say that it is enough to develop a database to write it into the database. However, the problem is described as follows:
The system is a C/S architecture. When a lazy organization sends a message to the server, the server analyzes and processes the information and sends it to one of the clients, this model can be simplified.
Lazy organization ----- push message -----> server processing ----- push message -----> client receiving
Use Database (SQL)
Lazy organization ----- push message -----> this step is complete, but how can I immediately notify the server to process the message when it is stored in the database? I thought it was a continuous round of notifications, for example, you can check the table every second. But in this case, the table must be large over time, so each select operation takes a long time, even how to optimize the use of indexes may not solve the problem well.
If you remove part of the table data (delete/transfer to another table) at intervals, but the system cannot stop and requires high real-time performance, the method of pulling messages by polling is always difficult to solve.

This problem has been plagued for several days. Later I learned from the programming event processing mode. Considering the fact that a trigger is added to a table, how does the trigger communicate with the application and how can I get new records? From this, let's think about it. We can use stored procedures. CLR stored procedures. As long as someone else calls my stored procedure, I can send a signal to the application during insertion, but how should I send it? I thought I could write a UDP sender program to send it to the local machine, so it turned into a push message.

The Code is as follows:

 

Using system;
Using system. Data;
Using system. Data. sqlclient;
Using system. Data. sqltypes;
Using Microsoft. sqlserver. server;
Using system. net;
Using system. net. Sockets;
Using system. text;

Public partial class storedprocedures
...{
Private Static readonly string sp_call = "ring ";
[Microsoft. sqlserver. server. sqlprocedure]
Public static void clr_ring (string from, string)
...{
Sqlcommand mycommand = new sqlcommand ();
Mycommand. commandtype = commandtype. storedprocedure;
Mycommand. commandtext = sp_call;
Mycommand. Parameters. addwithvalue ("@ fromnumber", from );
Mycommand. Parameters. addwithvalue ("@ tonumber", );
Sqlpipe mypipe = sqlcontext. pipe;
Mypipe. executeandsend (mycommand );

Udpclient client = new udpclient ();
String tosend = "message:" + from + "|" + to + "";
Byte [] bytes = unicodeencoding. Unicode. getbytes (tosend );
Client. Connect ("localhost", 3390 );
Client. Send (bytes, bytes. Length );
Client. Close ();
}
};

Finally, set the security level of the assembly to unsafe, and the Assembly owner to DBO.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.