コード例 #1
0
        public async Task Add(TcpClientEndpoint endpoint)
        {
            //获取读写连接字符串
            var strConn = _sockerConnectionFactory.CreateAllForSocket();
            await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, false, false, strConn, async (conn, transaction) =>
            {
                SqlTransaction sqlTran = null;
                if (transaction != null)
                {
                    sqlTran = (SqlTransaction)transaction;
                }

                await using (SqlCommand commond = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    Transaction = sqlTran
                })
                {
                    if (endpoint.ID == Guid.Empty)
                    {
                        commond.CommandText = @"insert into TcpClientEndpoint([id],[name],[serveraddress],[serverport],[keepalive],[poolmaxsize],[executedatafactorytype],[executedatafactorytypeusedi],[heartbeatsenddata],[createtime],[modifytime])
                                    values(default,@name,@serveraddress,@serverport,@keepalive,@poolmaxsize,@executedatafactorytype,@executedatafactorytypeusedi,@heartbeatsenddata,getutcdate(),getutcdate());
                                    select @newid=[id] from TcpListenerLog where [sequence]=SCOPE_IDENTITY()";
                    }
                    else
                    {
                        commond.CommandText = @"insert into TcpClientEndpoint([id],[name],[serveraddress],[serverport],[keepalive],[poolmaxsize],[executedatafactorytype],[executedatafactorytypeusedi],[heartbeatsenddata],[createtime],[modifytime])
                                    values(@id,@name,@serveraddress,@serverport,@keepalive,@poolmaxsize,@executedatafactorytype,@executedatafactorytypeusedi,@heartbeatsenddata,getutcdate(),getutcdate())";
                    }

                    SqlParameter parameter;
                    if (endpoint.ID != Guid.Empty)
                    {
                        parameter = new SqlParameter("@id", SqlDbType.UniqueIdentifier)
                        {
                            Value = endpoint.ID
                        };
                        commond.Parameters.Add(parameter);
                    }
                    else
                    {
                        parameter = new SqlParameter("@newid", SqlDbType.UniqueIdentifier)
                        {
                            Direction = ParameterDirection.Output
                        };
                        commond.Parameters.Add(parameter);
                    }

                    parameter = new SqlParameter("@name", SqlDbType.NVarChar, 150)
                    {
                        Value = endpoint.Name
                    };
                    commond.Parameters.Add(parameter);

                    parameter = new SqlParameter("@serveraddress", SqlDbType.NVarChar, 150)
                    {
                        Value = endpoint.ServerAddress
                    };
                    commond.Parameters.Add(parameter);

                    parameter = new SqlParameter("@serverport", SqlDbType.Int)
                    {
                        Value = endpoint.ServerPort
                    };
                    commond.Parameters.Add(parameter);

                    parameter = new SqlParameter("@keepalive", SqlDbType.Bit)
                    {
                        Value = endpoint.KeepAlive
                    };
                    commond.Parameters.Add(parameter);

                    parameter = new SqlParameter("@poolmaxsize", SqlDbType.Int)
                    {
                        Value = endpoint.PoolMaxSize
                    };
                    commond.Parameters.Add(parameter);

                    parameter = new SqlParameter("@executedatafactorytype", SqlDbType.NVarChar, 150)
                    {
                        Value = endpoint.ExecuteDataFactoryType
                    };
                    commond.Parameters.Add(parameter);

                    parameter = new SqlParameter("@executedatafactorytypeusedi", SqlDbType.Bit)
                    {
                        Value = endpoint.ExecuteDataFactoryTypeUseDI
                    };
                    commond.Parameters.Add(parameter);

                    parameter = new SqlParameter("@heartbeatsenddata", SqlDbType.NVarChar, 500)
                    {
                        Value = endpoint.HeartBeatSendData
                    };
                    commond.Parameters.Add(parameter);


                    await commond.PrepareAsync();

                    await commond.ExecuteNonQueryAsync();

                    if (endpoint.ID == Guid.Empty)
                    {
                        endpoint.ID = (Guid)commond.Parameters["@newid"].Value;
                    }
                }
            });
        }
