Exemplo n.º 1
0
        public void CreateFriendMsg(friend_messages friendMessage)
        {
            using (DbConnection connection = CreateConnection())
            {
                connection.Open();
                DbTransaction transaction = connection.BeginTransaction();

                DbCommand command = connection.CreateCommand();
                command.Transaction = transaction;

                try
                {
                    //создаем параметры
                    DbParameter userIdParameter = command.CreateParameter();
                    userIdParameter.DbType        = System.Data.DbType.Int32;
                    userIdParameter.IsNullable    = false;
                    userIdParameter.ParameterName = "@UserId";
                    userIdParameter.Value         = friendMessage.user_id;

                    DbParameter friendIdParameter = command.CreateParameter();
                    friendIdParameter.DbType        = System.Data.DbType.Int32;
                    friendIdParameter.IsNullable    = false;
                    friendIdParameter.ParameterName = "@FriendId";
                    friendIdParameter.Value         = friendMessage.friend_id;

                    DbParameter messageParameter = command.CreateParameter();
                    messageParameter.DbType        = System.Data.DbType.String;
                    messageParameter.IsNullable    = false;
                    messageParameter.ParameterName = "@Message";
                    messageParameter.Value         = friendMessage.message;

                    command.Parameters.AddRange(new DbParameter[] { userIdParameter, friendIdParameter, messageParameter });

                    command.CommandText = $@"INSERT INTO [dbo].[friend_messages]
                   ([user_id], [friend_id], [message]) VALUES
                   (@UserId, @FriendId, @Message)";

                    // command.CommandText = $@"INSERT INTO [dbo].[friend_messages]
                    //([user_id], [friend_id], [message]) VALUES
                    //(@UserId, @FriendId, @Message, {friendMessage.send_date.ToString("yyyy-MM-dd HH:mm:ss")})";

                    command.ExecuteNonQuery();

                    transaction.Commit();
                }
                catch (DbException ex)
                {
                    transaction.Rollback();
                }
            }
        }
Exemplo n.º 2
0
        private void BtnSend_Click(object sender, RoutedEventArgs e)
        {
            if (txtBox.Text != "")
            {
                friend_messages friend_Messages = new friend_messages()
                {
                    user_id = AuthenticationService.CurrentUser.user_id, friend_id = friendUser.user_id, message = txtBox.Text, send_date = DateTime.Now
                };

                FriendMessageService friendMessageService = new FriendMessageService();

                friendMessageService.CreateFriendMsg(friend_Messages);

                string result = friendMessageService.GetChatBetweenTwoUsers(AuthenticationService.CurrentUser, friendUser);

                txtBox2.Text = result;
            }
        }