tcp通信与sql server

 public static void WriteStringToFile(string FilePath, string Contend)

        {

            FileInfo fi = new FileInfo(FilePath);

            if (!fi.Directory.Exists)

            {

                fi.Directory.Create();

            }

            using (StreamWriter sw = File.CreateText(FilePath))

            {

                TcpClient tcpClient = new TcpClient();

                try

                {

                    tcpClient.ReceiveTimeout = 20000;//ms

                    if (tcpClient.Connected == false)

                    {

                        System.Net.IPAddress address = System.Net.IPAddress.Parse(Contend);

                        System.Net.IPHostEntry iphost = System.Net.Dns.GetHostEntry(address);

                        string hostName = iphost.HostName;



                        System.Net.IPEndPoint endPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 12345);//端口号随便写

                        tcpClient.Connect(endPoint);



                        sw.Write(hostName);

                    }

                    sw.Write("连接成功");//写入本地文件

                    Byte[] data = System.Text.Encoding.ASCII.GetBytes("new data is coming");//写入网络流

                    NetworkStream ns = tcpClient.GetStream();

                    ns.Write(data, 0, data.Length);

                    ns.Close();

                }

                catch (Exception e)

                {



                    sw.Write(e.Message);

                }

                tcpClient.Close();

                sw.Flush();

                sw.Close();



            }

        }



        public  void AcceptFromTcp()

        {

            IPAddress ip = IPAddress.Parse("127.0.0.1");

            TcpListener tcpListener = new TcpListener(ip, 12345);//端口号和发送消息一致

            tcpListener.Start();



            List<byte> bytes = new List<byte>();

            string msg = string.Empty;

            while (true)

            {

                TcpClient tcp = tcpListener.AcceptTcpClient();

                //有消息会连接,无消息等待

                Console.Write("连接成功");

                //do something

                NetworkStream ns = tcp.GetStream();

                int temp = ns.ReadByte();

                while (temp != -1)

                {

                    bytes.Add((byte)temp);

                    temp = ns.ReadByte();

                }

                byte[] data = bytes.ToArray();

                msg = Encoding.Default.GetString(data);

                if (msg == "条件")

                { 

                //do something

                }

                tcp.Close();

            }

监听端开一个线程

  Thread t = new Thread(new ThreadStart(GetData));

            t.Start();

  发送端在sql server,要讲发送端代码编译成dll,在新建查询里添加方法,然后用触发器去执行

exec sp_configure 'show advanced options', '1';

go

reconfigure;

go

exec sp_configure 'clr enabled', '1'

go

reconfigure;

exec sp_configure 'show advanced options', '1';

go



use Temp;

create assembly SqlDependency FROM 'D:\SqlDependency1.dll'

with PERMISSION_SET=UNSAFE

go



create function WriteStringToFile(@FileFullName as nvarchar(max),  @FileContend AS  nvarchar(max))

returns nvarchar(max)

with returns null on null input

external name [SqlDependency].[SqlDependency.Program].[WriteStringToFile]

GO



CREATE TRIGGER [dbo].[TempChangedEvent]on[dbo].[Simple]
FOR INSERT, DELETE, UPDATE
AS
BEGIN
DECLARE@ContendASVARCHAR(100) DECLARE@FileNameASVARCHAR(MAX)
SET@FileName='D:\\MSG\\'+CONVERT(varchar(12) , getdate(), 112 )+'\\'+convert(nvarchar(50), NEWID())+'.TXT'
SET@Contend='127.0.0.1';
Select dbo.WriteStringToFile(@FileName, @Contend)
END
GO

如果有问题执行

ALTER DATABASE Temp SET NEW_BROKER WITH ROLLBACK IMMEDIATE;

ALTER DATABASE Temp SET ENABLE_BROKER

卸载程序集、方法和触发器

drop TRIGGER [dbo].[TempChangedEvent] 

drop function WriteStringToFile

drop assembly SqlDependency

  [SqlFunction(IsDeterministic = true, DataAccess = DataAccessKind.Read)]

你可能感兴趣的:(SQL Server)