Example #1
0
        public async Task Add(HashRealNode node)
        {
            //获取读写连接字符串
            var strConn = _hashConnectionFactory.CreateAllForHash();
            await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, false, false, strConn, async (conn, transaction) =>
            {
                SqlTransaction sqlTran = null;
                if (transaction != null)
                {
                    sqlTran = (SqlTransaction)transaction;
                }

                using (SqlCommand command = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    Transaction = sqlTran,
                })
                {
                    SqlParameter parameter;
                    if (node.ID == Guid.Empty)
                    {
                        command.CommandText = @"
                                                    INSERT INTO [HASHREALNODE]
                                                      (
	                                                     [ID]
                                                        ,[Name]
	                                                    ,[GROUPID]
	                                                    ,[NODEKEY]
                                                        ,[CREATETIME]
                                                        ,[MODIFYTIME]
                                                      )VALUES(
	                                                      DEFAULT
                                                          ,@name
	                                                      ,@groupid	  
	                                                      ,@nodekey
	                                                      ,GETUTCDATE()
	                                                      ,GETUTCDATE()
                                                      );
                                                     select @newid =[id] from HASHREALNODE where [sequence] = SCOPE_IDENTITY()";
                        parameter           = new SqlParameter("@newid", SqlDbType.UniqueIdentifier)
                        {
                            Direction = ParameterDirection.Output
                        };
                        command.Parameters.Add(parameter);
                    }
                    else
                    {
                        command.CommandText = @"
                                                INSERT INTO [HASHREALNODE]
                                                      (
	                                                     [ID]
                                                        ,[Name]
	                                                    ,[GROUPID]
	                                                    ,[NODEKEY]
                                                        ,[CREATETIME]
                                                        ,[MODIFYTIME]
                                                      )VALUES(
	                                                      @id
                                                          ,@name
	                                                      ,@groupid	  
	                                                      ,@nodekey
	                                                      ,GETUTCDATE()
	                                                      ,GETUTCDATE()
                                                      )";

                        parameter = new SqlParameter("@id", SqlDbType.UniqueIdentifier)
                        {
                            Value = node.ID
                        };
                        command.Parameters.Add(parameter);
                    }

                    parameter = new SqlParameter("@name", SqlDbType.VarChar, 100)
                    {
                        Value = node.Name
                    };
                    command.Parameters.Add(parameter);

                    parameter = new SqlParameter("@groupid", SqlDbType.UniqueIdentifier)
                    {
                        Value = node.GroupId
                    };
                    command.Parameters.Add(parameter);
                    parameter = new SqlParameter("@nodekey", SqlDbType.VarChar, 4000)
                    {
                        Value = node.NodeKey
                    };
                    command.Parameters.Add(parameter);

                    command.Prepare();

                    await command.ExecuteNonQueryAsync();


                    //如果用户未赋值ID则创建成功后返回ID
                    if (node.ID == Guid.Empty)
                    {
                        node.ID = (Guid)command.Parameters["@newid"].Value;
                    }
                    ;
                }
            });
        }
Example #2
0
        public async Task Add(HashGroup group)
        {
            //获取读写连接字符串
            var strConn = _hashConnectionFactory.CreateAllForHash();
            await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, false, false, strConn, async (conn, transaction) =>
            {
                SqlTransaction sqlTran = null;
                if (transaction != null)
                {
                    sqlTran = (SqlTransaction)transaction;
                }

                using (SqlCommand commond = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    Transaction = sqlTran
                })
                {
                    SqlParameter parameter;
                    if (group.ID != Guid.Empty)
                    {
                        commond.CommandText = @"
                                                insert into [dbo].[HashGroup](
                                                                                    [id]
                                                                                    ,[name]
                                                                                    ,[count]
                                                                                    ,[strategyid]
                                                                                    ,[createtime]
                                                                                    ,[modifytime]
                                                                            )
                                                                            values(
                                                                                    @id
                                                                                    ,@name
                                                                                    ,@count
                                                                                    ,@strategyid
                                                                                    ,getutcdate()
                                                                                    ,getutcdate()
                                                                                 )	
		                                       "        ;

                        parameter = new SqlParameter("@id", SqlDbType.UniqueIdentifier)
                        {
                            Value = group.ID
                        };
                        commond.Parameters.Add(parameter);
                    }
                    else
                    {
                        commond.CommandText = @"
                                                insert into [dbo].[HashGroup](
                                                            [id]
                                                            ,[name]
                                                            ,[count]
                                                            ,[strategyid]
                                                            ,[createtime]
                                                            ,[modifytime]
                                                        )
                                                        values(
                                                                default
                                                                ,@name
                                                                ,@count
                                                                ,@strategyid
                                                                ,getutcdate()
                                                                ,getutcdate()
                                                        )
                                                select 
                                                        @newid=[id] 
                                                        from [dbo].[HashGroup] 
                                                where 
                                                        [sequence]=SCOPE_IDENTITY()";

                        parameter = new SqlParameter("@newid", SqlDbType.UniqueIdentifier)
                        {
                            Direction = ParameterDirection.Output
                        };
                        commond.Parameters.Add(parameter);
                    }

                    parameter = new SqlParameter("@name", SqlDbType.VarChar, 100)
                    {
                        Value = group.Name
                    };
                    commond.Parameters.Add(parameter);

                    parameter = new SqlParameter("@type", SqlDbType.VarChar, 100)
                    {
                        Value = group.Type
                    };
                    commond.Parameters.Add(parameter);

                    parameter = new SqlParameter("@count", SqlDbType.BigInt)
                    {
                        Value = group.Count
                    };
                    commond.Parameters.Add(parameter);

                    parameter = new SqlParameter("@strategyid", SqlDbType.UniqueIdentifier)
                    {
                        Value = group.StrategyID
                    };
                    commond.Parameters.Add(parameter);


                    commond.Prepare();

                    await commond.ExecuteNonQueryAsync();

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