コード例 #2
0
        public async Task Add(TcpListener listener)
        {
            //获取读写连接字符串
            var strConn = _sockerConnectionFactory.CreateAllForSocket();
            await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, false, false, strConn, async (conn, transaction) =>
            {
                SqlTransaction sqlTran = null;
                if (transaction != null)
                {
                    sqlTran = (SqlTransaction)transaction;
                }

                await using (SqlCommand commond = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    Transaction = sqlTran
                })
                {
                    if (listener.ID == Guid.Empty)
                    {
                        commond.CommandText = @"insert into TcpListener([id],[name],[port],[keepalive],[maxconcurrencycount],[maxbuffercount],[executedatafactorytype],[executedatafactorytypeusedi],[heartbeatsenddata],[description],[createtime],[modifytime])
                                    values(default,@name,@port,@keepalive,@maxconcurrencycount,@maxbuffercount,@executedatafactorytype,@executedatafactorytypeusedi,@heartbeatsenddata,@description,getutcdate(),getutcdate());
                                    select @newid=[id] from TcpListener where [sequence]=SCOPE_IDENTITY()";
                    }
                    else
                    {
                        commond.CommandText = @"insert into TcpListener([id],[name],[port],[keepalive],[maxconcurrencycount],[maxbuffercount],[executedatafactorytype],[executedatafactorytypeusedi],[heartbeatsenddata],[description],[createtime],[modifytime])
                                    values(@id,@name,@port,@keepalive,@maxconcurrencycount,@maxbuffercount,@executedatafactorytype,@executedatafactorytypeusedi,@heartbeatsenddata,@description,getutcdate(),getutcdate())";
                    }

                    SqlParameter parameter;
                    if (listener.ID != Guid.Empty)
                    {
                        parameter = new SqlParameter("@id", SqlDbType.UniqueIdentifier)
                        {
                            Value = listener.ID
                        };
                        commond.Parameters.Add(parameter);
                    }
                    else
                    {
                        parameter = new SqlParameter("@newid", SqlDbType.UniqueIdentifier)
                        {
                            Direction = ParameterDirection.Output
                        };
                        commond.Parameters.Add(parameter);
                    }

                    parameter = new SqlParameter("@name", SqlDbType.NVarChar, 150)
                    {
                        Value = listener.Name
                    };
                    commond.Parameters.Add(parameter);


                    parameter = new SqlParameter("@port", SqlDbType.Int)
                    {
                        Value = listener.Port
                    };
                    commond.Parameters.Add(parameter);

                    parameter = new SqlParameter("@keepalive", SqlDbType.Bit)
                    {
                        Value = listener.KeepAlive
                    };
                    commond.Parameters.Add(parameter);

                    parameter = new SqlParameter("@maxconcurrencycount", SqlDbType.Int)
                    {
                        Value = listener.MaxConcurrencyCount
                    };
                    commond.Parameters.Add(parameter);


                    parameter = new SqlParameter("@maxbuffercount", SqlDbType.Int)
                    {
                        Value = listener.MaxBufferCount
                    };
                    commond.Parameters.Add(parameter);


                    parameter = new SqlParameter("@executedatafactorytype", SqlDbType.NVarChar, 500)
                    {
                        Value = listener.ExecuteDataFactoryType
                    };
                    commond.Parameters.Add(parameter);

                    parameter = new SqlParameter("@executedatafactorytypeusedi", SqlDbType.Bit)
                    {
                        Value = listener.ExecuteDataFactoryTypeUseDI
                    };
                    commond.Parameters.Add(parameter);

                    parameter = new SqlParameter("@heartbeatsenddata", SqlDbType.NVarChar, 500)
                    {
                        Value = listener.HeartBeatSendData
                    };
                    commond.Parameters.Add(parameter);


                    parameter = new SqlParameter("@description", SqlDbType.NVarChar, 1000)
                    {
                        Value = listener.Description
                    };
                    commond.Parameters.Add(parameter);

                    await commond.PrepareAsync();

                    int reply = 3;
                    while (true)
                    {
                        try
                        {
                            await commond.ExecuteNonQueryAsync();
                            break;
                        }
                        catch (SqlException ex)
                        {
                            if (reply > 0 && (ex.Number == 41302 || ex.Number == 41305 || ex.Number == 41325 || ex.Number == 41301 || ex.Number == 1205))
                            {
                                reply--;
                                System.Threading.Thread.Sleep(1);
                            }
                            else
                            {
                                if (ex.Number == 2601)
                                {
                                    var fragment = new TextFragment()
                                    {
                                        Code = TextCodes.ExistSameNameTcpListener,
                                        DefaultFormatting = "名称为{0}的Tcp监听器已经存在",
                                        ReplaceParameters = new List <object>()
                                        {
                                            listener.Name
                                        }
                                    };

                                    throw new UtilityException((int)Errors.ExistSameNameTcpListener, fragment);
                                }

                                throw;
                            }
                        }
                    }

                    if (listener.ID == Guid.Empty)
                    {
                        listener.ID = (Guid)commond.Parameters["@newid"].Value;
                    }
                }
            });
        }