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)]

转载于:https://www.cnblogs.com/tgdjw/p/4506771.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值